Skip to content

Commit 2e51f6f

Browse files
dsnetgopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Auto-Submit: Joseph Tsai <[email protected]> Run-TryBot: Joseph Tsai <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent e64e000 commit 2e51f6f

File tree

2 files changed

+5
-14
lines changed

2 files changed

+5
-14
lines changed

src/encoding/json/encode.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,6 @@ type encodeState struct {
259259
ptrSeen map[any]struct{}
260260
}
261261

262-
func (e *encodeState) AvailableBuffer() []byte {
263-
return availableBuffer(&e.Buffer)
264-
}
265-
266262
const startDetectingCyclesAfter = 1000
267263

268264
var encodeStatePool sync.Pool
@@ -445,7 +441,7 @@ func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
445441
b, err := m.MarshalJSON()
446442
if err == nil {
447443
e.Grow(len(b))
448-
out := availableBuffer(&e.Buffer)
444+
out := e.AvailableBuffer()
449445
out, err = appendCompact(out, b, opts.escapeHTML)
450446
e.Buffer.Write(out)
451447
}
@@ -464,7 +460,7 @@ func addrMarshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
464460
b, err := m.MarshalJSON()
465461
if err == nil {
466462
e.Grow(len(b))
467-
out := availableBuffer(&e.Buffer)
463+
out := e.AvailableBuffer()
468464
out, err = appendCompact(out, b, opts.escapeHTML)
469465
e.Buffer.Write(out)
470466
}

src/encoding/json/indent.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@ package json
66

77
import "bytes"
88

9-
// TODO(https://go.dev/issue/53685): Use bytes.Buffer.AvailableBuffer instead.
10-
func availableBuffer(b *bytes.Buffer) []byte {
11-
return b.Bytes()[b.Len():]
12-
}
13-
149
// HTMLEscape appends to dst the JSON-encoded src with <, >, &, U+2028 and U+2029
1510
// characters inside string literals changed to \u003c, \u003e, \u0026, \u2028, \u2029
1611
// so that the JSON will be safe to embed inside HTML <script> tags.
1712
// For historical reasons, web browsers don't honor standard HTML
1813
// escaping within <script> tags, so an alternative JSON encoding must be used.
1914
func HTMLEscape(dst *bytes.Buffer, src []byte) {
2015
dst.Grow(len(src))
21-
dst.Write(appendHTMLEscape(availableBuffer(dst), src))
16+
dst.Write(appendHTMLEscape(dst.AvailableBuffer(), src))
2217
}
2318

2419
func appendHTMLEscape(dst, src []byte) []byte {
@@ -45,7 +40,7 @@ func appendHTMLEscape(dst, src []byte) []byte {
4540
// insignificant space characters elided.
4641
func Compact(dst *bytes.Buffer, src []byte) error {
4742
dst.Grow(len(src))
48-
b := availableBuffer(dst)
43+
b := dst.AvailableBuffer()
4944
b, err := appendCompact(b, src, false)
5045
dst.Write(b)
5146
return err
@@ -114,7 +109,7 @@ const indentGrowthFactor = 2
114109
// if src ends in a trailing newline, so will dst.
115110
func Indent(dst *bytes.Buffer, src []byte, prefix, indent string) error {
116111
dst.Grow(indentGrowthFactor * len(src))
117-
b := availableBuffer(dst)
112+
b := dst.AvailableBuffer()
118113
b, err := appendIndent(b, src, prefix, indent)
119114
dst.Write(b)
120115
return err

0 commit comments

Comments
 (0)