MCPcopy Create free account
hub / github.com/buger/jsonparser / decodeUnicodeEscape

Function decodeUnicodeEscape

escape.go:123–163  ·  view source on GitHub ↗

SYS-REQ-115

(in []byte)

Source from the content-addressed store, hash-verified

121
122// SYS-REQ-115
123func decodeUnicodeEscape(in []byte) (rune, int) {
124 if r, ok := decodeSingleUnicodeEscape(in); !ok {
125 // Invalid Unicode escape
126 return utf8.RuneError, -1
127 } else if !isUTF16EncodedRune(r) {
128 // Valid Unicode escape in Basic Multilingual Plane.
129 // Note: a single \uXXXX escape produces r in [0, 0xFFFF], so r is always
130 // within the BMP. The former r <= basicMultilingualPlaneOffset guard was
131 // tautological and has been removed — the real discriminator is whether r
132 // falls in the UTF-16 surrogate range.
133 return r, 6
134 } else if r >= lowSurrogateOffset {
135 // Lone low surrogate (0xDC00-0xDFFF) with no preceding high surrogate.
136 // Per RFC 8259/WHATWG a lone surrogate in a JSON string is malformed;
137 // match encoding/json by substituting U+FFFD and consuming only the 6
138 // bytes of this escape.
139 return utf8.RuneError, 6
140 } else if len(in) < 8 || in[6] != '\\' || in[7] != 'u' {
141 // Lone high surrogate (0xD800-0xDBFF): the high-surrogate escape is not
142 // followed by a "\u" low-surrogate escape. decodeSingleUnicodeEscape
143 // assumes the \u prefix and reads hex at fixed offsets, so without this
144 // guard it would misread whatever bytes follow (e.g. the literal "A7FA"
145 // after "\uDB29") as a low surrogate and synthesize a bogus code point
146 // (DEFECT-260727-SNGT). Substitute U+FFFD and consume only the 6 bytes
147 // of the high surrogate, matching encoding/json.
148 return utf8.RuneError, 6
149 } else if r2, ok := decodeSingleUnicodeEscape(in[6:]); !ok {
150 // A "\u" follows the high surrogate but the low-surrogate escape is
151 // itself malformed (truncated / bad hex) — the whole escape is broken.
152 return utf8.RuneError, -1
153 } else if r2 < lowSurrogateOffset || r2 > basicMultilingualPlaneReservedOffset {
154 // The following "\uXXXX" is not a valid low surrogate (0xDC00-0xDFFF):
155 // e.g. a BMP codepoint or another high surrogate. Treat the first escape
156 // as a lone high surrogate → U+FFFD, consuming 6 bytes; the following
157 // escape is reprocessed by the caller.
158 return utf8.RuneError, 6
159 } else {
160 // Valid UTF16 surrogate pair
161 return combineUTF16Surrogates(r, r2), 12
162 }
163}
164
165// backslashCharEscapeTable: when '\X' is found for some byte X, it is to be replaced with backslashCharEscapeTable[X]
166var backslashCharEscapeTable = [...]byte{

Calls 3

isUTF16EncodedRuneFunction · 0.85
combineUTF16SurrogatesFunction · 0.85