github.com/ssgreg/logf@v1.4.1/buffer.go (about)

     1  package logf
     2  
     3  import (
     4  	"strconv"
     5  )
     6  
     7  // PageSize is the recommended buffer size.
     8  const (
     9  	PageSize = 4 * 1024
    10  )
    11  
    12  // NewBuffer creates the new instance of Buffer with default capacity.
    13  func NewBuffer() *Buffer {
    14  	return NewBufferWithCapacity(PageSize)
    15  }
    16  
    17  // NewBufferWithCapacity creates the new instance of Buffer with the given
    18  // capacity.
    19  func NewBufferWithCapacity(capacity int) *Buffer {
    20  	return &Buffer{make([]byte, 0, capacity)}
    21  }
    22  
    23  // Buffer is a helping wrapper for byte slice.
    24  type Buffer struct {
    25  	Data []byte
    26  }
    27  
    28  // Write implements io.Writer.
    29  func (b *Buffer) Write(p []byte) (n int, err error) {
    30  	b.Data = append(b.Data, p...)
    31  
    32  	return len(p), nil
    33  }
    34  
    35  // String implements fmt.Stringer.
    36  func (b *Buffer) String() string {
    37  	return string(b.Bytes())
    38  }
    39  
    40  // EnsureSize ensures that the Buffer is able to append 's' bytes without
    41  // a further realloc.
    42  func (b *Buffer) EnsureSize(s int) []byte {
    43  	if cap(b.Data)-len(b.Data) < s {
    44  		tmpLen := len(b.Data)
    45  		tmp := make([]byte, tmpLen, tmpLen+s+(tmpLen>>1))
    46  		copy(tmp, b.Data)
    47  		b.Data = tmp
    48  	}
    49  
    50  	return b.Data
    51  }
    52  
    53  // ExtendBytes extends the Buffer with the given size and returns a slice
    54  // tp extended part of the Buffer.
    55  func (b *Buffer) ExtendBytes(s int) []byte {
    56  	b.Data = append(b.Data, make([]byte, s)...)
    57  
    58  	return b.Data[len(b.Data)-s:]
    59  }
    60  
    61  // AppendString appends a string to the Buffer.
    62  func (b *Buffer) AppendString(data string) {
    63  	b.Data = append(b.Data, data...)
    64  }
    65  
    66  // AppendBytes appends a byte slice to the Buffer.
    67  func (b *Buffer) AppendBytes(data []byte) {
    68  	b.Data = append(b.Data, data...)
    69  }
    70  
    71  // AppendByte appends a single byte to the Buffer.
    72  func (b *Buffer) AppendByte(data byte) {
    73  	b.Data = append(b.Data, data)
    74  }
    75  
    76  // Reset resets the underlying byte slice.
    77  func (b *Buffer) Reset() {
    78  	b.Data = b.Data[:0]
    79  }
    80  
    81  // Back returns the last byte of the underlying byte slice. A caller is in
    82  // charge of checking that the Buffer is not empty.
    83  func (b *Buffer) Back() byte {
    84  	return b.Data[len(b.Data)-1]
    85  }
    86  
    87  // Bytes returns the underlying byte slice as is.
    88  func (b *Buffer) Bytes() []byte {
    89  	return b.Data
    90  }
    91  
    92  // Len returns the length of the underlying byte slice.
    93  func (b *Buffer) Len() int {
    94  	return len(b.Data)
    95  }
    96  
    97  // Cap returns the capacity of the underlying byte slice.
    98  func (b *Buffer) Cap() int {
    99  	return cap(b.Data)
   100  }
   101  
   102  // AppendUint appends the string form in the base 10 of the given unsigned
   103  // integer to the given Buffer.
   104  func AppendUint(b *Buffer, n uint64) {
   105  	b.Data = strconv.AppendUint(b.Data, n, 10)
   106  }
   107  
   108  // AppendInt appends the string form in the base 10 of the given integer
   109  // to the given Buffer.
   110  func AppendInt(b *Buffer, n int64) {
   111  	b.Data = strconv.AppendInt(b.Data, n, 10)
   112  }
   113  
   114  // AppendFloat32 appends the string form of the given float32 to the given
   115  // Buffer.
   116  func AppendFloat32(b *Buffer, n float32) {
   117  	b.Data = strconv.AppendFloat(b.Data, float64(n), 'g', -1, 32)
   118  }
   119  
   120  // AppendFloat64 appends the string form of the given float32 to the given
   121  // Buffer.
   122  func AppendFloat64(b *Buffer, n float64) {
   123  	b.Data = strconv.AppendFloat(b.Data, n, 'g', -1, 64)
   124  }
   125  
   126  // AppendBool appends "true" or "false", according to the given bool to the
   127  // given Buffer.
   128  func AppendBool(b *Buffer, n bool) {
   129  	b.Data = strconv.AppendBool(b.Data, n)
   130  }