bytesutil.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // Package bytesutil provides utility functions for working with bytes and byte streams that are useful when
  2. // working with the RESP protocol.
  3. package bytesutil
  4. import (
  5. "bufio"
  6. "fmt"
  7. "io"
  8. "strconv"
  9. "sync"
  10. "errors"
  11. "github.com/mediocregopher/radix/v3/resp"
  12. )
  13. // AnyIntToInt64 converts a value of any of Go's integer types (signed and unsigned) into a signed int64.
  14. //
  15. // If m is not of one of Go's built in integer types the call will panic.
  16. func AnyIntToInt64(m interface{}) int64 {
  17. switch mt := m.(type) {
  18. case int:
  19. return int64(mt)
  20. case int8:
  21. return int64(mt)
  22. case int16:
  23. return int64(mt)
  24. case int32:
  25. return int64(mt)
  26. case int64:
  27. return mt
  28. case uint:
  29. return int64(mt)
  30. case uint8:
  31. return int64(mt)
  32. case uint16:
  33. return int64(mt)
  34. case uint32:
  35. return int64(mt)
  36. case uint64:
  37. return int64(mt)
  38. }
  39. panic(fmt.Sprintf("anyIntToInt64 got bad arg: %#v", m))
  40. }
  41. var bytePool = sync.Pool{
  42. New: func() interface{} {
  43. b := make([]byte, 0, 64)
  44. return &b
  45. },
  46. }
  47. // GetBytes returns a non-nil pointer to a byte slice from a pool of byte slices.
  48. //
  49. // The returned byte slice should be put back into the pool using PutBytes after usage.
  50. func GetBytes() *[]byte {
  51. return bytePool.Get().(*[]byte)
  52. }
  53. // PutBytes puts the given byte slice pointer into a pool that can be accessed via GetBytes.
  54. //
  55. // After calling PutBytes the given pointer and byte slice must not be accessed anymore.
  56. func PutBytes(b *[]byte) {
  57. *b = (*b)[:0]
  58. bytePool.Put(b)
  59. }
  60. // ParseInt is a specialized version of strconv.ParseInt that parses a base-10 encoded signed integer from a []byte.
  61. //
  62. // This can be used to avoid allocating a string, since strconv.ParseInt only takes a string.
  63. func ParseInt(b []byte) (int64, error) {
  64. if len(b) == 0 {
  65. return 0, errors.New("empty slice given to parseInt")
  66. }
  67. var neg bool
  68. if b[0] == '-' || b[0] == '+' {
  69. neg = b[0] == '-'
  70. b = b[1:]
  71. }
  72. n, err := ParseUint(b)
  73. if err != nil {
  74. return 0, err
  75. }
  76. if neg {
  77. return -int64(n), nil
  78. }
  79. return int64(n), nil
  80. }
  81. // ParseUint is a specialized version of strconv.ParseUint that parses a base-10 encoded integer from a []byte.
  82. //
  83. // This can be used to avoid allocating a string, since strconv.ParseUint only takes a string.
  84. func ParseUint(b []byte) (uint64, error) {
  85. if len(b) == 0 {
  86. return 0, errors.New("empty slice given to parseUint")
  87. }
  88. var n uint64
  89. for i, c := range b {
  90. if c < '0' || c > '9' {
  91. return 0, fmt.Errorf("invalid character %c at position %d in parseUint", c, i)
  92. }
  93. n *= 10
  94. n += uint64(c - '0')
  95. }
  96. return n, nil
  97. }
  98. // Expand expands the given byte slice to exactly n bytes.
  99. //
  100. // If cap(b) < n, a new slice will be allocated and filled with the bytes from b.
  101. func Expand(b []byte, n int) []byte {
  102. if cap(b) < n {
  103. nb := make([]byte, n)
  104. copy(nb, b)
  105. return nb
  106. }
  107. return b[:n]
  108. }
  109. // BufferedBytesDelim reads a line from br and checks that the line ends with \r\n, returning the line without \r\n.
  110. func BufferedBytesDelim(br *bufio.Reader) ([]byte, error) {
  111. b, err := br.ReadSlice('\n')
  112. if err != nil {
  113. return nil, err
  114. } else if len(b) < 2 || b[len(b)-2] != '\r' {
  115. return nil, fmt.Errorf("malformed resp %q", b)
  116. }
  117. return b[:len(b)-2], err
  118. }
  119. // BufferedIntDelim reads the current line from br as an integer.
  120. func BufferedIntDelim(br *bufio.Reader) (int64, error) {
  121. b, err := BufferedBytesDelim(br)
  122. if err != nil {
  123. return 0, err
  124. }
  125. return ParseInt(b)
  126. }
  127. // ReadNAppend appends exactly n bytes from r into b.
  128. func ReadNAppend(r io.Reader, b []byte, n int) ([]byte, error) {
  129. if n == 0 {
  130. return b, nil
  131. }
  132. m := len(b)
  133. b = Expand(b, len(b)+n)
  134. _, err := io.ReadFull(r, b[m:])
  135. return b, err
  136. }
  137. // ReadNDiscard discards exactly n bytes from r.
  138. func ReadNDiscard(r io.Reader, n int) error {
  139. type discarder interface {
  140. Discard(int) (int, error)
  141. }
  142. if n == 0 {
  143. return nil
  144. }
  145. switch v := r.(type) {
  146. case discarder:
  147. _, err := v.Discard(n)
  148. return err
  149. case io.Seeker:
  150. _, err := v.Seek(int64(n), io.SeekCurrent)
  151. return err
  152. }
  153. scratch := GetBytes()
  154. defer PutBytes(scratch)
  155. *scratch = (*scratch)[:cap(*scratch)]
  156. if len(*scratch) < n {
  157. *scratch = make([]byte, 8192)
  158. }
  159. for {
  160. buf := *scratch
  161. if len(buf) > n {
  162. buf = buf[:n]
  163. }
  164. nr, err := r.Read(buf)
  165. n -= nr
  166. if n == 0 || err != nil {
  167. return err
  168. }
  169. }
  170. }
  171. // ReadInt reads the next n bytes from r as a signed 64 bit integer.
  172. func ReadInt(r io.Reader, n int) (int64, error) {
  173. scratch := GetBytes()
  174. defer PutBytes(scratch)
  175. var err error
  176. if *scratch, err = ReadNAppend(r, *scratch, n); err != nil {
  177. return 0, err
  178. }
  179. i, err := ParseInt(*scratch)
  180. if err != nil {
  181. return 0, resp.ErrDiscarded{Err: err}
  182. }
  183. return i, nil
  184. }
  185. // ReadUint reads the next n bytes from r as an unsigned 64 bit integer.
  186. func ReadUint(r io.Reader, n int) (uint64, error) {
  187. scratch := GetBytes()
  188. defer PutBytes(scratch)
  189. var err error
  190. if *scratch, err = ReadNAppend(r, *scratch, n); err != nil {
  191. return 0, err
  192. }
  193. ui, err := ParseUint(*scratch)
  194. if err != nil {
  195. return 0, resp.ErrDiscarded{Err: err}
  196. }
  197. return ui, nil
  198. }
  199. // ReadFloat reads the next n bytes from r as a 64 bit floating point number with the given precision.
  200. func ReadFloat(r io.Reader, precision, n int) (float64, error) {
  201. scratch := GetBytes()
  202. defer PutBytes(scratch)
  203. var err error
  204. if *scratch, err = ReadNAppend(r, *scratch, n); err != nil {
  205. return 0, err
  206. }
  207. f, err := strconv.ParseFloat(string(*scratch), precision)
  208. if err != nil {
  209. return 0, resp.ErrDiscarded{Err: err}
  210. }
  211. return f, nil
  212. }