MCPcopy Create free account
hub / github.com/EmbeddedRPC/erpc / WideStringToUtf8

Function WideStringToUtf8

test/common/gtest/gtest.cpp:3295–3316  ·  view source on GitHub ↗

Converts a wide string to a narrow string in UTF-8 encoding. The wide string is assumed to have the following encoding: UTF-16 if sizeof(wchar_t) == 2 (on Windows, Cygwin, Symbian OS) UTF-32 if sizeof(wchar_t) == 4 (on Linux) Parameter str points to a null-terminated wide string. Parameter num_chars may additionally limit the number of wchar_t characters processed. -1 is used when the entire strin

Source from the content-addressed store, hash-verified

3293// and contains invalid UTF-16 surrogate pairs, values in those pairs
3294// will be encoded as individual Unicode characters from Basic Normal Plane.
3295std::string WideStringToUtf8(const wchar_t* str, int num_chars) {
3296 if (num_chars == -1)
3297 num_chars = static_cast<int>(wcslen(str));
3298
3299 ::std::stringstream stream;
3300 for (int i = 0; i < num_chars; ++i) {
3301 UInt32 unicode_code_point;
3302
3303 if (str[i] == L'\0') {
3304 break;
3305 } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {
3306 unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i],
3307 str[i + 1]);
3308 i++;
3309 } else {
3310 unicode_code_point = static_cast<UInt32>(str[i]);
3311 }
3312
3313 stream << CodePointToUtf8(unicode_code_point);
3314 }
3315 return StringStreamToString(&stream);
3316}
3317
3318// Converts a wide C string to an std::string using the UTF-8 encoding.
3319// NULL will be converted to "(null)".

Callers 2

StreamWideCharsToMessageFunction · 0.85
ShowWideCStringMethod · 0.85

Calls 4

IsUtf16SurrogatePairFunction · 0.85
CodePointToUtf8Function · 0.85
StringStreamToStringFunction · 0.85

Tested by

no test coverage detected