diff options
Diffstat (limited to 'encode_escape.go')
-rw-r--r-- | encode_escape.go | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/encode_escape.go b/encode_escape.go index 0054e72..97da6e9 100644 --- a/encode_escape.go +++ b/encode_escape.go @@ -5,6 +5,7 @@ package lowmemjson import ( + "fmt" "unicode/utf8" "git.lukeshu.com/go/lowmemjson/internal/jsonstring" @@ -27,6 +28,27 @@ const ( BackslashEscapeUnicode = jsonstring.BackslashEscapeUnicode ) +func hexToInt(c byte) rune { + switch { + case '0' <= c && c <= '9': + return rune(c) - '0' + case 'a' <= c && c <= 'f': + return rune(c) - 'a' + 10 + case 'A' <= c && c <= 'F': + return rune(c) - 'A' + 10 + default: + panic(fmt.Errorf("should not happen: invalid hex char: %q", c)) + } +} + +func hexToRune(a, b, c, d byte) rune { + return 0 | + hexToInt(a)<<12 | + hexToInt(b)<<8 | + hexToInt(c)<<4 | + hexToInt(d)<<0 +} + // A BackslashEscaper controls how a ReEncoder emits a character in a // JSON string. The `rune` argument is the character being // considered, and the `BackslashEscapeMode` argument is how it was |