summaryrefslogtreecommitdiff
path: root/reencode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-17 18:39:57 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-17 18:39:57 -0600
commitd72a5e1bf160b4bfa90192ae2da674b9c58ec6b8 (patch)
treef65f9e704f90702ecb19c9d9e215d18017cbf5eb /reencode.go
parent8fe464f2563ac35f7894041783debe0dfe82d528 (diff)
reencode: Encapsulate the handleRune state in a struct
Diffstat (limited to 'reencode.go')
-rw-r--r--reencode.go74
1 files changed, 38 insertions, 36 deletions
diff --git a/reencode.go b/reencode.go
index ead7572..8424168 100644
--- a/reencode.go
+++ b/reencode.go
@@ -56,12 +56,14 @@ type ReEncoder struct {
inputPos int64
// state: .handleRune
- lastNonSpace RuneType
- wasNumber bool
- curIndent int
- uhex [4]byte // "\uABCD"-encoded characters in strings
- fracZeros int64
- expZero bool
+ handleRuneState struct {
+ lastNonSpace RuneType
+ wasNumber bool
+ curIndent int
+ uhex [4]byte // "\uABCD"-encoded characters in strings
+ fracZeros int64
+ expZero bool
+ }
}
// public API //////////////////////////////////////////////////////////////////
@@ -177,9 +179,9 @@ func (enc *ReEncoder) handleRune(c rune, t RuneType) error {
// itself is handled.
func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
// emit newlines between top-level values
- if enc.lastNonSpace == RuneTypeEOF {
+ if enc.handleRuneState.lastNonSpace == RuneTypeEOF {
switch {
- case enc.wasNumber && t.IsNumber():
+ case enc.handleRuneState.wasNumber && t.IsNumber():
if err := enc.emitByte('\n'); err != nil {
return err, false
}
@@ -193,35 +195,35 @@ func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
// shorten numbers
switch t { // trim trailing '0's from the fraction-part, but don't remove all digits
case RuneTypeNumberFracDot:
- enc.fracZeros = 0
+ enc.handleRuneState.fracZeros = 0
case RuneTypeNumberFracDig:
- if c == '0' && enc.lastNonSpace == RuneTypeNumberFracDig {
- enc.fracZeros++
+ if c == '0' && enc.handleRuneState.lastNonSpace == RuneTypeNumberFracDig {
+ enc.handleRuneState.fracZeros++
return nil, false
}
fallthrough
default:
- for enc.fracZeros > 0 {
+ for enc.handleRuneState.fracZeros > 0 {
if err := enc.emitByte('0'); err != nil {
return err, false
}
- enc.fracZeros--
+ enc.handleRuneState.fracZeros--
}
}
switch t { // trim leading '0's from the exponent-part, but don't remove all digits
case RuneTypeNumberExpE, RuneTypeNumberExpSign:
- enc.expZero = true
+ enc.handleRuneState.expZero = true
case RuneTypeNumberExpDig:
- if c == '0' && enc.expZero {
+ if c == '0' && enc.handleRuneState.expZero {
return nil, false
}
- enc.expZero = false
+ enc.handleRuneState.expZero = false
default:
- if enc.expZero {
+ if enc.handleRuneState.expZero {
if err := enc.emitByte('0'); err != nil {
return err, false
}
- enc.expZero = false
+ enc.handleRuneState.expZero = false
}
}
@@ -237,10 +239,10 @@ func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
// let us manage whitespace
return nil, false
case RuneTypeObjectBeg, RuneTypeArrayBeg:
- enc.curIndent++
+ enc.handleRuneState.curIndent++
case RuneTypeObjectEnd, RuneTypeArrayEnd:
- enc.curIndent--
- switch enc.lastNonSpace {
+ enc.handleRuneState.curIndent--
+ switch enc.handleRuneState.lastNonSpace {
case RuneTypeObjectBeg, RuneTypeArrayBeg:
// collapse
default:
@@ -249,14 +251,14 @@ func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
}
}
default:
- switch enc.lastNonSpace {
+ switch enc.handleRuneState.lastNonSpace {
case RuneTypeObjectBeg, RuneTypeObjectComma, RuneTypeArrayBeg, RuneTypeArrayComma:
if err := enc.emitNlIndent(); err != nil {
return err, false
}
}
}
- if enc.lastNonSpace == RuneTypeObjectColon {
+ if enc.handleRuneState.lastNonSpace == RuneTypeObjectColon {
if err := enc.emitByte(' '); err != nil {
return err, false
}
@@ -270,7 +272,7 @@ func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
func (enc *ReEncoder) handleRuneMain(c rune, t RuneType) error {
defer func() {
if t != RuneTypeSpace {
- enc.lastNonSpace = t
+ enc.handleRuneState.lastNonSpace = t
}
}()
@@ -302,33 +304,33 @@ func (enc *ReEncoder) handleRuneMain(c rune, t RuneType) error {
panic("should not happen")
}
case RuneTypeStringEscUA:
- enc.uhex[0], _ = hex2int(c)
+ enc.handleRuneState.uhex[0], _ = hex2int(c)
return nil
case RuneTypeStringEscUB:
- enc.uhex[1], _ = hex2int(c)
+ enc.handleRuneState.uhex[1], _ = hex2int(c)
return nil
case RuneTypeStringEscUC:
- enc.uhex[2], _ = hex2int(c)
+ enc.handleRuneState.uhex[2], _ = hex2int(c)
return nil
case RuneTypeStringEscUD:
- enc.uhex[3], _ = hex2int(c)
+ enc.handleRuneState.uhex[3], _ = hex2int(c)
c := 0 |
- rune(enc.uhex[0])<<12 |
- rune(enc.uhex[1])<<8 |
- rune(enc.uhex[2])<<4 |
- rune(enc.uhex[3])<<0
+ rune(enc.handleRuneState.uhex[0])<<12 |
+ rune(enc.handleRuneState.uhex[1])<<8 |
+ rune(enc.handleRuneState.uhex[2])<<4 |
+ rune(enc.handleRuneState.uhex[3])<<0
return enc.emit(writeStringChar(enc.Out, c, BackslashEscapeUnicode, enc.BackslashEscape))
case RuneTypeError: // EOF explicitly stated by .Close()
fallthrough
case RuneTypeEOF: // EOF implied by the start of the next top-level value
- enc.wasNumber = enc.lastNonSpace.IsNumber()
+ enc.handleRuneState.wasNumber = enc.handleRuneState.lastNonSpace.IsNumber()
switch {
case enc.ForceTrailingNewlines:
- t = RuneTypeError // enc.lastNonSpace : an NL isn't needed (we already printed one)
+ t = RuneTypeError // enc.handleRuneState.lastNonSpace : an NL isn't needed (we already printed one)
return enc.emitByte('\n')
default:
- t = RuneTypeEOF // enc.lastNonSpace : an NL *might* be needed
+ t = RuneTypeEOF // enc.handleRuneState.lastNonSpace : an NL *might* be needed
return nil
}
default:
@@ -358,7 +360,7 @@ func (enc *ReEncoder) emitNlIndent() error {
return err
}
}
- for i := 0; i < enc.curIndent; i++ {
+ for i := 0; i < enc.handleRuneState.curIndent; i++ {
if err := enc.emit(io.WriteString(enc.Out, enc.Indent)); err != nil {
return err
}