summaryrefslogtreecommitdiff
path: root/encode_escape.go
diff options
context:
space:
mode:
Diffstat (limited to 'encode_escape.go')
-rw-r--r--encode_escape.go32
1 files changed, 28 insertions, 4 deletions
diff --git a/encode_escape.go b/encode_escape.go
index ab0d9c1..97da6e9 100644
--- a/encode_escape.go
+++ b/encode_escape.go
@@ -5,7 +5,10 @@
package lowmemjson
import (
+ "fmt"
"unicode/utf8"
+
+ "git.lukeshu.com/go/lowmemjson/internal/jsonstring"
)
// BackslashEscapeMode identifies one of the three ways that a
@@ -17,14 +20,35 @@ import (
// single-character)
//
// - as a long Unicode `\uXXXX` backslash sequence
-type BackslashEscapeMode uint8
+type BackslashEscapeMode = jsonstring.BackslashEscapeMode
const (
- BackslashEscapeNone BackslashEscapeMode = iota
- BackslashEscapeShort
- BackslashEscapeUnicode
+ BackslashEscapeNone = jsonstring.BackslashEscapeNone
+ BackslashEscapeShort = jsonstring.BackslashEscapeShort
+ 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