strings: add Builder.Cap method #26269
Labels
FrozenDueToAge
NeedsFix
The path to resolution is known, but the work has not been done.
Proposal-Accepted
Milestone
This would mirror
bytes.Buffer.Cap
, which returns the capacity of the underlying[]byte
.In particular, I need access to the capacity to calculate if a number of bytes will fit in the Builder without an allocation. That would look like:
One possible use case is to control how the allocations happen. For example, I'd like to "batch"
[]byte
tostring
allocations, to amortize their cost until the compiler gets smart enough to remove the allocs when possible.My code could look like:
As far as I can tell, writing such a function is not simple without a
Cap
method. And adding such a method shouldn't add any problems, other than adding to the API. Unless there is a particular reason why the method was left out that I'm not aware of./cc @cespare @bradfitz @ianlancetaylor
The text was updated successfully, but these errors were encountered: