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.
13 // A Builder is used to efficiently build a string using [Builder.Write] methods.
14 // It minimizes memory copying. The zero value is ready to use.
15 // Do not copy a non-zero Builder.
17 addr *Builder // of receiver, to detect copies by value
21 // noescape hides a pointer from escape analysis. It is the identity function
22 // but escape analysis doesn't think the output depends on the input.
23 // noescape is inlined and currently compiles down to zero instructions.
25 // This was copied from the runtime; see issues 23382 and 7921.
29 func noescape(p unsafe.Pointer) unsafe.Pointer {
31 return unsafe.Pointer(x ^ 0)
34 func (b *Builder) copyCheck() {
36 // This hack works around a failing of Go's escape analysis
37 // that was causing b to escape and be heap allocated.
39 // TODO: once issue 7921 is fixed, this should be reverted to
41 b.addr = (*Builder)(noescape(unsafe.Pointer(b)))
42 } else if b.addr != b {
43 panic("strings: illegal use of non-zero Builder copied by value")
47 // String returns the accumulated string.
48 func (b *Builder) String() string {
49 return unsafe.String(unsafe.SliceData(b.buf), len(b.buf))
52 // Len returns the number of accumulated bytes; b.Len() == len(b.String()).
53 func (b *Builder) Len() int { return len(b.buf) }
55 // Cap returns the capacity of the builder's underlying byte slice. It is the
56 // total space allocated for the string being built and includes any bytes
58 func (b *Builder) Cap() int { return cap(b.buf) }
60 // Reset resets the [Builder] to be empty.
61 func (b *Builder) Reset() {
66 // grow copies the buffer to a new, larger buffer so that there are at least n
67 // bytes of capacity beyond len(b.buf).
68 func (b *Builder) grow(n int) {
69 buf := bytealg.MakeNoZero(2*cap(b.buf) + n)[:len(b.buf)]
74 // Grow grows b's capacity, if necessary, to guarantee space for
75 // another n bytes. After Grow(n), at least n bytes can be written to b
76 // without another allocation. If n is negative, Grow panics.
77 func (b *Builder) Grow(n int) {
80 panic("strings.Builder.Grow: negative count")
82 if cap(b.buf)-len(b.buf) < n {
87 // Write appends the contents of p to b's buffer.
88 // Write always returns len(p), nil.
89 func (b *Builder) Write(p []byte) (int, error) {
91 b.buf = append(b.buf, p...)
95 // WriteByte appends the byte c to b's buffer.
96 // The returned error is always nil.
97 func (b *Builder) WriteByte(c byte) error {
99 b.buf = append(b.buf, c)
103 // WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
104 // It returns the length of r and a nil error.
105 func (b *Builder) WriteRune(r rune) (int, error) {
108 b.buf = utf8.AppendRune(b.buf, r)
109 return len(b.buf) - n, nil
112 // WriteString appends the contents of s to b's buffer.
113 // It returns the length of s and a nil error.
114 func (b *Builder) WriteString(s string) (int, error) {
116 b.buf = append(b.buf, s...)