From 689587cd726c91e7b0744b28bc6bbc31144234f0 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 25 Jan 2021 00:06:16 +0900 Subject: [PATCH] Fix indent encoding --- encode.go | 6 ++++-- encode_vm.go | 2 +- encode_vm_escaped_indent.go | 12 ++++++++---- encode_vm_indent.go | 11 +++++++---- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/encode.go b/encode.go index cf4cdaa..0905c44 100644 --- a/encode.go +++ b/encode.go @@ -22,6 +22,7 @@ type Encoder struct { enabledIndent bool enabledHTMLEscape bool unorderedMap bool + baseIndent int prefix []byte indentStr []byte } @@ -156,6 +157,7 @@ func (e *Encoder) release() { } func (e *Encoder) reset() { + e.baseIndent = 0 e.enabledHTMLEscape = true e.enabledIndent = false e.unorderedMap = false @@ -298,7 +300,7 @@ func appendStructEnd(b []byte) []byte { func (e *Encoder) appendStructEndIndent(b []byte, indent int) []byte { b = append(b, '\n') b = append(b, e.prefix...) - b = append(b, bytes.Repeat(e.indentStr, indent)...) + b = append(b, bytes.Repeat(e.indentStr, e.baseIndent+indent)...) return append(b, '}', ',', '\n') } @@ -319,5 +321,5 @@ func encodeByteSlice(b []byte, src []byte) []byte { func (e *Encoder) encodeIndent(b []byte, indent int) []byte { b = append(b, e.prefix...) - return append(b, bytes.Repeat(e.indentStr, indent)...) + return append(b, bytes.Repeat(e.indentStr, e.baseIndent+indent)...) } diff --git a/encode_vm.go b/encode_vm.go index 9ee6aba..3d80ff4 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -237,7 +237,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet) ( ctx.ptrs = newPtrs - bb, err := e.runEscaped(ctx, b, ifaceCodeSet) + bb, err := e.run(ctx, b, ifaceCodeSet) if err != nil { return nil, err } diff --git a/encode_vm_escaped_indent.go b/encode_vm_escaped_indent.go index bc0116c..66eeb92 100644 --- a/encode_vm_escaped_indent.go +++ b/encode_vm_escaped_indent.go @@ -198,10 +198,13 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet ctx.ptrs = newPtrs - bb, err := e.runEscaped(ctx, b, ifaceCodeSet) + oldBaseIndent := e.baseIndent + e.baseIndent = code.indent + bb, err := e.runEscapedIndent(ctx, b, ifaceCodeSet) if err != nil { return nil, err } + e.baseIndent = oldBaseIndent ctx.ptrs = oldPtrs ctxptr = ctx.ptr() @@ -233,7 +236,7 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet if err := encodeWithIndent( &buf, bb, - string(e.prefix)+string(bytes.Repeat(e.indentStr, code.indent)), + string(e.prefix)+string(bytes.Repeat(e.indentStr, e.baseIndent+code.indent)), string(e.indentStr), ); err != nil { return nil, err @@ -543,9 +546,10 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet }) buf := b[pos[0]:] buf = buf[:0] + for _, kv := range kvs { buf = append(buf, e.prefix...) - buf = append(buf, bytes.Repeat(e.indentStr, code.indent+1)...) + buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent+1)...) buf = append(buf, []byte(kv.key)...) buf[len(buf)-2] = ':' @@ -555,7 +559,7 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, codeSet buf = buf[:len(buf)-2] buf = append(buf, '\n') buf = append(buf, e.prefix...) - buf = append(buf, bytes.Repeat(e.indentStr, code.indent)...) + buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent)...) buf = append(buf, '}', ',', '\n') b = b[:pos[0]] b = append(b, buf...) diff --git a/encode_vm_indent.go b/encode_vm_indent.go index 9c2f03a..fdac498 100644 --- a/encode_vm_indent.go +++ b/encode_vm_indent.go @@ -198,10 +198,13 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode ctx.ptrs = newPtrs - bb, err := e.runEscaped(ctx, b, ifaceCodeSet) + oldBaseIndent := e.baseIndent + e.baseIndent = code.indent + bb, err := e.runIndent(ctx, b, ifaceCodeSet) if err != nil { return nil, err } + e.baseIndent = oldBaseIndent ctx.ptrs = oldPtrs ctxptr = ctx.ptr() @@ -233,7 +236,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode if err := encodeWithIndent( &buf, bb, - string(e.prefix)+string(bytes.Repeat(e.indentStr, code.indent)), + string(e.prefix)+string(bytes.Repeat(e.indentStr, e.baseIndent+code.indent)), string(e.indentStr), ); err != nil { return nil, err @@ -545,7 +548,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode buf = buf[:0] for _, kv := range kvs { buf = append(buf, e.prefix...) - buf = append(buf, bytes.Repeat(e.indentStr, code.indent+1)...) + buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent+1)...) buf = append(buf, []byte(kv.key)...) buf[len(buf)-2] = ':' @@ -555,7 +558,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, codeSet *opcode buf = buf[:len(buf)-2] buf = append(buf, '\n') buf = append(buf, e.prefix...) - buf = append(buf, bytes.Repeat(e.indentStr, code.indent)...) + buf = append(buf, bytes.Repeat(e.indentStr, e.baseIndent+code.indent)...) buf = append(buf, '}', ',', '\n') b = b[:pos[0]] b = append(b, buf...)