1
0
mirror of https://github.com/golang/go synced 2024-11-23 20:40:07 -07:00

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 <cherryyz@google.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Auto-Submit: Joseph Tsai <joetsai@digital-static.net>
Run-TryBot: Joseph Tsai <joetsai@digital-static.net>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
This commit is contained in:
Joe Tsai 2023-03-11 12:03:28 -08:00 committed by Gopher Robot
parent e64e000f55
commit 2e51f6f25c
2 changed files with 5 additions and 14 deletions

View File

@ -259,10 +259,6 @@ type encodeState struct {
ptrSeen map[any]struct{} ptrSeen map[any]struct{}
} }
func (e *encodeState) AvailableBuffer() []byte {
return availableBuffer(&e.Buffer)
}
const startDetectingCyclesAfter = 1000 const startDetectingCyclesAfter = 1000
var encodeStatePool sync.Pool var encodeStatePool sync.Pool
@ -445,7 +441,7 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
b, err := m.MarshalJSON() b, err := m.MarshalJSON()
if err == nil { if err == nil {
e.Grow(len(b)) e.Grow(len(b))
out := availableBuffer(&e.Buffer) out := e.AvailableBuffer()
out, err = appendCompact(out, b, opts.escapeHTML) out, err = appendCompact(out, b, opts.escapeHTML)
e.Buffer.Write(out) e.Buffer.Write(out)
} }
@ -464,7 +460,7 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
b, err := m.MarshalJSON() b, err := m.MarshalJSON()
if err == nil { if err == nil {
e.Grow(len(b)) e.Grow(len(b))
out := availableBuffer(&e.Buffer) out := e.AvailableBuffer()
out, err = appendCompact(out, b, opts.escapeHTML) out, err = appendCompact(out, b, opts.escapeHTML)
e.Buffer.Write(out) e.Buffer.Write(out)
} }

View File

@ -6,11 +6,6 @@ package json
import "bytes" 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 // 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 // characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
// so that the JSON will be safe to embed inside HTML <script> tags. // so that the JSON will be safe to embed inside HTML <script> tags.
@ -18,7 +13,7 @@ func availableBuffer(b *bytes.Buffer) []byte {
// escaping within <script> tags, so an alternative JSON encoding must be used. // escaping within <script> tags, so an alternative JSON encoding must be used.
func HTMLEscape(dst *bytes.Buffer, src []byte) { func HTMLEscape(dst *bytes.Buffer, src []byte) {
dst.Grow(len(src)) dst.Grow(len(src))
dst.Write(appendHTMLEscape(availableBuffer(dst), src)) dst.Write(appendHTMLEscape(dst.AvailableBuffer(), src))
} }
func appendHTMLEscape(dst, src []byte) []byte { func appendHTMLEscape(dst, src []byte) []byte {
@ -45,7 +40,7 @@ func appendHTMLEscape(dst, src []byte) []byte {
// insignificant space characters elided. // insignificant space characters elided.
func Compact(dst *bytes.Buffer, src []byte) error { func Compact(dst *bytes.Buffer, src []byte) error {
dst.Grow(len(src)) dst.Grow(len(src))
b := availableBuffer(dst) b := dst.AvailableBuffer()
b, err := appendCompact(b, src, false) b, err := appendCompact(b, src, false)
dst.Write(b) dst.Write(b)
return err return err
@ -114,7 +109,7 @@ const indentGrowthFactor = 2
// if src ends in a trailing newline, so will dst. // if src ends in a trailing newline, so will dst.
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error { func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
dst.Grow(indentGrowthFactor * len(src)) dst.Grow(indentGrowthFactor * len(src))
b := availableBuffer(dst) b := dst.AvailableBuffer()
b, err := appendIndent(b, src, prefix, indent) b, err := appendIndent(b, src, prefix, indent)
dst.Write(b) dst.Write(b)
return err return err