mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
encoding/json/jsontext: rename Encoder.UnusedBuffer as Encoder.AvailableBuffer
This follows the precedent set by: bufio.Writer.AvailableBuffer bytes.Buffer.AvailableBuffer both with methods that return a zero-length buffer that is intended to only be used with a following Write call. This keeps the older UnusedBuffer method around so that at least one commit that has both methods for migration purposes. Updates #71497 Change-Id: I3815f593e09f645280ae5ad9cbdd63a6c147123b Reviewed-on: https://go-review.googlesource.com/c/go/+/683896 Reviewed-by: Damien Neil <dneil@google.com> TryBot-Bypass: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
This commit is contained in:
parent
f8ccda2e05
commit
0b4d2eab2f
3 changed files with 17 additions and 12 deletions
|
|
@ -74,8 +74,8 @@ type encodeBuffer struct {
|
||||||
|
|
||||||
// maxValue is the approximate maximum Value size passed to WriteValue.
|
// maxValue is the approximate maximum Value size passed to WriteValue.
|
||||||
maxValue int
|
maxValue int
|
||||||
// unusedCache is the buffer returned by the UnusedBuffer method.
|
// availBuffer is the buffer returned by the AvailableBuffer method.
|
||||||
unusedCache []byte
|
availBuffer []byte // always has zero length
|
||||||
// bufStats is statistics about buffer utilization.
|
// bufStats is statistics about buffer utilization.
|
||||||
// It is only used with pooled encoders in pools.go.
|
// It is only used with pooled encoders in pools.go.
|
||||||
bufStats bufferStatistics
|
bufStats bufferStatistics
|
||||||
|
|
@ -465,9 +465,9 @@ func (e *encoderState) AppendRaw(k Kind, safeASCII bool, appendFn func([]byte) (
|
||||||
isVerbatim := safeASCII || !jsonwire.NeedEscape(b[pos+len(`"`):len(b)-len(`"`)])
|
isVerbatim := safeASCII || !jsonwire.NeedEscape(b[pos+len(`"`):len(b)-len(`"`)])
|
||||||
if !isVerbatim {
|
if !isVerbatim {
|
||||||
var err error
|
var err error
|
||||||
b2 := append(e.unusedCache, b[pos+len(`"`):len(b)-len(`"`)]...)
|
b2 := append(e.availBuffer, b[pos+len(`"`):len(b)-len(`"`)]...)
|
||||||
b, err = jsonwire.AppendQuote(b[:pos], string(b2), &e.Flags)
|
b, err = jsonwire.AppendQuote(b[:pos], string(b2), &e.Flags)
|
||||||
e.unusedCache = b2[:0]
|
e.availBuffer = b2[:0]
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return wrapSyntacticError(e, err, pos, +1)
|
return wrapSyntacticError(e, err, pos, +1)
|
||||||
}
|
}
|
||||||
|
|
@ -900,20 +900,25 @@ func (e *Encoder) OutputOffset() int64 {
|
||||||
return e.s.previousOffsetEnd()
|
return e.s.previousOffsetEnd()
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnusedBuffer returns a zero-length buffer with a possible non-zero capacity.
|
// Deprecated: Use [Encoder.AvailableBuffer] instead.
|
||||||
|
func (e *Encoder) UnusedBuffer() []byte {
|
||||||
|
return e.AvailableBuffer()
|
||||||
|
}
|
||||||
|
|
||||||
|
// AvailableBuffer returns a zero-length buffer with a possible non-zero capacity.
|
||||||
// This buffer is intended to be used to populate a [Value]
|
// This buffer is intended to be used to populate a [Value]
|
||||||
// being passed to an immediately succeeding [Encoder.WriteValue] call.
|
// being passed to an immediately succeeding [Encoder.WriteValue] call.
|
||||||
//
|
//
|
||||||
// Example usage:
|
// Example usage:
|
||||||
//
|
//
|
||||||
// b := d.UnusedBuffer()
|
// b := d.AvailableBuffer()
|
||||||
// b = append(b, '"')
|
// b = append(b, '"')
|
||||||
// b = appendString(b, v) // append the string formatting of v
|
// b = appendString(b, v) // append the string formatting of v
|
||||||
// b = append(b, '"')
|
// b = append(b, '"')
|
||||||
// ... := d.WriteValue(b)
|
// ... := d.WriteValue(b)
|
||||||
//
|
//
|
||||||
// It is the user's responsibility to ensure that the value is valid JSON.
|
// It is the user's responsibility to ensure that the value is valid JSON.
|
||||||
func (e *Encoder) UnusedBuffer() []byte {
|
func (e *Encoder) AvailableBuffer() []byte {
|
||||||
// NOTE: We don't return e.buf[len(e.buf):cap(e.buf)] since WriteValue would
|
// NOTE: We don't return e.buf[len(e.buf):cap(e.buf)] since WriteValue would
|
||||||
// need to take special care to avoid mangling the data while reformatting.
|
// need to take special care to avoid mangling the data while reformatting.
|
||||||
// WriteValue can't easily identify whether the input Value aliases e.buf
|
// WriteValue can't easily identify whether the input Value aliases e.buf
|
||||||
|
|
@ -921,10 +926,10 @@ func (e *Encoder) UnusedBuffer() []byte {
|
||||||
// Should this ever alias e.buf, we need to consider how it operates with
|
// Should this ever alias e.buf, we need to consider how it operates with
|
||||||
// the specialized performance optimization for bytes.Buffer.
|
// the specialized performance optimization for bytes.Buffer.
|
||||||
n := 1 << bits.Len(uint(e.s.maxValue|63)) // fast approximation for max length
|
n := 1 << bits.Len(uint(e.s.maxValue|63)) // fast approximation for max length
|
||||||
if cap(e.s.unusedCache) < n {
|
if cap(e.s.availBuffer) < n {
|
||||||
e.s.unusedCache = make([]byte, 0, n)
|
e.s.availBuffer = make([]byte, 0, n)
|
||||||
}
|
}
|
||||||
return e.s.unusedCache
|
return e.s.availBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
// StackDepth returns the depth of the state machine for written JSON data.
|
// StackDepth returns the depth of the state machine for written JSON data.
|
||||||
|
|
|
||||||
|
|
@ -113,7 +113,7 @@ func marshalInlinedFallbackAll(enc *jsontext.Encoder, va addressableValue, mo *j
|
||||||
mk := newAddressableValue(m.Type().Key())
|
mk := newAddressableValue(m.Type().Key())
|
||||||
mv := newAddressableValue(m.Type().Elem())
|
mv := newAddressableValue(m.Type().Elem())
|
||||||
marshalKey := func(mk addressableValue) error {
|
marshalKey := func(mk addressableValue) error {
|
||||||
b, err := jsonwire.AppendQuote(enc.UnusedBuffer(), mk.String(), &mo.Flags)
|
b, err := jsonwire.AppendQuote(enc.AvailableBuffer(), mk.String(), &mo.Flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return newMarshalErrorBefore(enc, m.Type().Key(), err)
|
return newMarshalErrorBefore(enc, m.Type().Key(), err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -199,7 +199,7 @@ func (n Number) MarshalJSONTo(enc *jsontext.Encoder) error {
|
||||||
}
|
}
|
||||||
n = cmp.Or(n, "0")
|
n = cmp.Or(n, "0")
|
||||||
var num []byte
|
var num []byte
|
||||||
val := enc.UnusedBuffer()
|
val := enc.AvailableBuffer()
|
||||||
if stringify {
|
if stringify {
|
||||||
val = append(val, '"')
|
val = append(val, '"')
|
||||||
val = append(val, n...)
|
val = append(val, n...)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue