MCPcopy Create free account
hub / github.com/Snapchat/Valdi / utf16DecodeCheck

Function utf16DecodeCheck

valdi_core/src/valdi_core/cpp/Text/UTF16Utils.cpp:38–49  ·  view source on GitHub ↗

* Like utf8DecodeCheck, but for UTF-16. */

Source from the content-addressed store, hash-verified

36 * Like utf8DecodeCheck, but for UTF-16.
37 */
38static inline OffsetPt utf16DecodeCheck(const char16_t* str, std::u16string::size_type i) {
39 if (isHighSurrogate(str[i]) && isLowSurrogate(str[i + 1])) {
40 // High surrogate followed by low surrogate
41 char32_t pt = (((str[i] - 0xD800) << 10) | (str[i + 1] - 0xDC00)) + 0x10000;
42 return {2, pt};
43 } else if (isHighSurrogate(str[i]) || isLowSurrogate(str[i])) {
44 // High surrogate *not* followed by low surrogate, or unpaired low surrogate
45 return kInvalidPt;
46 } else {
47 return {1, str[i]};
48 }
49}
50
51static inline char32_t utf16Decode(const char16_t* str, std::u16string::size_type& i) {
52 OffsetPt res = utf16DecodeCheck(str, i);

Callers 1

utf16DecodeFunction · 0.85

Calls 2

isHighSurrogateFunction · 0.85
isLowSurrogateFunction · 0.85

Tested by

no test coverage detected