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
| 2805 | // and contains invalid UTF-16 surrogate pairs, values in those pairs |
| 2806 | // will be encoded as individual Unicode characters from Basic Normal Plane. |
| 2807 | String 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)". |
no test coverage detected