20# include <util/pragma_push.def>
22# pragma warning(disable : 4668)
24# pragma warning(disable : 5039)
27# include <util/pragma_pop.def>
37 int slength =
static_cast<int>(wcslen(s));
39 WideCharToMultiByte(CP_UTF8, 0, s, slength, NULL, 0, NULL, NULL);
40 std::string
r(rlength, 0);
41 WideCharToMultiByte(CP_UTF8, 0, s, slength, &
r[0], rlength, NULL, NULL);
45 return narrow(std::wstring(s));
49std::wstring
widen(
const char *s)
53 int slength =
static_cast<int>(strlen(s));
54 int rlength = MultiByteToWideChar(CP_UTF8, 0, s, slength, NULL, 0);
55 std::wstring
r(rlength, 0);
56 MultiByteToWideChar(CP_UTF8, 0, s, slength, &
r[0], rlength);
60 return widen(std::string(s));
64std::string
narrow(
const std::wstring &s)
68 int slength =
static_cast<int>(s.size());
70 WideCharToMultiByte(CP_UTF8, 0, &s[0], slength, NULL, 0, NULL, NULL);
71 std::string
r(rlength, 0);
72 WideCharToMultiByte(CP_UTF8, 0, &s[0], slength, &
r[0], rlength, NULL, NULL);
78 result.reserve(s.size());
80 for(
const auto codepoint : s)
87std::wstring
widen(
const std::string &s)
91 int slength =
static_cast<int>(s.size());
92 int rlength = MultiByteToWideChar(CP_UTF8, 0, &s[0], slength, NULL, 0);
93 std::wstring
r(rlength, 0);
94 MultiByteToWideChar(CP_UTF8, 0, &s[0], slength, &
r[0], rlength);
101 r.reserve(utf32.size());
102 for(
auto codepoint : utf32)
113 result +=
static_cast<char>(c);
116 result +=
static_cast<char>((c >> 6) | 0xc0);
117 result +=
static_cast<char>((c & 0x3f) | 0x80);
121 result +=
static_cast<char>((c >> 12) | 0xe0);
122 result +=
static_cast<char>(((c >> 6) & 0x3f) | 0x80);
123 result +=
static_cast<char>((c & 0x3f) | 0x80);
127 result +=
static_cast<char>((c >> 18) | 0xf0);
128 result +=
static_cast<char>(((c >> 12) & 0x3f) | 0x80);
129 result +=
static_cast<char>(((c >> 6) & 0x3f) | 0x80);
130 result +=
static_cast<char>((c & 0x3f) | 0x80);
140 result.reserve(s.size());
142 for(
const auto c : s)
148std::vector<std::string>
narrow_argv(
int argc,
const wchar_t **argv_wide)
150 if(argv_wide ==
nullptr)
151 return std::vector<std::string>();
153 std::vector<std::string> argv_narrow;
154 argv_narrow.reserve(argc);
156 for(
int i = 0; i != argc; ++i)
157 argv_narrow.push_back(
narrow(argv_wide[i]));
170 result +=
static_cast<wchar_t>(code);
179 code = code - 0x10000;
180 const uint16_t i1 =
static_cast<uint16_t
>(((code >> 10) & 0x3ff) | 0xD800);
181 result +=
static_cast<wchar_t>(i1);
182 const uint16_t i2 =
static_cast<uint16_t
>((code & 0x3ff) | 0xDC00);
183 result +=
static_cast<wchar_t>(i2);
194 result.reserve(in.size());
207 std::u32string result;
208 result.reserve(utf8_str.size());
209 std::string::size_type i = 0;
210 while(i < utf8_str.size())
212 unsigned char c = utf8_str[i++];
222 else if(c <= 0xDF && i < utf8_str.size())
227 code = (c & 0x1Fu) << 6;
231 else if(c <= 0xEF && i + 1 < utf8_str.size())
233 code = (c & 0xFu) << 12;
235 code += (c & 0x3Fu) << 6;
239 else if(c <= 0xF7 && i + 2 < utf8_str.size())
241 code = (c & 0x7u) << 18;
243 code += (c & 0x3Fu) << 12;
245 code += (c & 0x3Fu) << 6;
258 result.append(1, code);
274 std::ostringstream &result,
275 const std::locale &loc)
292 else if(ch <= 255 && isprint(ch, loc))
294 const auto uch =
static_cast<unsigned char>(ch);
297 if(uch ==
'"' || uch ==
'\\')
305 result <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0')
306 <<
static_cast<unsigned int>(ch);
318 std::ostringstream &result,
319 const std::locale &loc)
321 if(ch == (
wchar_t)
'\'')
323 const auto uch =
static_cast<unsigned char>(ch);
325 result <<
'\\' << uch;
337 std::ostringstream result;
338 const std::locale loc;
352 std::ostringstream result;
353 const std::locale loc;
354 for(
const auto ch : in)
369 std::wstring wide_string(utf16_str.begin(), utf16_str.end());
370 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>{}
371 .to_bytes(wide_string);
373 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,
char16_t>{}
374 .to_bytes(utf16_str);
381 return std::strtol(hex.c_str(),
nullptr, 16);
output_type narrow(input_type input)
Run-time checked narrowing cast.
#define PRECONDITION(CONDITION)
std::string narrow(const wchar_t *s)
std::u32string utf8_to_utf32(const std::string &utf8_str)
Convert UTF8-encoded string to UTF-32 with architecture-native endianness.
static void utf16_native_endian_to_java(const wchar_t ch, std::ostringstream &result, const std::locale &loc)
Escapes non-printable characters, whitespace except for spaces, double- and single-quotes and backsla...
std::wstring widen(const char *s)
static void utf16_append_code(unsigned int code, std::wstring &result)
static void utf16_native_endian_to_java_string(const wchar_t ch, std::ostringstream &result, const std::locale &loc)
Escapes non-printable characters, whitespace except for spaces, double quotes and backslashes.
std::string utf16_native_endian_to_utf8(const char16_t utf16_char)
char16_t codepoint_hex_to_utf16_native_endian(const std::string &hex)
std::string utf32_native_endian_to_utf8(const std::basic_string< char32_t > &s)
std::vector< std::string > narrow_argv(int argc, const wchar_t **argv_wide)
std::string codepoint_hex_to_utf8(const std::string &hex)
std::wstring utf8_to_utf16_native_endian(const std::string &in)
Convert UTF8-encoded string to UTF-16 with architecture-native endianness.
static void utf8_append_code(unsigned int c, std::string &)
Appends a unicode character to a utf8-encoded string.