Converts a Unicode code point to a narrow string in UTF-8 encoding. code_point parameter is of type UInt32 because wchar_t may not be wide enough to contain a code point. If the code_point is not a valid Unicode code point (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted to "(Invalid Unicode 0xXXXXXXXX)".
| 2918 | // (i.e. outside of Unicode range U+0 to U+10FFFF) it will be converted |
| 2919 | // to "(Invalid Unicode 0xXXXXXXXX)". |
| 2920 | std::string CodePointToUtf8(UInt32 code_point) { |
| 2921 | if (code_point > kMaxCodePoint4) { |
| 2922 | return "(Invalid Unicode 0x" + String::FormatHexInt(code_point) + ")"; |
| 2923 | } |
| 2924 | |
| 2925 | char str[5]; // Big enough for the largest valid code point. |
| 2926 | if (code_point <= kMaxCodePoint1) { |
| 2927 | str[1] = '\0'; |
| 2928 | str[0] = static_cast<char>(code_point); // 0xxxxxxx |
| 2929 | } else if (code_point <= kMaxCodePoint2) { |
| 2930 | str[2] = '\0'; |
| 2931 | str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx |
| 2932 | str[0] = static_cast<char>(0xC0 | code_point); // 110xxxxx |
| 2933 | } else if (code_point <= kMaxCodePoint3) { |
| 2934 | str[3] = '\0'; |
| 2935 | str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx |
| 2936 | str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx |
| 2937 | str[0] = static_cast<char>(0xE0 | code_point); // 1110xxxx |
| 2938 | } else { // code_point <= kMaxCodePoint4 |
| 2939 | str[4] = '\0'; |
| 2940 | str[3] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx |
| 2941 | str[2] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx |
| 2942 | str[1] = static_cast<char>(0x80 | ChopLowBits(&code_point, 6)); // 10xxxxxx |
| 2943 | str[0] = static_cast<char>(0xF0 | code_point); // 11110xxx |
| 2944 | } |
| 2945 | return str; |
| 2946 | } |
| 2947 | |
| 2948 | // The following two functions only make sense if the the system |
| 2949 | // uses UTF-16 for wide string encoding. All supported systems |
no test coverage detected