// Len returns the number of accumulated bytes; b.Len() == len(b.String()).
func (b *Builder) Len() int { return len(b.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 (b *Builder) Cap() int { return cap(b.buf) }
+
// Reset resets the Builder to be empty.
func (b *Builder) Reset() {
b.addr = nil
if n := b.Len(); n != len(got) {
t.Errorf("Len: got %d; but len(String()) is %d", n, len(got))
}
+ if n := b.Cap(); n < len(got) {
+ t.Errorf("Cap: got %d; but len(String()) is %d", n, len(got))
+ }
}
func TestBuilder(t *testing.T) {
allocs := testing.AllocsPerRun(100, func() {
var b Builder
b.Grow(growLen) // should be only alloc, when growLen > 0
+ if b.Cap() < growLen {
+ t.Fatalf("growLen=%d: Cap() is lower than growLen", growLen)
+ }
b.Write(p)
if b.String() != string(p) {
t.Fatalf("growLen=%d: bad data written after Grow", growLen)
b.Len()
},
},
+ {
+ name: "Cap",
+ wantPanic: false,
+ fn: func() {
+ var a Builder
+ a.WriteByte('x')
+ b := a
+ b.Cap()
+ },
+ },
{
name: "Reset",
wantPanic: false,