MCPcopy Create free account
hub / github.com/apache/singa / WideStringToUtf8

Function WideStringToUtf8

test/gtest/gtest-all.cc:2984–3005  ·  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

2982// and contains invalid UTF-16 surrogate pairs, values in those pairs
2983// will be encoded as individual Unicode characters from Basic Normal Plane.
2984std::string WideStringToUtf8(const wchar_t* str, int num_chars) {
2985 if (num_chars == -1)
2986 num_chars = static_cast<int>(wcslen(str));
2987
2988 ::std::stringstream stream;
2989 for (int i = 0; i < num_chars; ++i) {
2990 UInt32 unicode_code_point;
2991
2992 if (str[i] == L'\0') {
2993 break;
2994 } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) {
2995 unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i],
2996 str[i + 1]);
2997 i++;
2998 } else {
2999 unicode_code_point = static_cast<UInt32>(str[i]);
3000 }
3001
3002 stream << CodePointToUtf8(unicode_code_point);
3003 }
3004 return StringStreamToString(&stream);
3005}
3006
3007// Converts a wide C string to an std::string using the UTF-8 encoding.
3008// 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