buffer.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. // Copyright 2009 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package bytes
  5. // Simple byte buffer for marshaling data.
  6. import (
  7. "errors"
  8. "io"
  9. "unicode/utf8"
  10. )
  11. // smallBufferSize is an initial allocation minimal capacity.
  12. const smallBufferSize = 64
  13. // A Buffer is a variable-sized buffer of bytes with Read and Write methods.
  14. // The zero value for Buffer is an empty buffer ready to use.
  15. type Buffer struct {
  16. buf []byte // contents are the bytes buf[off : len(buf)]
  17. off int // read at &buf[off], write at &buf[len(buf)]
  18. lastRead readOp // last read operation, so that Unread* can work correctly.
  19. }
  20. // The readOp constants describe the last action performed on
  21. // the buffer, so that UnreadRune and UnreadByte can check for
  22. // invalid usage. opReadRuneX constants are chosen such that
  23. // converted to int they correspond to the rune size that was read.
  24. type readOp int8
  25. // Don't use iota for these, as the values need to correspond with the
  26. // names and comments, which is easier to see when being explicit.
  27. const (
  28. opRead readOp = -1 // Any other read operation.
  29. opInvalid readOp = 0 // Non-read operation.
  30. opReadRune1 readOp = 1 // Read rune of size 1.
  31. opReadRune2 readOp = 2 // Read rune of size 2.
  32. opReadRune3 readOp = 3 // Read rune of size 3.
  33. opReadRune4 readOp = 4 // Read rune of size 4.
  34. )
  35. // ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
  36. var ErrTooLarge = errors.New("bytes.Buffer: too large")
  37. var errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read")
  38. const maxInt = int(^uint(0) >> 1)
  39. // Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
  40. // The slice is valid for use only until the next buffer modification (that is,
  41. // only until the next call to a method like Read, Write, Reset, or Truncate).
  42. // The slice aliases the buffer content at least until the next buffer modification,
  43. // so immediate changes to the slice will affect the result of future reads.
  44. func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
  45. // String returns the contents of the unread portion of the buffer
  46. // as a string. If the Buffer is a nil pointer, it returns "<nil>".
  47. //
  48. // To build strings more efficiently, see the strings.Builder type.
  49. func (b *Buffer) String() string {
  50. if b == nil {
  51. // Special case, useful in debugging.
  52. return "<nil>"
  53. }
  54. return string(b.buf[b.off:])
  55. }
  56. // empty reports whether the unread portion of the buffer is empty.
  57. func (b *Buffer) empty() bool { return len(b.buf) <= b.off }
  58. // Len returns the number of bytes of the unread portion of the buffer;
  59. // b.Len() == len(b.Bytes()).
  60. func (b *Buffer) Len() int { return len(b.buf) - b.off }
  61. // Cap returns the capacity of the buffer's underlying byte slice, that is, the
  62. // total space allocated for the buffer's data.
  63. func (b *Buffer) Cap() int { return cap(b.buf) }
  64. // Truncate discards all but the first n unread bytes from the buffer
  65. // but continues to use the same allocated storage.
  66. // It panics if n is negative or greater than the length of the buffer.
  67. func (b *Buffer) Truncate(n int) {
  68. if n == 0 {
  69. b.Reset()
  70. return
  71. }
  72. b.lastRead = opInvalid
  73. if n < 0 || n > b.Len() {
  74. panic("bytes.Buffer: truncation out of range")
  75. }
  76. b.buf = b.buf[:b.off+n]
  77. }
  78. // Reset resets the buffer to be empty,
  79. // but it retains the underlying storage for use by future writes.
  80. // Reset is the same as Truncate(0).
  81. func (b *Buffer) Reset() {
  82. b.buf = b.buf[:0]
  83. b.off = 0
  84. b.lastRead = opInvalid
  85. }
  86. // tryGrowByReslice is a inlineable version of grow for the fast-case where the
  87. // internal buffer only needs to be resliced.
  88. // It returns the index where bytes should be written and whether it succeeded.
  89. func (b *Buffer) tryGrowByReslice(n int) (int, bool) {
  90. if l := len(b.buf); n <= cap(b.buf)-l {
  91. b.buf = b.buf[:l+n]
  92. return l, true
  93. }
  94. return 0, false
  95. }
  96. // grow grows the buffer to guarantee space for n more bytes.
  97. // It returns the index where bytes should be written.
  98. // If the buffer can't grow it will panic with ErrTooLarge.
  99. func (b *Buffer) grow(n int) int {
  100. m := b.Len()
  101. // If buffer is empty, reset to recover space.
  102. if m == 0 && b.off != 0 {
  103. b.Reset()
  104. }
  105. // Try to grow by means of a reslice.
  106. if i, ok := b.tryGrowByReslice(n); ok {
  107. return i
  108. }
  109. if b.buf == nil && n <= smallBufferSize {
  110. b.buf = make([]byte, n, smallBufferSize)
  111. return 0
  112. }
  113. c := cap(b.buf)
  114. if n <= c/2-m {
  115. // We can slide things down instead of allocating a new
  116. // slice. We only need m+n <= c to slide, but
  117. // we instead let capacity get twice as large so we
  118. // don't spend all our time copying.
  119. copy(b.buf, b.buf[b.off:])
  120. } else if c > maxInt-c-n {
  121. panic(ErrTooLarge)
  122. } else {
  123. // Not enough space anywhere, we need to allocate.
  124. buf := makeSlice(2*c + n)
  125. copy(buf, b.buf[b.off:])
  126. b.buf = buf
  127. }
  128. // Restore b.off and len(b.buf).
  129. b.off = 0
  130. b.buf = b.buf[:m+n]
  131. return m
  132. }
  133. // Grow grows the buffer's capacity, if necessary, to guarantee space for
  134. // another n bytes. After Grow(n), at least n bytes can be written to the
  135. // buffer without another allocation.
  136. // If n is negative, Grow will panic.
  137. // If the buffer can't grow it will panic with ErrTooLarge.
  138. func (b *Buffer) Grow(n int) {
  139. if n < 0 {
  140. panic("bytes.Buffer.Grow: negative count")
  141. }
  142. m := b.grow(n)
  143. b.buf = b.buf[:m]
  144. }
  145. // Write appends the contents of p to the buffer, growing the buffer as
  146. // needed. The return value n is the length of p; err is always nil. If the
  147. // buffer becomes too large, Write will panic with ErrTooLarge.
  148. func (b *Buffer) Write(p []byte) (n int, err error) {
  149. b.lastRead = opInvalid
  150. m, ok := b.tryGrowByReslice(len(p))
  151. if !ok {
  152. m = b.grow(len(p))
  153. }
  154. return copy(b.buf[m:], p), nil
  155. }
  156. // WriteString appends the contents of s to the buffer, growing the buffer as
  157. // needed. The return value n is the length of s; err is always nil. If the
  158. // buffer becomes too large, WriteString will panic with ErrTooLarge.
  159. func (b *Buffer) WriteString(s string) (n int, err error) {
  160. b.lastRead = opInvalid
  161. m, ok := b.tryGrowByReslice(len(s))
  162. if !ok {
  163. m = b.grow(len(s))
  164. }
  165. return copy(b.buf[m:], s), nil
  166. }
  167. // MinRead is the minimum slice size passed to a Read call by
  168. // Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
  169. // what is required to hold the contents of r, ReadFrom will not grow the
  170. // underlying buffer.
  171. const MinRead = 512
  172. // ReadFrom reads data from r until EOF and appends it to the buffer, growing
  173. // the buffer as needed. The return value n is the number of bytes read. Any
  174. // error except io.EOF encountered during the read is also returned. If the
  175. // buffer becomes too large, ReadFrom will panic with ErrTooLarge.
  176. func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
  177. b.lastRead = opInvalid
  178. for {
  179. i := b.grow(MinRead)
  180. b.buf = b.buf[:i]
  181. m, e := r.Read(b.buf[i:cap(b.buf)])
  182. if m < 0 {
  183. panic(errNegativeRead)
  184. }
  185. b.buf = b.buf[:i+m]
  186. n += int64(m)
  187. if e == io.EOF {
  188. return n, nil // e is EOF, so return nil explicitly
  189. }
  190. if e != nil {
  191. return n, e
  192. }
  193. }
  194. }
  195. // makeSlice allocates a slice of size n. If the allocation fails, it panics
  196. // with ErrTooLarge.
  197. func makeSlice(n int) []byte {
  198. // If the make fails, give a known error.
  199. defer func() {
  200. if recover() != nil {
  201. panic(ErrTooLarge)
  202. }
  203. }()
  204. return make([]byte, n)
  205. }
  206. // WriteTo writes data to w until the buffer is drained or an error occurs.
  207. // The return value n is the number of bytes written; it always fits into an
  208. // int, but it is int64 to match the io.WriterTo interface. Any error
  209. // encountered during the write is also returned.
  210. func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
  211. b.lastRead = opInvalid
  212. if nBytes := b.Len(); nBytes > 0 {
  213. m, e := w.Write(b.buf[b.off:])
  214. if m > nBytes {
  215. panic("bytes.Buffer.WriteTo: invalid Write count")
  216. }
  217. b.off += m
  218. n = int64(m)
  219. if e != nil {
  220. return n, e
  221. }
  222. // all bytes should have been written, by definition of
  223. // Write method in io.Writer
  224. if m != nBytes {
  225. return n, io.ErrShortWrite
  226. }
  227. }
  228. // Buffer is now empty; reset.
  229. b.Reset()
  230. return n, nil
  231. }
  232. // WriteByte appends the byte c to the buffer, growing the buffer as needed.
  233. // The returned error is always nil, but is included to match bufio.Writer's
  234. // WriteByte. If the buffer becomes too large, WriteByte will panic with
  235. // ErrTooLarge.
  236. func (b *Buffer) WriteByte(c byte) error {
  237. b.lastRead = opInvalid
  238. m, ok := b.tryGrowByReslice(1)
  239. if !ok {
  240. m = b.grow(1)
  241. }
  242. b.buf[m] = c
  243. return nil
  244. }
  245. // WriteRune appends the UTF-8 encoding of Unicode code point r to the
  246. // buffer, returning its length and an error, which is always nil but is
  247. // included to match bufio.Writer's WriteRune. The buffer is grown as needed;
  248. // if it becomes too large, WriteRune will panic with ErrTooLarge.
  249. func (b *Buffer) WriteRune(r rune) (n int, err error) {
  250. // Compare as uint32 to correctly handle negative runes.
  251. if uint32(r) < utf8.RuneSelf {
  252. b.WriteByte(byte(r))
  253. return 1, nil
  254. }
  255. b.lastRead = opInvalid
  256. m, ok := b.tryGrowByReslice(utf8.UTFMax)
  257. if !ok {
  258. m = b.grow(utf8.UTFMax)
  259. }
  260. n = utf8.EncodeRune(b.buf[m:m+utf8.UTFMax], r)
  261. b.buf = b.buf[:m+n]
  262. return n, nil
  263. }
  264. // Read reads the next len(p) bytes from the buffer or until the buffer
  265. // is drained. The return value n is the number of bytes read. If the
  266. // buffer has no data to return, err is io.EOF (unless len(p) is zero);
  267. // otherwise it is nil.
  268. func (b *Buffer) Read(p []byte) (n int, err error) {
  269. b.lastRead = opInvalid
  270. if b.empty() {
  271. // Buffer is empty, reset to recover space.
  272. b.Reset()
  273. if len(p) == 0 {
  274. return 0, nil
  275. }
  276. return 0, io.EOF
  277. }
  278. n = copy(p, b.buf[b.off:])
  279. b.off += n
  280. if n > 0 {
  281. b.lastRead = opRead
  282. }
  283. return n, nil
  284. }
  285. // Next returns a slice containing the next n bytes from the buffer,
  286. // advancing the buffer as if the bytes had been returned by Read.
  287. // If there are fewer than n bytes in the buffer, Next returns the entire buffer.
  288. // The slice is only valid until the next call to a read or write method.
  289. func (b *Buffer) Next(n int) []byte {
  290. b.lastRead = opInvalid
  291. m := b.Len()
  292. if n > m {
  293. n = m
  294. }
  295. data := b.buf[b.off : b.off+n]
  296. b.off += n
  297. if n > 0 {
  298. b.lastRead = opRead
  299. }
  300. return data
  301. }
  302. // ReadByte reads and returns the next byte from the buffer.
  303. // If no byte is available, it returns error io.EOF.
  304. func (b *Buffer) ReadByte() (byte, error) {
  305. if b.empty() {
  306. // Buffer is empty, reset to recover space.
  307. b.Reset()
  308. return 0, io.EOF
  309. }
  310. c := b.buf[b.off]
  311. b.off++
  312. b.lastRead = opRead
  313. return c, nil
  314. }
  315. // ReadRune reads and returns the next UTF-8-encoded
  316. // Unicode code point from the buffer.
  317. // If no bytes are available, the error returned is io.EOF.
  318. // If the bytes are an erroneous UTF-8 encoding, it
  319. // consumes one byte and returns U+FFFD, 1.
  320. func (b *Buffer) ReadRune() (r rune, size int, err error) {
  321. if b.empty() {
  322. // Buffer is empty, reset to recover space.
  323. b.Reset()
  324. return 0, 0, io.EOF
  325. }
  326. c := b.buf[b.off]
  327. if c < utf8.RuneSelf {
  328. b.off++
  329. b.lastRead = opReadRune1
  330. return rune(c), 1, nil
  331. }
  332. r, n := utf8.DecodeRune(b.buf[b.off:])
  333. b.off += n
  334. b.lastRead = readOp(n)
  335. return r, n, nil
  336. }
  337. // UnreadRune unreads the last rune returned by ReadRune.
  338. // If the most recent read or write operation on the buffer was
  339. // not a successful ReadRune, UnreadRune returns an error. (In this regard
  340. // it is stricter than UnreadByte, which will unread the last byte
  341. // from any read operation.)
  342. func (b *Buffer) UnreadRune() error {
  343. if b.lastRead <= opInvalid {
  344. return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
  345. }
  346. if b.off >= int(b.lastRead) {
  347. b.off -= int(b.lastRead)
  348. }
  349. b.lastRead = opInvalid
  350. return nil
  351. }
  352. var errUnreadByte = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")
  353. // UnreadByte unreads the last byte returned by the most recent successful
  354. // read operation that read at least one byte. If a write has happened since
  355. // the last read, if the last read returned an error, or if the read read zero
  356. // bytes, UnreadByte returns an error.
  357. func (b *Buffer) UnreadByte() error {
  358. if b.lastRead == opInvalid {
  359. return errUnreadByte
  360. }
  361. b.lastRead = opInvalid
  362. if b.off > 0 {
  363. b.off--
  364. }
  365. return nil
  366. }
  367. // ReadBytes reads until the first occurrence of delim in the input,
  368. // returning a slice containing the data up to and including the delimiter.
  369. // If ReadBytes encounters an error before finding a delimiter,
  370. // it returns the data read before the error and the error itself (often io.EOF).
  371. // ReadBytes returns err != nil if and only if the returned data does not end in
  372. // delim.
  373. func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
  374. slice, err := b.readSlice(delim)
  375. // return a copy of slice. The buffer's backing array may
  376. // be overwritten by later calls.
  377. line = append(line, slice...)
  378. return line, err
  379. }
  380. // readSlice is like ReadBytes but returns a reference to internal buffer data.
  381. func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
  382. i := IndexByte(b.buf[b.off:], delim)
  383. end := b.off + i + 1
  384. if i < 0 {
  385. end = len(b.buf)
  386. err = io.EOF
  387. }
  388. line = b.buf[b.off:end]
  389. b.off = end
  390. b.lastRead = opRead
  391. return line, err
  392. }
  393. // ReadString reads until the first occurrence of delim in the input,
  394. // returning a string containing the data up to and including the delimiter.
  395. // If ReadString encounters an error before finding a delimiter,
  396. // it returns the data read before the error and the error itself (often io.EOF).
  397. // ReadString returns err != nil if and only if the returned data does not end
  398. // in delim.
  399. func (b *Buffer) ReadString(delim byte) (line string, err error) {
  400. slice, err := b.readSlice(delim)
  401. return string(slice), err
  402. }
  403. // NewBuffer creates and initializes a new Buffer using buf as its
  404. // initial contents. The new Buffer takes ownership of buf, and the
  405. // caller should not use buf after this call. NewBuffer is intended to
  406. // prepare a Buffer to read existing data. It can also be used to set
  407. // the initial size of the internal buffer for writing. To do that,
  408. // buf should have the desired capacity but a length of zero.
  409. //
  410. // In most cases, new(Buffer) (or just declaring a Buffer variable) is
  411. // sufficient to initialize a Buffer.
  412. func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
  413. // NewBufferString creates and initializes a new Buffer using string s as its
  414. // initial contents. It is intended to prepare a buffer to read an existing
  415. // string.
  416. //
  417. // In most cases, new(Buffer) (or just declaring a Buffer variable) is
  418. // sufficient to initialize a Buffer.
  419. func NewBufferString(s string) *Buffer {
  420. return &Buffer{buf: []byte(s)}
  421. }