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

Function decodeSingleUnicodeEscape

escape.go:82–96  ·  view source on GitHub ↗

decodeSingleUnicodeEscape decodes a single \uXXXX escape sequence. The prefix \u is assumed to be present and is not checked. In JSON, these escapes can either come alone or as part of "UTF16 surrogate pairs" that must be handled together. This function only handles one; decodeUnicodeEscape handles

(in []byte)

Source from the content-addressed store, hash-verified

80// In JSON, these escapes can either come alone or as part of "UTF16 surrogate pairs" that must be handled together.
81// This function only handles one; decodeUnicodeEscape handles this more complex case.
82func decodeSingleUnicodeEscape(in []byte) (rune, bool) {
83 // We need at least 6 characters total
84 if len(in) < 6 {
85 return utf8.RuneError, false
86 }
87
88 // Convert hex to decimal
89 h1, h2, h3, h4 := h2I(in[2]), h2I(in[3]), h2I(in[4]), h2I(in[5])
90 if h1 == badHex || h2 == badHex || h3 == badHex || h4 == badHex {
91 return utf8.RuneError, false
92 }
93
94 // Compose the hex digits
95 return rune(h1<<12 + h2<<8 + h3<<4 + h4), true
96}
97
98// isUTF16EncodedRune checks if a rune is in the range for non-BMP characters,
99// which is used to describe UTF16 chars.

Calls 1

h2IFunction · 0.85