Source File
builder.go
Belonging Package
strings
// Copyright 2017 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package strings
import (
)
// A Builder is used to efficiently build a string using [Builder.Write] methods.
// It minimizes memory copying. The zero value is ready to use.
// Do not copy a non-zero Builder.
type Builder struct {
addr *Builder // of receiver, to detect copies by value
// External users should never get direct access to this buffer, since
// the slice at some point will be converted to a string using unsafe, also
// data between len(buf) and cap(buf) might be uninitialized.
buf []byte
}
func ( *Builder) () {
if .addr == nil {
// This hack works around a failing of Go's escape analysis
// that was causing b to escape and be heap allocated.
// See issue 23382.
// TODO: once issue 7921 is fixed, this should be reverted to
// just "b.addr = b".
.addr = (*Builder)(abi.NoEscape(unsafe.Pointer()))
} else if .addr != {
panic("strings: illegal use of non-zero Builder copied by value")
}
}
// String returns the accumulated string.
func ( *Builder) () string {
return unsafe.String(unsafe.SliceData(.buf), len(.buf))
}
// Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func ( *Builder) () int { return len(.buf) }
// Cap returns the capacity of the builder's underlying byte slice. It is the
// total space allocated for the string being built and includes any bytes
// already written.
func ( *Builder) () int { return cap(.buf) }
// Reset resets the [Builder] to be empty.
func ( *Builder) () {
.addr = nil
.buf = nil
}
// grow copies the buffer to a new, larger buffer so that there are at least n
// bytes of capacity beyond len(b.buf).
func ( *Builder) ( int) {
:= bytealg.MakeNoZero(2*cap(.buf) + )[:len(.buf)]
copy(, .buf)
.buf =
}
// Grow grows b's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to b
// without another allocation. If n is negative, Grow panics.
func ( *Builder) ( int) {
.copyCheck()
if < 0 {
panic("strings.Builder.Grow: negative count")
}
if cap(.buf)-len(.buf) < {
.grow()
}
}
// Write appends the contents of p to b's buffer.
// Write always returns len(p), nil.
func ( *Builder) ( []byte) (int, error) {
.copyCheck()
.buf = append(.buf, ...)
return len(), nil
}
// WriteByte appends the byte c to b's buffer.
// The returned error is always nil.
func ( *Builder) ( byte) error {
.copyCheck()
.buf = append(.buf, )
return nil
}
// WriteRune appends the UTF-8 encoding of Unicode code point r to b's buffer.
// It returns the length of r and a nil error.
func ( *Builder) ( rune) (int, error) {
.copyCheck()
:= len(.buf)
.buf = utf8.AppendRune(.buf, )
return len(.buf) - , nil
}
// WriteString appends the contents of s to b's buffer.
// It returns the length of s and a nil error.
func ( *Builder) ( string) (int, error) {
.copyCheck()
.buf = append(.buf, ...)
return len(), nil
}
The pages are generated with Golds v0.7.3. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |