summaryrefslogtreecommitdiff
path: root/reencode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2023-01-28 22:43:32 -0700
committerLuke Shumaker <lukeshu@lukeshu.com>2023-01-30 22:00:25 -0700
commit659114fee7c39f06c408135169f2848a881dfe5e (patch)
tree24ac40d1d84a3765aa49de316938a32244e14d34 /reencode.go
parent1a79116fd80a2c290ffd498686ff3b6d4dd3745b (diff)
reencode: I see handleRuneMain's defer handling showing up in the CPU profile
Diffstat (limited to 'reencode.go')
-rw-r--r--reencode.go42
1 files changed, 19 insertions, 23 deletions
diff --git a/reencode.go b/reencode.go
index 49d8ddb..a5dc3c8 100644
--- a/reencode.go
+++ b/reencode.go
@@ -455,48 +455,40 @@ func (enc *ReEncoder) handleRunePre(c rune, t internal.RuneType) (error, bool) {
// handleRuneMain handles the new rune itself, not buffered things.
func (enc *ReEncoder) handleRuneMain(c rune, t internal.RuneType) error {
- defer func() {
- if t != internal.RuneTypeSpace {
- enc.lastNonSpace = t
- }
- }()
-
+ var err error
switch t {
case internal.RuneTypeStringChar:
- return enc.emit(writeStringChar(enc.out, c, BackslashEscapeNone, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, c, BackslashEscapeNone, enc.BackslashEscape))
case internal.RuneTypeStringEsc, internal.RuneTypeStringEscU:
- return nil
+ // do nothing
case internal.RuneTypeStringEsc1:
switch c {
case '"':
- return enc.emit(writeStringChar(enc.out, '"', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '"', BackslashEscapeShort, enc.BackslashEscape))
case '\\':
- return enc.emit(writeStringChar(enc.out, '\\', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '\\', BackslashEscapeShort, enc.BackslashEscape))
case '/':
- return enc.emit(writeStringChar(enc.out, '/', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '/', BackslashEscapeShort, enc.BackslashEscape))
case 'b':
- return enc.emit(writeStringChar(enc.out, '\b', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '\b', BackslashEscapeShort, enc.BackslashEscape))
case 'f':
- return enc.emit(writeStringChar(enc.out, '\f', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '\f', BackslashEscapeShort, enc.BackslashEscape))
case 'n':
- return enc.emit(writeStringChar(enc.out, '\n', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '\n', BackslashEscapeShort, enc.BackslashEscape))
case 'r':
- return enc.emit(writeStringChar(enc.out, '\r', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '\r', BackslashEscapeShort, enc.BackslashEscape))
case 't':
- return enc.emit(writeStringChar(enc.out, '\t', BackslashEscapeShort, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, '\t', BackslashEscapeShort, enc.BackslashEscape))
default:
panic("should not happen")
}
case internal.RuneTypeStringEscUA:
enc.uhex[0], _ = internal.HexToInt(c)
- return nil
case internal.RuneTypeStringEscUB:
enc.uhex[1], _ = internal.HexToInt(c)
- return nil
case internal.RuneTypeStringEscUC:
enc.uhex[2], _ = internal.HexToInt(c)
- return nil
case internal.RuneTypeStringEscUD:
enc.uhex[3], _ = internal.HexToInt(c)
c := 0 |
@@ -504,7 +496,7 @@ func (enc *ReEncoder) handleRuneMain(c rune, t internal.RuneType) error {
rune(enc.uhex[1])<<8 |
rune(enc.uhex[2])<<4 |
rune(enc.uhex[3])<<0
- return enc.emit(writeStringChar(enc.out, c, BackslashEscapeUnicode, enc.BackslashEscape))
+ err = enc.emit(writeStringChar(enc.out, c, BackslashEscapeUnicode, enc.BackslashEscape))
case internal.RuneTypeError: // EOF explicitly stated by .Close()
fallthrough
@@ -513,14 +505,18 @@ func (enc *ReEncoder) handleRuneMain(c rune, t internal.RuneType) error {
switch {
case enc.ForceTrailingNewlines:
t = internal.RuneTypeError // enc.lastNonSpace : an NL isn't needed (we already printed one)
- return enc.emitByte('\n')
+ err = enc.emitByte('\n')
default:
t = internal.RuneTypeEOF // enc.lastNonSpace : an NL *might* be needed
- return nil
}
default:
- return enc.emitByte(byte(c))
+ err = enc.emitByte(byte(c))
}
+
+ if t != internal.RuneTypeSpace {
+ enc.lastNonSpace = t
+ }
+ return err
}
func (enc *ReEncoder) emitByte(c byte) error {