Skip to content

Commit d84ba88

Browse files
committed
allocate buffer in wrap before printing to it
1 parent 88c821f commit d84ba88

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

flag.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -680,11 +680,10 @@ func wrap(i, w int, s string) string {
680680
// for all flags in the FlagSet. Wrapped to `cols` columns (0 for no
681681
// wrapping)
682682
func (f *FlagSet) FlagUsagesWrapped(cols int) string {
683-
buf := new(strings.Builder)
684-
685-
lines := make([]string, 0, len(f.formal))
686-
687-
maxlen := 0
683+
var (
684+
max, maxlen int
685+
lines = make([]string, 0, len(f.formal))
686+
)
688687
f.VisitAll(func(flag *Flag) {
689688
if flag.Hidden {
690689
return
@@ -738,8 +737,11 @@ func (f *FlagSet) FlagUsagesWrapped(cols int) string {
738737
}
739738

740739
lines = append(lines, line)
740+
max += len(line)
741741
})
742742

743+
buf := new(strings.Builder)
744+
buf.Grow(max)
743745
for _, line := range lines {
744746
sidx := strings.Index(line, "\x00")
745747
spacing := strings.Repeat(" ", maxlen-sidx)

0 commit comments

Comments
 (0)