Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: utils.TrimBytes should trim all content #1779

Merged
merged 3 commits into from Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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