summaryrefslogtreecommitdiff
path: root/reencode.go
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@datawire.io>2022-08-18 01:07:39 -0600
committerLuke Shumaker <lukeshu@datawire.io>2022-08-18 01:07:39 -0600
commitfb063e2955debb9d02debf3f59de291d99017058 (patch)
treefdd0e8a1ed8b5ee63f7ea2aacb6657249fb63dbb /reencode.go
parent3bd2e0e93647b806a68f06f319ea23511ec24a37 (diff)
Add more reencode tests
Diffstat (limited to 'reencode.go')
-rw-r--r--reencode.go23
1 files changed, 15 insertions, 8 deletions
diff --git a/reencode.go b/reencode.go
index e781c03..e7030f8 100644
--- a/reencode.go
+++ b/reencode.go
@@ -214,19 +214,25 @@ func (enc *ReEncoder) handleRune(c rune, t RuneType) error {
}
}
} else { // speculating
+
+ // conCompress is whether we're 1-up from the leaf;
+ // set this *before* the calls to .handleRune.
+ canCompress := enc.handleRuneState.specu.indentFmt.handleRuneState.specu == nil
+
if err := enc.handleRuneState.specu.compactFmt.handleRune(c, t); err != nil {
return err
}
if err := enc.handleRuneState.specu.indentFmt.handleRune(c, t); err != nil {
return err
}
+
switch {
case enc.handleRuneState.specu.compactBuf.Len() >= enc.CompactIfUnder: // stop speculating; use indent
if _, err := enc.handleRuneState.specu.indentBuf.WriteTo(enc.Out); err != nil {
return err
}
enc.handleRuneState = enc.handleRuneState.specu.indentFmt.handleRuneState
- case t == RuneTypeObjectEnd || t == RuneTypeArrayEnd: // stop speculating; use compact
+ case canCompress && (t == RuneTypeObjectEnd || t == RuneTypeArrayEnd): // stop speculating; use compact
if _, err := enc.handleRuneState.specu.compactBuf.WriteTo(enc.Out); err != nil {
return err
}
@@ -311,10 +317,8 @@ func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
case enc.Indent != "":
switch t {
case RuneTypeSpace:
- // let us manage whitespace
+ // let us manage whitespace, don't pass it through
return nil, false
- case RuneTypeObjectBeg, RuneTypeArrayBeg:
- enc.handleRuneState.curIndent++
case RuneTypeObjectEnd, RuneTypeArrayEnd:
enc.handleRuneState.curIndent--
switch enc.handleRuneState.lastNonSpace {
@@ -331,11 +335,14 @@ func (enc *ReEncoder) handleRunePre(c rune, t RuneType) (error, bool) {
if err := enc.emitNlIndent(); err != nil {
return err, false
}
+ case RuneTypeObjectColon:
+ if err := enc.emitByte(' '); err != nil {
+ return err, false
+ }
}
- }
- if enc.handleRuneState.lastNonSpace == RuneTypeObjectColon {
- if err := enc.emitByte(' '); err != nil {
- return err, false
+ switch t {
+ case RuneTypeObjectBeg, RuneTypeArrayBeg:
+ enc.handleRuneState.curIndent++
}
}
}