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)
| 38 | // In JSON, these escapes can either come alone or as part of "UTF16 surrogate pairs" that must be handled together. |
| 39 | // This function only handles one; decodeUnicodeEscape handles this more complex case. |
| 40 | func decodeSingleUnicodeEscape(in []byte) (rune, bool) { |
| 41 | // We need at least 6 characters total |
| 42 | if len(in) < 6 { |
| 43 | return utf8.RuneError, false |
| 44 | } |
| 45 | |
| 46 | // Convert hex to decimal |
| 47 | h1, h2, h3, h4 := h2I(in[2]), h2I(in[3]), h2I(in[4]), h2I(in[5]) |
| 48 | if h1 == badHex || h2 == badHex || h3 == badHex || h4 == badHex { |
| 49 | return utf8.RuneError, false |
| 50 | } |
| 51 | |
| 52 | // Compose the hex digits |
| 53 | return rune(h1<<12 + h2<<8 + h3<<4 + h4), true |
| 54 | } |
| 55 | |
| 56 | // isUTF16EncodedRune checks if a rune is in the range for non-BMP characters, |
| 57 | // which is used to describe UTF16 chars. |
searching dependent graphs…