From 2e51f6f25cfc37f1e1d6fd3a90f0570979939a21 Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Sat, 11 Mar 2023 12:03:28 -0800 Subject: [PATCH] encoding/json: make use of Buffer.AvailableBuffer Use the explicit API for acquiring an empty available buffer, rather than the hack that's implemented in terms of Bytes and Len. Change-Id: If286ed42693acd61ffe28dc849ed4b76c3ae4434 Reviewed-on: https://go-review.googlesource.com/c/go/+/476337 Reviewed-by: Cherry Mui Reviewed-by: Brad Fitzpatrick Auto-Submit: Joseph Tsai Run-TryBot: Joseph Tsai TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- src/encoding/json/encode.go | 8 ++------ src/encoding/json/indent.go | 11 +++-------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/encoding/json/encode.go b/src/encoding/json/encode.go index f3c824d13e..79a82cfe75 100644 --- a/src/encoding/json/encode.go +++ b/src/encoding/json/encode.go @@ -259,10 +259,6 @@ type encodeState struct { ptrSeen map[any]struct{} } -func (e *encodeState) AvailableBuffer() []byte { - return availableBuffer(&e.Buffer) -} - const startDetectingCyclesAfter = 1000 var encodeStatePool sync.Pool @@ -445,7 +441,7 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { b, err := m.MarshalJSON() if err == nil { e.Grow(len(b)) - out := availableBuffer(&e.Buffer) + out := e.AvailableBuffer() out, err = appendCompact(out, b, opts.escapeHTML) e.Buffer.Write(out) } @@ -464,7 +460,7 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) { b, err := m.MarshalJSON() if err == nil { e.Grow(len(b)) - out := availableBuffer(&e.Buffer) + out := e.AvailableBuffer() out, err = appendCompact(out, b, opts.escapeHTML) e.Buffer.Write(out) } diff --git a/src/encoding/json/indent.go b/src/encoding/json/indent.go index 99951208a0..26bb5d2e47 100644 --- a/src/encoding/json/indent.go +++ b/src/encoding/json/indent.go @@ -6,11 +6,6 @@ package json import "bytes" -// TODO(https://go.dev/issue/53685): Use bytes.Buffer.AvailableBuffer instead. -func availableBuffer(b *bytes.Buffer) []byte { - return b.Bytes()[b.Len():] -} - // HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029 // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029 // so that the JSON will be safe to embed inside HTML