summaryrefslogtreecommitdiff
path: root/reencode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2022-08-14 10:55:02 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-14 17:06:28 -0600
commitc8a79cbfde0e42ac3d677bb986e4dbfc9e5cfa85 (patch)
tree10294f06b9d1143644dbdfb7a2e895acd8504efd /reencode.go
parent076ca46bad3e18ea7f4c3b3320ab410c3ebea747 (diff)
reencode: Rethink the string backslash encoder
Diffstat (limited to 'reencode.go')
-rw-r--r--reencode.go28
1 files changed, 14 insertions, 14 deletions
diff --git a/reencode.go b/reencode.go
index 50c8ba3..66f25da 100644
--- a/reencode.go
+++ b/reencode.go
@@ -24,12 +24,12 @@ type ReEncoder struct {
// encoding/json only.
prefix string
// Returns whether a given character in a string should be
- // "\uXXXX" escaped. The bool argument is whether it was
+ // backslash-escaped. The bool argument is whether it was
// \u-escaped in the input. This does not affect characters
- // that must or must-not be \u-escaped to be valid JSON.
+ // that must or must-not be escaped to be valid JSON.
//
- // If not set, then EscapeUnicodeDefault is used.
- UnicodeEscape func(rune, bool) bool
+ // If not set, then EscapeDefault is used.
+ BackslashEscape func(rune, BackslashEscapeMode) BackslashEscapeMode
bailAfterCurrent bool
@@ -339,7 +339,7 @@ func (enc *ReEncoder) stateInString(c rune) error {
enc.popState()
return enc.emitByte(byte(c))
case 0x0020 <= c && c <= 0x10FFFF:
- return enc.emit(writeStringChar(enc.Out, c, false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, c, BackslashEscapeNone, enc.BackslashEscape))
default:
return &SyntaxError{fmt.Sprintf("string: unexpected character: %c", c), enc.inputPos}
}
@@ -348,28 +348,28 @@ func (enc *ReEncoder) stateInBackslash(c rune) error {
switch c {
case '"':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '"', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '"', BackslashEscapeShort, enc.BackslashEscape))
case '\\':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '\\', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '\\', BackslashEscapeShort, enc.BackslashEscape))
case '/':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '/', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '/', BackslashEscapeShort, enc.BackslashEscape))
case 'b':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '\b', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '\b', BackslashEscapeShort, enc.BackslashEscape))
case 'f':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '\f', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '\f', BackslashEscapeShort, enc.BackslashEscape))
case 'n':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '\n', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '\n', BackslashEscapeShort, enc.BackslashEscape))
case 'r':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '\r', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '\r', BackslashEscapeShort, enc.BackslashEscape))
case 't':
enc.replaceState(enc.stateInString, false)
- return enc.emit(writeStringChar(enc.Out, '\t', false, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, '\t', BackslashEscapeShort, enc.BackslashEscape))
case 'u':
enc.replaceState(enc.stateInUnicode, false)
return nil
@@ -396,7 +396,7 @@ func (enc *ReEncoder) stateInUnicode(c rune) error {
rune(enc.stateBuf[2])<<4 |
rune(enc.stateBuf[3])<<0
enc.stateBuf = enc.stateBuf[:0]
- return enc.emit(writeStringChar(enc.Out, c, true, enc.UnicodeEscape))
+ return enc.emit(writeStringChar(enc.Out, c, BackslashEscapeUnicode, enc.BackslashEscape))
}
return nil
}