gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

encode_slice.go (2908B)


      1 package msgpack
      2 
      3 import (
      4 	"math"
      5 	"reflect"
      6 
      7 	"github.com/vmihailenco/msgpack/v5/msgpcode"
      8 )
      9 
     10 var stringSliceType = reflect.TypeOf(([]string)(nil))
     11 
     12 func encodeStringValue(e *Encoder, v reflect.Value) error {
     13 	return e.EncodeString(v.String())
     14 }
     15 
     16 func encodeByteSliceValue(e *Encoder, v reflect.Value) error {
     17 	return e.EncodeBytes(v.Bytes())
     18 }
     19 
     20 func encodeByteArrayValue(e *Encoder, v reflect.Value) error {
     21 	if err := e.EncodeBytesLen(v.Len()); err != nil {
     22 		return err
     23 	}
     24 
     25 	if v.CanAddr() {
     26 		b := v.Slice(0, v.Len()).Bytes()
     27 		return e.write(b)
     28 	}
     29 
     30 	e.buf = grow(e.buf, v.Len())
     31 	reflect.Copy(reflect.ValueOf(e.buf), v)
     32 	return e.write(e.buf)
     33 }
     34 
     35 func grow(b []byte, n int) []byte {
     36 	if cap(b) >= n {
     37 		return b[:n]
     38 	}
     39 	b = b[:cap(b)]
     40 	b = append(b, make([]byte, n-len(b))...)
     41 	return b
     42 }
     43 
     44 func (e *Encoder) EncodeBytesLen(l int) error {
     45 	if l < 256 {
     46 		return e.write1(msgpcode.Bin8, uint8(l))
     47 	}
     48 	if l <= math.MaxUint16 {
     49 		return e.write2(msgpcode.Bin16, uint16(l))
     50 	}
     51 	return e.write4(msgpcode.Bin32, uint32(l))
     52 }
     53 
     54 func (e *Encoder) encodeStringLen(l int) error {
     55 	if l < 32 {
     56 		return e.writeCode(msgpcode.FixedStrLow | byte(l))
     57 	}
     58 	if l < 256 {
     59 		return e.write1(msgpcode.Str8, uint8(l))
     60 	}
     61 	if l <= math.MaxUint16 {
     62 		return e.write2(msgpcode.Str16, uint16(l))
     63 	}
     64 	return e.write4(msgpcode.Str32, uint32(l))
     65 }
     66 
     67 func (e *Encoder) EncodeString(v string) error {
     68 	if intern := e.flags&useInternedStringsFlag != 0; intern || len(e.dict) > 0 {
     69 		return e.encodeInternedString(v, intern)
     70 	}
     71 	return e.encodeNormalString(v)
     72 }
     73 
     74 func (e *Encoder) encodeNormalString(v string) error {
     75 	if err := e.encodeStringLen(len(v)); err != nil {
     76 		return err
     77 	}
     78 	return e.writeString(v)
     79 }
     80 
     81 func (e *Encoder) EncodeBytes(v []byte) error {
     82 	if v == nil {
     83 		return e.EncodeNil()
     84 	}
     85 	if err := e.EncodeBytesLen(len(v)); err != nil {
     86 		return err
     87 	}
     88 	return e.write(v)
     89 }
     90 
     91 func (e *Encoder) EncodeArrayLen(l int) error {
     92 	if l < 16 {
     93 		return e.writeCode(msgpcode.FixedArrayLow | byte(l))
     94 	}
     95 	if l <= math.MaxUint16 {
     96 		return e.write2(msgpcode.Array16, uint16(l))
     97 	}
     98 	return e.write4(msgpcode.Array32, uint32(l))
     99 }
    100 
    101 func encodeStringSliceValue(e *Encoder, v reflect.Value) error {
    102 	ss := v.Convert(stringSliceType).Interface().([]string)
    103 	return e.encodeStringSlice(ss)
    104 }
    105 
    106 func (e *Encoder) encodeStringSlice(s []string) error {
    107 	if s == nil {
    108 		return e.EncodeNil()
    109 	}
    110 	if err := e.EncodeArrayLen(len(s)); err != nil {
    111 		return err
    112 	}
    113 	for _, v := range s {
    114 		if err := e.EncodeString(v); err != nil {
    115 			return err
    116 		}
    117 	}
    118 	return nil
    119 }
    120 
    121 func encodeSliceValue(e *Encoder, v reflect.Value) error {
    122 	if v.IsNil() {
    123 		return e.EncodeNil()
    124 	}
    125 	return encodeArrayValue(e, v)
    126 }
    127 
    128 func encodeArrayValue(e *Encoder, v reflect.Value) error {
    129 	l := v.Len()
    130 	if err := e.EncodeArrayLen(l); err != nil {
    131 		return err
    132 	}
    133 	for i := 0; i < l; i++ {
    134 		if err := e.EncodeValue(v.Index(i)); err != nil {
    135 			return err
    136 		}
    137 	}
    138 	return nil
    139 }