From c8a79cbfde0e42ac3d677bb986e4dbfc9e5cfa85 Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 14 Aug 2022 10:55:02 -0600 Subject: reencode: Rethink the string backslash encoder --- reencode.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'reencode.go') 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 } -- cgit v1.2.3-2-g168b