summaryrefslogtreecommitdiff
path: root/encode_escape.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-02-16 19:06:46 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-02-16 21:24:15 -0700
commit2b7fff828e29b63ae08a871b4b1e74784fab29e5 (patch)
treed95b3a00d4703c4c5eb41dce1529e4d675293ce5 /encode_escape.go
parent1e2d058c78969118b099940afdb100a3b93325cc (diff)
Clean up the hex handling
Diffstat (limited to 'encode_escape.go')
-rw-r--r--encode_escape.go22
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