MCPcopy Create free account
hub / github.com/aws/smithy-go / encodeByteSlice

Function encodeByteSlice

encoding/json/value.go:119–149  ·  view source on GitHub ↗

Based on encoding/json encodeByteSlice from the Go Standard Library https://golang.org/src/encoding/json/encode.go

(w *bytes.Buffer, scratch []byte, v []byte)

Source from the content-addressed store, hash-verified

117// Based on encoding/json encodeByteSlice from the Go Standard Library
118// https://golang.org/src/encoding/json/encode.go
119func encodeByteSlice(w *bytes.Buffer, scratch []byte, v []byte) {
120 if v == nil {
121 w.WriteString(null)
122 return
123 }
124
125 w.WriteRune(quote)
126
127 encodedLen := base64.StdEncoding.EncodedLen(len(v))
128 if encodedLen <= len(scratch) {
129 // If the encoded bytes fit in e.scratch, avoid an extra
130 // allocation and use the cheaper Encoding.Encode.
131 dst := scratch[:encodedLen]
132 base64.StdEncoding.Encode(dst, v)
133 w.Write(dst)
134 } else if encodedLen <= 1024 {
135 // The encoded bytes are short enough to allocate for, and
136 // Encoding.Encode is still cheaper.
137 dst := make([]byte, encodedLen)
138 base64.StdEncoding.Encode(dst, v)
139 w.Write(dst)
140 } else {
141 // The encoded bytes are too long to cheaply allocate, and
142 // Encoding.Encode is no longer noticeably cheaper.
143 enc := base64.NewEncoder(base64.StdEncoding, w)
144 enc.Write(v)
145 enc.Close()
146 }
147
148 w.WriteRune(quote)
149}

Callers 1

Base64EncodeBytesMethod · 0.70

Calls 5

WriteStringMethod · 0.65
WriteRuneMethod · 0.65
WriteMethod · 0.65
EncodeMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected