buffer.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. // Package buffer provides a thin wrapper around a byte slice. Unlike the
  21. // standard library's bytes.Buffer, it supports a portion of the strconv
  22. // package's zero-allocation formatters.
  23. package buffer // import "go.uber.org/zap/buffer"
  24. import (
  25. "strconv"
  26. "time"
  27. )
  28. const _size = 1024 // by default, create 1 KiB buffers
  29. // Buffer is a thin wrapper around a byte slice. It's intended to be pooled, so
  30. // the only way to construct one is via a Pool.
  31. type Buffer struct {
  32. bs []byte
  33. pool Pool
  34. }
  35. // AppendByte writes a single byte to the Buffer.
  36. func (b *Buffer) AppendByte(v byte) {
  37. b.bs = append(b.bs, v)
  38. }
  39. // AppendString writes a string to the Buffer.
  40. func (b *Buffer) AppendString(s string) {
  41. b.bs = append(b.bs, s...)
  42. }
  43. // AppendInt appends an integer to the underlying buffer (assuming base 10).
  44. func (b *Buffer) AppendInt(i int64) {
  45. b.bs = strconv.AppendInt(b.bs, i, 10)
  46. }
  47. // AppendTime appends the time formatted using the specified layout.
  48. func (b *Buffer) AppendTime(t time.Time, layout string) {
  49. b.bs = t.AppendFormat(b.bs, layout)
  50. }
  51. // AppendUint appends an unsigned integer to the underlying buffer (assuming
  52. // base 10).
  53. func (b *Buffer) AppendUint(i uint64) {
  54. b.bs = strconv.AppendUint(b.bs, i, 10)
  55. }
  56. // AppendBool appends a bool to the underlying buffer.
  57. func (b *Buffer) AppendBool(v bool) {
  58. b.bs = strconv.AppendBool(b.bs, v)
  59. }
  60. // AppendFloat appends a float to the underlying buffer. It doesn't quote NaN
  61. // or +/- Inf.
  62. func (b *Buffer) AppendFloat(f float64, bitSize int) {
  63. b.bs = strconv.AppendFloat(b.bs, f, 'f', -1, bitSize)
  64. }
  65. // Len returns the length of the underlying byte slice.
  66. func (b *Buffer) Len() int {
  67. return len(b.bs)
  68. }
  69. // Cap returns the capacity of the underlying byte slice.
  70. func (b *Buffer) Cap() int {
  71. return cap(b.bs)
  72. }
  73. // Bytes returns a mutable reference to the underlying byte slice.
  74. func (b *Buffer) Bytes() []byte {
  75. return b.bs
  76. }
  77. // String returns a string copy of the underlying byte slice.
  78. func (b *Buffer) String() string {
  79. return string(b.bs)
  80. }
  81. // Reset resets the underlying byte slice. Subsequent writes re-use the slice's
  82. // backing array.
  83. func (b *Buffer) Reset() {
  84. b.bs = b.bs[:0]
  85. }
  86. // Write implements io.Writer.
  87. func (b *Buffer) Write(bs []byte) (int, error) {
  88. b.bs = append(b.bs, bs...)
  89. return len(bs), nil
  90. }
  91. // WriteByte writes a single byte to the Buffer.
  92. //
  93. // Error returned is always nil, function signature is compatible
  94. // with bytes.Buffer and bufio.Writer
  95. func (b *Buffer) WriteByte(v byte) error {
  96. b.AppendByte(v)
  97. return nil
  98. }
  99. // WriteString writes a string to the Buffer.
  100. //
  101. // Error returned is always nil, function signature is compatible
  102. // with bytes.Buffer and bufio.Writer
  103. func (b *Buffer) WriteString(s string) (int, error) {
  104. b.AppendString(s)
  105. return len(s), nil
  106. }
  107. // TrimNewline trims any final "\n" byte from the end of the buffer.
  108. func (b *Buffer) TrimNewline() {
  109. if i := len(b.bs) - 1; i >= 0 {
  110. if b.bs[i] == '\n' {
  111. b.bs = b.bs[:i]
  112. }
  113. }
  114. }
  115. // Free returns the Buffer to its Pool.
  116. //
  117. // Callers must not retain references to the Buffer after calling Free.
  118. func (b *Buffer) Free() {
  119. b.pool.put(b)
  120. }