Skip to content

Commit

Permalink
Merge pull request #111 from xushiwei/t
Browse files Browse the repository at this point in the history
stringutil: NewBuilderSize
  • Loading branch information
xushiwei authored Feb 25, 2024
2 parents 3cf3f74 + 22936bf commit fb42664
Show file tree
Hide file tree
Showing 3 changed files with 14 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
3 changes: 3 additions & 0 deletions stringutil/concat.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package stringutil

// Concat concatenates parts of a string together.
func Concat(parts ...string) string {
if len(parts) == 1 {
return parts[0]
}
n := 0
for _, part := range parts {
n += len(part)
Expand Down
7 changes: 5 additions & 2 deletions stringutil/string_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,19 @@ import (
)

func TestConcat(t *testing.T) {
if ret := Concat("1"); ret != "1" {
t.Fatal("Concat(1):", ret)
}
if ret := Concat("1", "23", "!"); ret != "123!" {
t.Fatal("Concat:", ret)
}
}

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 fb42664

Please sign in to comment.