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