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)
157 if(argv_wide==
nullptr)
161 const char **argv_narrow=
new const char *[argc+1];
162 argv_narrow[argc]=
nullptr;
164 for(
int i=0; i<argc; i++)
165 argv_narrow[i]=strdup(
narrow(argv_wide[i]).c_str());
175 uint16_t b1=x & 0xFF;
176 uint16_t b2=x & 0xFF00;
177 return (b1 << 8) | (b2 >> 8);
190 result+=
static_cast<wchar_t>(a);
200 unsigned int i1=((code>>10) & 0x3ff) | 0xD800;
201 unsigned int a1=(swap_bytes)?
do_swap_bytes(static_cast<uint16_t>(i1)):i1;
202 result+=
static_cast<wchar_t>(a1);
203 unsigned int i2=(code & 0x3ff) | 0xDC00;
204 unsigned int a2=(swap_bytes)?
do_swap_bytes(static_cast<uint16_t>(i2)):i2;
205 result+=
static_cast<wchar_t>(a2);
217 result.reserve(in.size());
221 unsigned char c=in[i++];
231 else if(c<=0xDF && i<in.size())
236 code=(c & 0x1F) << 6;
240 else if(c<=0xEF && i+1<in.size())
242 code=(c & 0xF) << 12;
244 code+=(c & 0x3F) << 6;
248 else if(c<=0xF7 && i+2<in.size())
250 code=(c & 0x7) << 18;
252 code+=(c & 0x3F) << 12;
254 code+=(c & 0x3F) << 6;
293 std::ostringstream result;
295 for(
const auto c : in)
297 if(c<=255 && isprint(c,
loc))
298 result << (
unsigned char)c;
std::string narrow(const wchar_t *s)
std::wstring widen(const char *s)
const char ** narrow_argv(int argc, const wchar_t **argv_wide)
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::string utf16_little_endian_to_ascii(const std::wstring &in)
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)
void utf16_append_code(unsigned int code, bool swap_bytes, std::wstring &result)
std::wstring utf8_to_utf16_big_endian(const std::string &in)