github.com/pubgo/xprocess@v0.1.11/xprocess_strings/builder.go (about)

     1  // Copyright 2017 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  
     5  package xprocess_strings
     6  
     7  import (
     8  	"sync"
     9  	"unicode/utf8"
    10  	"unsafe"
    11  )
    12  
    13  // A Builder is used to efficiently build a string using Write methods.
    14  // It minimizes memory copying. The zero value is ready to use.
    15  // Do not copy a non-zero Builder.
    16  type Builder struct {
    17  	noCopy noCopy
    18  	buf    []byte
    19  }
    20  
    21  // String returns the accumulated string.
    22  func (b *Builder) String() string {
    23  	return *(*string)(unsafe.Pointer(&b.buf))
    24  }
    25  
    26  // Len returns the number of accumulated bytes; b.Len() == len(b.String()).
    27  func (b *Builder) Len() int { return len(b.buf) }
    28  
    29  // Cap returns the capacity of the builder's underlying byte slice. It is the
    30  // total space allocated for the string being built and includes any bytes
    31  // already written.
    32  func (b *Builder) Cap() int { return cap(b.buf) }
    33  
    34  // Reset resets the Builder to be empty.
    35  func (b *Builder) Reset() {
    36  	b.buf = b.buf[:0]
    37  	builderPool.Put(b)
    38  }
    39  
    40  // grow copies the buffer to a new, larger buffer so that there are at least n
    41  // bytes of capacity beyond len(b.buf).
    42  func (b *Builder) grow(n int) {
    43  	buf := make([]byte, len(b.buf), 2*cap(b.buf)+n)
    44  	copy(buf, b.buf)
    45  	b.buf = buf
    46  }
    47  
    48  // Grow grows b's capacity, if necessary, to guarantee space for
    49  // another n bytes. After Grow(n), at least n bytes can be written to b
    50  // without another allocation. If n is negative, Grow panics.
    51  func (b *Builder) Grow(n int) {
    52  	if n < 0 {
    53  		panic("strings.Builder.Grow: negative count")
    54  	}
    55  	if cap(b.buf)-len(b.buf) < n {
    56  		b.grow(n)
    57  	}
    58  }
    59  
    60  // Write appends the contents of p to b's buffer.
    61  // Write always returns len(p), nil.
    62  func (b *Builder) Write(p []byte) (int, error) {
    63  	b.buf = append(b.buf, p...)
    64  	return len(p), nil
    65  }
    66  
    67  // WriteByte appends the byte c to b's buffer.
    68  // The returned error is always nil.
    69  func (b *Builder) WriteByte(c byte) error {
    70  	b.buf = append(b.buf, c)
    71  	return nil
    72  }
    73  
    74  // WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
    75  // It returns the length of r and a nil error.
    76  func (b *Builder) WriteRune(r rune) (int, error) {
    77  	if r < utf8.RuneSelf {
    78  		b.buf = append(b.buf, byte(r))
    79  		return 1, nil
    80  	}
    81  	l := len(b.buf)
    82  	if cap(b.buf)-l < utf8.UTFMax {
    83  		b.grow(utf8.UTFMax)
    84  	}
    85  	n := utf8.EncodeRune(b.buf[l:l+utf8.UTFMax], r)
    86  	b.buf = b.buf[:l+n]
    87  	return n, nil
    88  }
    89  
    90  func (b *Builder) Append(ss ...string) {
    91  	for i := range ss {
    92  		_, _ = b.WriteString(ss[i])
    93  	}
    94  }
    95  
    96  // WriteString appends the contents of s to b's buffer.
    97  // It returns the length of s and a nil error.
    98  func (b *Builder) WriteString(s string) (int, error) {
    99  	b.buf = append(b.buf, s...)
   100  	return len(s), nil
   101  }
   102  
   103  var builderPool = sync.Pool{New: func() interface{} { return &Builder{} }}
   104  
   105  func GetBuilder() *Builder { return builderPool.Get().(*Builder) }
   106  
   107  func Append(ss ...string) string {
   108  	b := GetBuilder()
   109  	defer b.Reset()
   110  
   111  	for i := range ss {
   112  		_, _ = b.WriteString(ss[i])
   113  	}
   114  	return b.String()
   115  }
   116  
   117  // noCopy may be embedded into structs which must not be copied
   118  // after the first use.
   119  //
   120  // See https://golang.org/issues/8005#issuecomment-190753527
   121  // for details.
   122  type noCopy struct{}
   123  
   124  // Lock is a no-op used by -copylocks checker from `go vet`.
   125  func (*noCopy) Lock()   {}
   126  func (*noCopy) Unlock() {}