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
| 1498 | // and contains invalid UTF-16 surrogate pairs, values in those pairs |
| 1499 | // will be encoded as individual Unicode characters from Basic Normal Plane. |
| 1500 | std::string WideStringToUtf8(const wchar_t* str, int num_chars) { |
| 1501 | if (num_chars == -1) |
| 1502 | num_chars = static_cast<int>(wcslen(str)); |
| 1503 | |
| 1504 | ::std::stringstream stream; |
| 1505 | for (int i = 0; i < num_chars; ++i) { |
| 1506 | UInt32 unicode_code_point; |
| 1507 | |
| 1508 | if (str[i] == L'\0') { |
| 1509 | break; |
| 1510 | } else if (i + 1 < num_chars && IsUtf16SurrogatePair(str[i], str[i + 1])) { |
| 1511 | unicode_code_point = CreateCodePointFromUtf16SurrogatePair(str[i], |
| 1512 | str[i + 1]); |
| 1513 | i++; |
| 1514 | } else { |
| 1515 | unicode_code_point = static_cast<UInt32>(str[i]); |
| 1516 | } |
| 1517 | |
| 1518 | stream << CodePointToUtf8(unicode_code_point); |
| 1519 | } |
| 1520 | return StringStreamToString(&stream); |
| 1521 | } |
| 1522 | |
| 1523 | // Converts a wide C string to an std::string using the UTF-8 encoding. |
| 1524 | // NULL will be converted to "(null)". |
no test coverage detected