MCPcopy Create free account
hub / github.com/blues/note / encodeByteSlice

Function encodeByteSlice

jsonxt/encode.go:722–750  ·  view source on GitHub ↗
(e *encodeState, v reflect.Value, _ encOpts)

Source from the content-addressed store, hash-verified

720}
721
722func encodeByteSlice(e *encodeState, v reflect.Value, _ encOpts) {
723 if v.IsNil() {
724 e.WriteString("null")
725 return
726 }
727 s := v.Bytes()
728 e.WriteByte('"')
729 encodedLen := base64.StdEncoding.EncodedLen(len(s))
730 if encodedLen <= len(e.scratch) {
731 // If the encoded bytes fit in e.scratch, avoid an extra
732 // allocation and use the cheaper Encoding.Encode.
733 dst := e.scratch[:encodedLen]
734 base64.StdEncoding.Encode(dst, s)
735 e.Write(dst)
736 } else if encodedLen <= 1024 {
737 // The encoded bytes are short enough to allocate for, and
738 // Encoding.Encode is still cheaper.
739 dst := make([]byte, encodedLen)
740 base64.StdEncoding.Encode(dst, s)
741 e.Write(dst)
742 } else {
743 // The encoded bytes are too long to cheaply allocate, and
744 // Encoding.Encode is no longer noticeably cheaper.
745 enc := base64.NewEncoder(base64.StdEncoding, e)
746 enc.Write(s)
747 enc.Close()
748 }
749 e.WriteByte('"')
750}
751
752// sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
753type sliceEncoder struct {

Callers

nothing calls this directly

Calls 2

EncodeMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected