| 1 | // Copyright 2013 The Flutter Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "flutter/fml/string_conversion.h" |
| 6 | |
| 7 | #include <codecvt> |
| 8 | #include <locale> |
| 9 | #include <sstream> |
| 10 | #include <string> |
| 11 | |
| 12 | #include "flutter/fml/build_config.h" |
| 13 | |
| 14 | #if defined(FML_OS_WIN) |
| 15 | // TODO(naifu): https://github.com/flutter/flutter/issues/98074 |
| 16 | // Eliminate this workaround for a link error on Windows when the underlying |
| 17 | // bug is fixed. |
| 18 | std::locale::id std::codecvt<char16_t, char, _Mbstatet>::id; |
| 19 | #endif // defined(FML_OS_WIN) |
| 20 | |
| 21 | namespace fml { |
| 22 | |
| 23 | using Utf16StringConverter = |
| 24 | std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>; |
| 25 | |
| 26 | std::string Join(const std::vector<std::string>& vec, const char* delim) { |
| 27 | std::stringstream res; |
| 28 | for (size_t i = 0; i < vec.size(); ++i) { |
| 29 | res << vec[i]; |
| 30 | if (i < vec.size() - 1) { |
| 31 | res << delim; |
| 32 | } |
| 33 | } |
| 34 | return res.str(); |
| 35 | } |
| 36 | |
| 37 | std::string Utf16ToUtf8(const std::u16string_view string) { |
| 38 | Utf16StringConverter converter; |
| 39 | return converter.to_bytes(wptr: string.data()); |
| 40 | } |
| 41 | |
| 42 | std::u16string Utf8ToUtf16(const std::string_view string) { |
| 43 | Utf16StringConverter converter; |
| 44 | return converter.from_bytes(ptr: string.data()); |
| 45 | } |
| 46 | |
| 47 | } // namespace fml |
| 48 |
