summaryrefslogtreecommitdiff
path: root/internal
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 /internal
parent1e2d058c78969118b099940afdb100a3b93325cc (diff)
Clean up the hex handling
Diffstat (limited to 'internal')
-rw-r--r--internal/jsonparse/hex.go20
-rw-r--r--internal/jsonparse/parse.go28
-rw-r--r--internal/jsonstring/encode_string.go10
3 files changed, 20 insertions, 38 deletions
diff --git a/internal/jsonparse/hex.go b/internal/jsonparse/hex.go
deleted file mode 100644
index 3ed5f01..0000000
--- a/internal/jsonparse/hex.go
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright (C) 2022-2023 Luke Shumaker <lukeshu@lukeshu.com>
-//
-// SPDX-License-Identifier: GPL-2.0-or-later
-
-package jsonparse
-
-const Hex = "0123456789abcdef"
-
-func HexToInt(c rune) (byte, bool) {
- switch {
- case '0' <= c && c <= '9':
- return byte(c) - '0', true
- case 'a' <= c && c <= 'f':
- return byte(c) - 'a' + 10, true
- case 'A' <= c && c <= 'F':
- return byte(c) - 'A' + 10, true
- default:
- return 0, false
- }
-}
diff --git a/internal/jsonparse/parse.go b/internal/jsonparse/parse.go
index 73584d9..2f5c1ab 100644
--- a/internal/jsonparse/parse.go
+++ b/internal/jsonparse/parse.go
@@ -14,6 +14,12 @@ import (
var ErrParserExceededMaxDepth = errors.New("exceeded max depth")
+func isHex(c rune) bool {
+ return ('0' <= c && c <= '9') ||
+ ('a' <= c && c <= 'f') ||
+ ('A' <= c && c <= 'F')
+}
+
// RuneType is the classification of a rune when parsing JSON input.
// A Parser, rather than grouping runes into tokens and classifying
// tokens, classifies runes directly.
@@ -667,30 +673,26 @@ func (par *Parser) HandleRune(c rune) (RuneType, error) {
return RuneTypeError, fmt.Errorf("string backslash sequence: unexpected character: %q", c)
}
case RuneTypeStringEscU:
- if _, ok := HexToInt(c); ok {
- return par.replaceState(RuneTypeStringEscUA), nil
- } else {
+ if !isHex(c) {
return RuneTypeError, fmt.Errorf("string unicode sequence: unexpected character: %q", c)
}
+ return par.replaceState(RuneTypeStringEscUA), nil
case RuneTypeStringEscUA:
- if _, ok := HexToInt(c); ok {
- return par.replaceState(RuneTypeStringEscUB), nil
- } else {
+ if !isHex(c) {
return RuneTypeError, fmt.Errorf("string unicode sequence: unexpected character: %q", c)
}
+ return par.replaceState(RuneTypeStringEscUB), nil
case RuneTypeStringEscUB:
- if _, ok := HexToInt(c); ok {
- return par.replaceState(RuneTypeStringEscUC), nil
- } else {
+ if !isHex(c) {
return RuneTypeError, fmt.Errorf("string unicode sequence: unexpected character: %q", c)
}
+ return par.replaceState(RuneTypeStringEscUC), nil
case RuneTypeStringEscUC:
- if _, ok := HexToInt(c); ok {
- par.replaceState(RuneTypeStringBeg)
- return RuneTypeStringEscUD, nil
- } else {
+ if !isHex(c) {
return RuneTypeError, fmt.Errorf("string unicode sequence: unexpected character: %q", c)
}
+ par.replaceState(RuneTypeStringBeg)
+ return RuneTypeStringEscUD, nil
// number //////////////////////////////////////////////////////////////////////////////////
//
// Here's a flattened drawing of the syntax diagram from www.json.org :
diff --git a/internal/jsonstring/encode_string.go b/internal/jsonstring/encode_string.go
index f29dc3f..a7670c6 100644
--- a/internal/jsonstring/encode_string.go
+++ b/internal/jsonstring/encode_string.go
@@ -10,7 +10,6 @@ import (
"unicode/utf8"
"git.lukeshu.com/go/lowmemjson/internal/fastio"
- "git.lukeshu.com/go/lowmemjson/internal/jsonparse"
)
// BackslashEscapeMode is describe in the main lowmemjson package
@@ -27,13 +26,14 @@ const (
type BackslashEscaper = func(rune, BackslashEscapeMode) BackslashEscapeMode
func writeStringUnicodeEscape(w io.Writer, c rune) (int, error) {
+ const alphabet = "0123456789abcdef"
buf := [6]byte{
'\\',
'u',
- jsonparse.Hex[(c>>12)&0xf],
- jsonparse.Hex[(c>>8)&0xf],
- jsonparse.Hex[(c>>4)&0xf],
- jsonparse.Hex[(c>>0)&0xf],
+ alphabet[(c>>12)&0xf],
+ alphabet[(c>>8)&0xf],
+ alphabet[(c>>4)&0xf],
+ alphabet[(c>>0)&0xf],
}
return w.Write(buf[:])
}