github.com/webmafia/fast@v0.10.0/string_buffer.go (about)

     1  package fast
     2  
     3  import (
     4  	"unicode/utf8"
     5  )
     6  
     7  // A StringBuffer is used to efficiently build a string using Write methods.
     8  // It minimizes memory copying. The zero value is ready to use.
     9  // Do not copy a non-zero StringBuffer.
    10  type StringBuffer struct {
    11  	buf []byte
    12  }
    13  
    14  func NewStringBuffer(cap int) *StringBuffer {
    15  	return &StringBuffer{
    16  		buf: make([]byte, 0, cap),
    17  	}
    18  }
    19  
    20  // String returns the accumulated string.
    21  func (b StringBuffer) String() string {
    22  	return BytesToString(b.buf)
    23  }
    24  
    25  // String returns the accumulated string as bytes.
    26  func (b StringBuffer) Bytes() []byte {
    27  	return b.buf
    28  }
    29  
    30  // Len returns the number of accumulated bytes; b.Len() == len(b.String()).
    31  func (b *StringBuffer) Len() int {
    32  	return len(b.buf)
    33  }
    34  
    35  // Cap returns the capacity of the StringBuffer's underlying byte slice. It is the
    36  // total space allocated for the string being built and includes any bytes
    37  // already written.
    38  func (b *StringBuffer) Cap() int {
    39  	return cap(b.buf)
    40  }
    41  
    42  // Reset resets the StringBuffer to be empty.
    43  func (b *StringBuffer) Reset() {
    44  	b.buf = b.buf[:0]
    45  }
    46  
    47  // grow copies the buffer to a new, larger buffer so that there are at least n
    48  // bytes of capacity beyond len(b.buf).
    49  func (b *StringBuffer) grow(n int) {
    50  	buf := MakeNoZero(2*cap(b.buf) + n)[:len(b.buf)]
    51  	copy(buf, b.buf)
    52  	b.buf = buf
    53  }
    54  
    55  // Grow grows b's capacity, if necessary, to guarantee space for
    56  // another n bytes. After Grow(n), at least n bytes can be written to b
    57  // without another allocation. If n is negative, Grow panics.
    58  func (b *StringBuffer) Grow(n int) {
    59  	if n < 0 {
    60  		panic("fast.StringBuffer.Grow: negative count")
    61  	}
    62  	if cap(b.buf)-len(b.buf) < n {
    63  		b.grow(n)
    64  	}
    65  }
    66  
    67  // Write appends the contents of p to b's buffer.
    68  // Write always returns len(p), nil.
    69  func (b *StringBuffer) Write(p []byte) (int, error) {
    70  	b.buf = append(b.buf, p...)
    71  	return len(p), nil
    72  }
    73  
    74  // WriteByte appends the byte c to b's buffer.
    75  // The returned error is always nil.
    76  func (b *StringBuffer) WriteByte(c byte) error {
    77  	b.buf = append(b.buf, c)
    78  	return nil
    79  }
    80  
    81  // WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
    82  // It returns the length of r and a nil error.
    83  func (b *StringBuffer) WriteRune(r rune) (int, error) {
    84  	n := len(b.buf)
    85  	b.buf = utf8.AppendRune(b.buf, r)
    86  	return len(b.buf) - n, nil
    87  }
    88  
    89  // WriteString appends the contents of s to b's buffer.
    90  // It returns the length of s and a nil error.
    91  func (b *StringBuffer) WriteString(s string) (int, error) {
    92  	b.buf = append(b.buf, s...)
    93  	return len(s), nil
    94  }