26 return reinterpret_cast<uint8_t &
>(i);
35 int slength=
static_cast<int>(wcslen(s));
37 WideCharToMultiByte(CP_UTF8, 0, s, slength, NULL, 0, NULL, NULL);
38 std::string
r(rlength, 0);
39 WideCharToMultiByte(CP_UTF8, 0, s, slength, &
r[0], rlength, NULL, NULL);
48 r+=
static_cast<char>(*s);
56 std::wstring
widen(
const char *s)
60 int slength=
static_cast<int>(strlen(s));
62 MultiByteToWideChar(CP_UTF8, 0, s, slength, NULL, 0);
63 std::wstring
r(rlength, 0);
64 MultiByteToWideChar(CP_UTF8, 0, s, slength, &
r[0], rlength);
81 std::string
narrow(
const std::wstring &s)
85 int slength=
static_cast<int>(s.size());
87 WideCharToMultiByte(CP_UTF8, 0, &s[0], slength, NULL, 0, NULL, NULL);
88 std::string
r(rlength, 0);
89 WideCharToMultiByte(CP_UTF8, 0, &s[0], slength, &
r[0], rlength, NULL, NULL);
94 return std::string(s.begin(), s.end());
98 std::wstring
widen(
const std::string &s)
102 int slength=
static_cast<int>(s.size());
104 MultiByteToWideChar(CP_UTF8, 0, &s[0], slength, NULL, 0);
105 std::wstring
r(rlength, 0);
106 MultiByteToWideChar(CP_UTF8, 0, &s[0], slength, &
r[0], rlength);
111 return std::wstring(s.begin(), s.end());
120 result+=
static_cast<char>(c);
123 result+=
static_cast<char>((c >> 6) | 0xc0);
124 result+=
static_cast<char>((c &0x3f) | 0x80);
128 result+=
static_cast<char>((c >> 12) | 0xe0);
129 result+=
static_cast<char>(((c >> 6) &0x3f) | 0x80);
130 result+=
static_cast<char>((c &0x3f) | 0x80);
134 result+=
static_cast<char>((c >> 18) | 0xf0);
135 result+=
static_cast<char>(((c >> 12) &0x3f)| 0x80);
136 result+=
static_cast<char>(((c >> 6) &0x3f) | 0x80);
137 result+=
static_cast<char>((c &0x3f) | 0x80);
147 result.reserve(s.size());
149 for(
const auto c : s)
155 std::vector<std::string>
narrow_argv(
int argc,
const wchar_t **argv_wide)
157 if(argv_wide==
nullptr)
158 return std::vector<std::string>();
160 std::vector<std::string> argv_narrow(argc);
162 for(
int i=0; i!=argc; ++i)
163 argv_narrow[i]=
narrow(argv_wide[i]);
173 uint16_t b1=x & 0xFF;
174 uint16_t b2=x & 0xFF00;
175 return (b1 << 8) | (b2 >> 8);
188 result+=
static_cast<wchar_t>(a);
198 unsigned int i1=((code>>10) & 0x3ff) | 0xD800;
199 unsigned int a1=(swap_bytes)?
do_swap_bytes(static_cast<uint16_t>(i1)):i1;
200 result+=
static_cast<wchar_t>(a1);
201 unsigned int i2=(code & 0x3ff) | 0xDC00;
202 unsigned int a2=(swap_bytes)?
do_swap_bytes(static_cast<uint16_t>(i2)):i2;
203 result+=
static_cast<wchar_t>(a2);
215 result.reserve(in.size());
219 unsigned char c=in[i++];
229 else if(c<=0xDF && i<in.size())
234 code=(c & 0x1F) << 6;
238 else if(c<=0xEF && i+1<in.size())
240 code=(c & 0xF) << 12;
242 code+=(c & 0x3F) << 6;
246 else if(c<=0xF7 && i+2<in.size())
248 code=(c & 0x7) << 18;
250 code+=(c & 0x3F) << 12;
252 code+=(c & 0x3F) << 6;
293 std::ostringstream &result,
294 const std::locale &
loc)
311 else if(ch <= 255 && isprint(ch,
loc))
313 const auto uch =
static_cast<unsigned char>(ch);
315 if(uch ==
'"' || uch ==
'\\' || uch ==
'\'')
323 result <<
"\\u" << std::hex << std::setw(4) << std::setfill(
'0')
324 <<
static_cast<unsigned int>(ch);
332 std::ostringstream result;
333 const std::locale
loc;
342 std::ostringstream result;
343 const std::locale
loc;
344 for(
const auto ch : in)
std::string narrow(const wchar_t *s)
std::wstring widen(const char *s)
uint16_t do_swap_bytes(uint16_t x)
A helper function for dealing with different UTF16 endians.
std::string utf32_to_utf8(const std::basic_string< unsigned int > &s)
std::wstring utf8_to_utf16(const std::string &in, bool swap_bytes)
unsignedbv_typet size_type()
bool is_little_endian_arch()
Determine endianness of the architecture.
std::vector< std::string > narrow_argv(int argc, const wchar_t **argv_wide)
static void utf8_append_code(unsigned int c, std::string &result)
Appends a unicode character to a utf8-encoded string.
std::wstring utf8_to_utf16_little_endian(const std::string &in)
static void utf16_little_endian_to_java(const wchar_t ch, std::ostringstream &result, const std::locale &loc)
void utf16_append_code(unsigned int code, bool swap_bytes, std::wstring &result)
std::wstring utf8_to_utf16_big_endian(const std::string &in)