Skip to content

Commit

Permalink
feat: add str util: strutil.Buffer, a extended bytes.Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed May 19, 2022
1 parent 4a9d090 commit 53dd0b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
46 changes: 46 additions & 0 deletions strutil/bytes_buf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package strutil

import (
"bytes"
"fmt"
)

// Buffer wrap and extends the bytes.Buffer
type Buffer struct {
*bytes.Buffer
}

// NewEmptyBuffer instance
func NewEmptyBuffer() *Buffer {
return &Buffer{
Buffer: new(bytes.Buffer),
}
}

// QuietWritef write message to buffer
func (b *Buffer) QuietWritef(tpl string, vs ...interface{}) {
_, _ = b.WriteString(fmt.Sprintf(tpl, vs...))
}

// QuietWriteln write message to buffer with newline
func (b *Buffer) QuietWriteln(s string) {
_, _ = b.WriteString(s)
_ = b.WriteByte('\n')
}

// QuietWriteString to buffer
func (b *Buffer) QuietWriteString(ss ...string) {
for _, s := range ss {
_, _ = b.WriteString(s)
}
}

// MustWriteString to buffer
func (b *Buffer) MustWriteString(ss ...string) {
for _, s := range ss {
_, err := b.WriteString(s)
if err != nil {
panic(err)
}
}
}
File renamed without changes.
3 changes: 3 additions & 0 deletions strutil/split_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func TestSplit(t *testing.T) {
ss = strutil.SplitN("a, , b,c", ",", 2)
assert.Equal(t, `[]string{"a", "b,c"}`, fmt.Sprintf("%#v", ss))

ss = strutil.SplitN("origin https://github.com/gookit/gitw (push)", " ", 3)
assert.Len(t, ss, 3)

ss = strutil.Split(" ", ",")
assert.Nil(t, ss)
}
Expand Down

0 comments on commit 53dd0b2

Please sign in to comment.