Skip to content

Commit

Permalink
stringutil: NewBuilderSize
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Feb 25, 2024
1 parent a134450 commit 1698ae2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion stringutil/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ type Builder struct {
}

// NewBuilder creates a new Builder object.
func NewBuilder(ncap int) *Builder {
func NewBuilder(b []byte) *Builder {
return &Builder{b: b}
}

// NewBuilderSize creates a new Builder object whose buffer has at least the specified size.
func NewBuilderSize(ncap int) *Builder {
return &Builder{b: make([]byte, 0, ncap)}
}

Expand Down
4 changes: 2 additions & 2 deletions stringutil/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ func TestConcat(t *testing.T) {
}

func TestBuild(t *testing.T) {
if ret := NewBuilder(0).Build(); ret != "" {
if ret := NewBuilder(nil).Build(); ret != "" {
t.Fatal("NewBuilder(0):", ret)
}
if ret := NewBuilder(16).Add("1").AddByte('2', '3').AddByte('!').Build(); ret != "123!" {
if ret := NewBuilderSize(16).Add("1").AddByte('2', '3').AddByte('!').Build(); ret != "123!" {
t.Fatal("TestBuild:", ret)
}
}
Expand Down

0 comments on commit 1698ae2

Please sign in to comment.