MCPcopy Create free account
hub / github.com/crossuo/crossuo / wstr_to_utf8

Function wstr_to_utf8

common/str.h:161–189  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

159}
160
161astr_t wstr_to_utf8(const wstr_t &aWstr)
162{
163#if defined(_MSC_VER)
164 int size = ::WideCharToMultiByte(
165 CP_UTF8, 0, &aWstr[0], (int)aWstr.size(), nullptr, 0, nullptr, nullptr);
166 astr_t result = "";
167
168 if (size > 0)
169 {
170 result.resize(size + 1);
171 ::WideCharToMultiByte(
172 CP_UTF8, 0, &aWstr[0], (int)aWstr.size(), &result[0], size, nullptr, nullptr);
173 result.resize(size); // result[size] = 0;
174 }
175#else
176 mbstate_t state{};
177 astr_t result{};
178 auto p = aWstr.data();
179 const auto size = wcsrtombs(nullptr, &p, 0, &state);
180 if (size > 0)
181 {
182 result.resize(size + 1);
183 wcsrtombs(&result[0], &p, size, &state);
184 result.resize(size);
185 }
186#endif
187
188 return result;
189}
190
191wstr_t wstr_from_utf8(const astr_t &aStr)
192{

Callers 4

CPacketBookPageDataMethod · 0.85

Calls 3

resizeMethod · 0.80
sizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected