Skip to content

Commit

Permalink
fix: utils.TrimBytes should trim all content (#1779)
Browse files Browse the repository at this point in the history
* perf: if all string content should be trimmed, end the loop early

* test: complete test cases for all Trim functions

* fix: utils.TrimBytes should trim all content
  • Loading branch information
fufuok committed Feb 15, 2022
1 parent e1833df commit 7b1a7a9
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion utils/bytes.go
Expand Up @@ -41,7 +41,7 @@ func TrimLeftBytes(b []byte, cutset byte) []byte {
// TrimBytes is the equivalent of bytes.Trim
func TrimBytes(b []byte, cutset byte) []byte {
i, j := 0, len(b)-1
for ; i < j; i++ {
for ; i <= j; i++ {
if b[i] != cutset {
break
}
Expand Down
27 changes: 27 additions & 0 deletions utils/bytes_test.go
Expand Up @@ -80,6 +80,15 @@ func Test_TrimRightBytes(t *testing.T) {

res = TrimRightBytes([]byte("/test"), '/')
AssertEqual(t, []byte("/test"), res)

res = TrimRightBytes([]byte(" "), ' ')
AssertEqual(t, 0, len(res))

res = TrimRightBytes([]byte(" "), ' ')
AssertEqual(t, 0, len(res))

res = TrimRightBytes([]byte(""), ' ')
AssertEqual(t, 0, len(res))
}

func Benchmark_TrimRightBytes(b *testing.B) {
Expand All @@ -106,6 +115,15 @@ func Test_TrimLeftBytes(t *testing.T) {

res = TrimLeftBytes([]byte("test/"), '/')
AssertEqual(t, []byte("test/"), res)

res = TrimLeftBytes([]byte(" "), ' ')
AssertEqual(t, 0, len(res))

res = TrimLeftBytes([]byte(" "), ' ')
AssertEqual(t, 0, len(res))

res = TrimLeftBytes([]byte(""), ' ')
AssertEqual(t, 0, len(res))
}

func Benchmark_TrimLeftBytes(b *testing.B) {
Expand Down Expand Up @@ -135,6 +153,15 @@ func Test_TrimBytes(t *testing.T) {

res = TrimBytes([]byte(".test"), '.')
AssertEqual(t, []byte("test"), res)

res = TrimBytes([]byte(" "), ' ')
AssertEqual(t, 0, len(res))

res = TrimBytes([]byte(" "), ' ')
AssertEqual(t, 0, len(res))

res = TrimBytes([]byte(""), ' ')
AssertEqual(t, 0, len(res))
}

func Benchmark_TrimBytes(b *testing.B) {
Expand Down
4 changes: 2 additions & 2 deletions utils/strings.go
Expand Up @@ -38,12 +38,12 @@ func TrimLeft(s string, cutset byte) string {
// Trim is the equivalent of strings.Trim
func Trim(s string, cutset byte) string {
i, j := 0, len(s)-1
for ; i < j; i++ {
for ; i <= j; i++ {
if s[i] != cutset {
break
}
}
for ; i <= j; j-- {
for ; i < j; j-- {
if s[j] != cutset {
break
}
Expand Down
18 changes: 18 additions & 0 deletions utils/strings_test.go
Expand Up @@ -71,6 +71,15 @@ func Test_TrimRight(t *testing.T) {

res = TrimRight("/test", '/')
AssertEqual(t, "/test", res)

res = TrimRight(" ", ' ')
AssertEqual(t, "", res)

res = TrimRight(" ", ' ')
AssertEqual(t, "", res)

res = TrimRight("", ' ')
AssertEqual(t, "", res)
}

func Benchmark_TrimRight(b *testing.B) {
Expand All @@ -97,6 +106,15 @@ func Test_TrimLeft(t *testing.T) {

res = TrimLeft("test/", '/')
AssertEqual(t, "test/", res)

res = TrimLeft(" ", ' ')
AssertEqual(t, "", res)

res = TrimLeft(" ", ' ')
AssertEqual(t, "", res)

res = TrimLeft("", ' ')
AssertEqual(t, "", res)
}

func Benchmark_TrimLeft(b *testing.B) {
Expand Down

0 comments on commit 7b1a7a9

Please sign in to comment.