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));
49 std::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));
64 std::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)
87 std::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);
141 result.reserve(s.size());
143 for(
const auto c : s)
149 std::vector<std::string>
narrow_argv(
int argc,
const wchar_t **argv_wide)
151 if(argv_wide ==
nullptr)
152 return std::vector<std::string>();
154 std::vector<std::string> argv_narrow;
155 argv_narrow.reserve(argc);
157 for(
int i = 0; i != argc; ++i)
158 argv_narrow.push_back(
narrow(argv_wide[i]));
171 result +=
static_cast<wchar_t>(code);
180 code = code - 0x10000;
181 const uint16_t i1 =
static_cast<uint16_t
>(((code >> 10) & 0x3ff) | 0xD800);
182 result +=
static_cast<wchar_t>(i1);
183 const uint16_t i2 =
static_cast<uint16_t
>((code & 0x3ff) | 0xDC00);
184 result +=
static_cast<wchar_t>(i2);
195 result.reserve(in.size());
208 std::u32string result;
209 result.reserve(utf8_str.size());
211 while(i < utf8_str.size())
213 unsigned char c = utf8_str[i++];
223 else if(c <= 0xDF && i < utf8_str.size())
228 code = (c & 0x1Fu) << 6;
232 else if(c <= 0xEF && i + 1 < utf8_str.size())
234 code = (c & 0xFu) << 12;
236 code += (c & 0x3Fu) << 6;
240 else if(c <= 0xF7 && i + 2 < utf8_str.size())
242 code = (c & 0x7u) << 18;
244 code += (c & 0x3Fu) << 12;
246 code += (c & 0x3Fu) << 6;
259 result.append(1, code);
275 std::ostringstream &result,
276 const std::locale &loc)
293 else if(ch <= 255 &&
isprint(ch, loc))
295 const auto uch =
static_cast<unsigned char>(ch);
298 if(uch ==
'"' || uch ==
'\\')
306 result <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0')
307 <<
static_cast<unsigned int>(ch);
319 std::ostringstream &result,
320 const std::locale &loc)
322 if(ch == (
wchar_t)
'\'')
324 const auto uch =
static_cast<unsigned char>(ch);
326 result <<
'\\' << uch;
338 std::ostringstream result;
339 const std::locale loc;
353 std::ostringstream result;
354 const std::locale loc;
355 for(
const auto ch : in)
370 std::wstring wide_string(utf16_str.begin(), utf16_str.end());
371 return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>,
wchar_t>{}
372 .to_bytes(wide_string);
374 return std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}
375 .to_bytes(utf16_str);
#define PRECONDITION(CONDITION)
long strtol(const char *nptr, char **endptr, int base)
size_t strlen(const char *s)
std::vector< std::string > narrow_argv(int argc, const wchar_t **argv_wide)
std::string narrow(const wchar_t *s)
std::string utf32_native_endian_to_utf8(const std::basic_string< unsigned int > &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 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.