MCPcopy Create free account
hub / github.com/OpenPTrack/open_ptrack_v2 / WideStringToUtf8

Function WideStringToUtf8

rtpose_wrapper/src/gtest/gtest-all.cpp:2807–2829  ·  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

2805// and contains invalid UTF-16 surrogate pairs, values in those pairs
2806// will be encoded as individual Unicode characters from Basic Normal Plane.
2807String WideStringToUtf8(const wchar_t* str, int num_chars) {
2808 if (num_chars == -1)
2809 num_chars = static_cast<int>(wcslen(str));
2810
2811 ::std::stringstream stream;
2812 for (int i = 0; i < num_chars; ++i) {
2813 UInt32 unicode_code_point;
2814
2815 if (str[i] == L'\0') {
2816 break;
2817 } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {
2818 unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i],
2819 str[i + 1]);
2820 i++;
2821 } else {
2822 unicode_code_point = static_cast<UInt32>(str[i]);
2823 }
2824
2825 char buffer[32]; // CodePointToUtf8 requires a buffer this big.
2826 stream << CodePointToUtf8(unicode_code_point, buffer);
2827 }
2828 return StringStreamToString(&stream);
2829}
2830
2831// Converts a wide C string to a String using the UTF-8 encoding.
2832// 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