| 2033 | } |
| 2034 | |
| 2035 | Error String::append_utf16(const char16_t *p_utf16, int p_len, bool p_default_little_endian) { |
| 2036 | if (!p_utf16) { |
| 2037 | return ERR_INVALID_DATA; |
| 2038 | } |
| 2039 | |
| 2040 | String aux; |
| 2041 | |
| 2042 | int cstr_size = 0; |
| 2043 | int str_size = 0; |
| 2044 | |
| 2045 | #ifdef BIG_ENDIAN_ENABLED |
| 2046 | bool byteswap = p_default_little_endian; |
| 2047 | #else |
| 2048 | bool byteswap = !p_default_little_endian; |
| 2049 | #endif |
| 2050 | /* HANDLE BOM (Byte Order Mark) */ |
| 2051 | if (p_len < 0 || p_len >= 1) { |
| 2052 | bool has_bom = false; |
| 2053 | if (uint16_t(p_utf16[0]) == 0xfeff) { // correct BOM, read as is |
| 2054 | has_bom = true; |
| 2055 | byteswap = false; |
| 2056 | } else if (uint16_t(p_utf16[0]) == 0xfffe) { // backwards BOM, swap bytes |
| 2057 | has_bom = true; |
| 2058 | byteswap = true; |
| 2059 | } |
| 2060 | if (has_bom) { |
| 2061 | if (p_len >= 0) { |
| 2062 | p_len -= 1; |
| 2063 | } |
| 2064 | p_utf16 += 1; |
| 2065 | } |
| 2066 | } |
| 2067 | |
| 2068 | bool decode_error = false; |
| 2069 | { |
| 2070 | const char16_t *ptrtmp = p_utf16; |
| 2071 | const char16_t *ptrtmp_limit = p_len >= 0 ? &p_utf16[p_len] : nullptr; |
| 2072 | uint32_t c_prev = 0; |
| 2073 | bool skip = false; |
| 2074 | while (ptrtmp != ptrtmp_limit && *ptrtmp) { |
| 2075 | uint32_t c = (byteswap) ? BSWAP16(*ptrtmp) : *ptrtmp; |
| 2076 | |
| 2077 | if ((c & 0xfffffc00) == 0xd800) { // lead surrogate |
| 2078 | if (skip) { |
| 2079 | print_unicode_error(vformat("Unpaired lead surrogate (%x [trail?] %x)", c_prev, c)); |
| 2080 | decode_error = true; |
| 2081 | } |
| 2082 | skip = true; |
| 2083 | } else if ((c & 0xfffffc00) == 0xdc00) { // trail surrogate |
| 2084 | if (skip) { |
| 2085 | str_size--; |
| 2086 | } else { |
| 2087 | print_unicode_error(vformat("Unpaired trail surrogate (%x [lead?] %x)", c_prev, c)); |
| 2088 | decode_error = true; |
| 2089 | } |
| 2090 | skip = false; |
| 2091 | } else { |
| 2092 | skip = false; |
no test coverage detected