encode_slice.go 2.8 KB

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