16 return ((code) < 0x110000 && (((code) & 0xFFFFF800) != 0xD800));
23 char buf[FCITX_UTF8_MAX_LENGTH + 1];
25 return {buf, buf +
length};
30 std::string UTF16ToUTF8(std::wstring_view data) {
32 for (
size_t i = 0; i < data.size();) {
34 uint16_t chr = data[i];
35 uint16_t chrNext = (i + 1 == data.size()) ? 0 : data[i + 1];
36 if (chr < 0xD800 || chr > 0xDFFF) {
39 }
else if (0xD800 <= chr && chr <= 0xDBFF) {
43 if (0xDC00 <= chrNext && chrNext <= 0xDFFF) {
45 ucs4 = (((chr & 0x3FF) << 10) | (chrNext & 0x3FF)) + (1 << 16);
48 }
else if (0xDC00 <= chr && chr <= 0xDFFF) {
56 std::wstring UTF8ToUTF16(std::string_view str) {
61 for (
const auto ucs4 : utf8::MakeUTF8CharRange(str)) {
63 result.push_back(static_cast<uint16_t>(ucs4));
64 }
else if (ucs4 < 0x110000) {
65 result.push_back(0xD800 | (((ucs4 - 0x10000) >> 10) & 0x3ff));
66 result.push_back(0xDC00 | (ucs4 & 0x3ff));
std::string UCS4ToUTF8(uint32_t code)
Convert UCS4 to UTF8 string.
size_t length(Iter start, Iter end)
Return the number UTF-8 characters in the string iterator range.
bool validate(Iter start, Iter end)
Check if the string iterator range is valid utf8 string.
C++ Utility functions for handling utf8 strings.
C-style utf8 utility functions.
bool UCS4IsValid(uint32_t code)
Check if a ucs4 is valid.
int fcitx_ucs4_to_utf8(uint32_t c, char *output)
Convert ucs4 char to utf8, need to have enough memory for it.