代码拉取完成,页面将自动刷新
package funcs
import (
"encoding/base64"
"reflect"
"gitee.com/go-better/dev/encoding/jsondoc/encoder/types"
)
func newSliceEncoder(t reflect.Type) encoderFunc {
// Byte slices get special treatment; arrays don't.
if t.Elem().Kind() == reflect.Uint8 {
p := reflect.PtrTo(t.Elem())
if !p.Implements(marshalerType) && !p.Implements(textMarshalerType) {
return encodeByteSlice
}
}
enc := sliceEncoder{newArrayEncoder(t)}
return enc.encode
}
func newArrayEncoder(t reflect.Type) encoderFunc {
enc := arrayEncoder{typeEncoder(t.Elem())}
return enc.encode
}
// sliceEncoder just wraps an arrayEncoder, checking to make sure the value isn't nil.
type sliceEncoder struct {
arrayEnc encoderFunc
}
func (se sliceEncoder) encode(buf *types.Buffer, v reflect.Value, opts types.Options) {
if v.Len() == 0 {
typ := v.Type()
if opts.NotRecursion(typ) {
v = reflect.MakeSlice(typ, 1, 1)
v.Index(0).Set(reflect.Zero(typ.Elem()))
opts.AppendConvertedTypeInUpperLayers(typ)
}
}
if v.IsNil() {
buf.WriteString("null")
return
}
se.arrayEnc(buf, v, opts)
}
type arrayEncoder struct {
elemEnc encoderFunc
}
func (ae arrayEncoder) encode(buf *types.Buffer, v reflect.Value, opts types.Options) {
buf.WriteByte('[')
n := v.Len()
if n > 0 {
opts.WriteCommentIfPresent(buf)
}
for i := 0; i < n; i++ {
if i > 0 {
buf.WriteByte(',')
}
ae.elemEnc(buf, v.Index(i), opts)
}
buf.WriteByte(']')
}
func encodeByteSlice(buf *types.Buffer, v reflect.Value, _ types.Options) {
if v.IsNil() {
buf.WriteString("null")
return
}
s := v.Bytes()
buf.WriteByte('"')
encodedLen := base64.StdEncoding.EncodedLen(len(s))
if encodedLen <= len(buf.Scratch) {
// If the encoded bytes fit in buf.Scratch, avoid an extra
// allocation and use the cheaper Encoding.Encode.
dst := buf.Scratch[:encodedLen]
base64.StdEncoding.Encode(dst, s)
buf.Write(dst)
} else if encodedLen <= 1024 {
// The encoded bytes are short enough to allocate for, and
// Encoding.Encode is still cheaper.
dst := make([]byte, encodedLen)
base64.StdEncoding.Encode(dst, s)
buf.Write(dst)
} else {
// The encoded bytes are too long to cheaply allocate, and
// Encoding.Encode is no longer noticeably cheaper.
enc := base64.NewEncoder(base64.StdEncoding, buf)
enc.Write(s)
enc.Close()
}
buf.WriteByte('"')
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。