diff options
-rw-r--r-- | reencode.go | 42 |
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 { |