Skip to content

proposal: strings: add (*Builder).ResetKeepCapacity method #60768

Open
@mateusz834

Description

@mateusz834

Currently the strings.Builder wastes the remaining capacity when (*Builder).Reset is being called, it works just the same way as creating a new empty strings.Builder.

// Reset resets the Builder to be empty.
func (b *Builder) Reset() {
	b.addr = nil
	b.buf = nil
}

Currently it is not possible in a backwards compatibility manner to keep the remaining capacity in the Builder after calling Reset(), because it would require keeping the b.addr pointer, so that it is not possible to copy the Builder.

I propose adding a simple (*Builder).ResetKeepCapacity method with following signature:

// ResetKeepCapacity resets the builder and keeps the remaining
// capacity (b.Cap() - b.Len()) for future use.
func (b *Builder) ResetKeepCapacity() {
	b.buf = b.buf[len(b.buf):]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions