Skip to content

Commit

Permalink
add u.TrimPrefix()
Browse files Browse the repository at this point in the history
  • Loading branch information
kjk committed Oct 9, 2021
1 parent b0c8f0d commit b3f6c16
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
7 changes: 7 additions & 0 deletions u/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,10 @@ func Capitalize(s string) string {
s = strings.ToLower(s)
return strings.ToUpper(s[0:1]) + s[1:]
}

// TrimPrefix is like strings.TrimPrefix but also returns a bool
// indicating that the string was trimmed
func TrimPrefix(s string, prefix string) (string, bool) {
s2 := strings.TrimPrefix(s, prefix)
return s2, len(s) != len(s2)
}
22 changes: 22 additions & 0 deletions u/strings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package u

import (
"testing"

"github.com/kjk/common/assert"
)

func TestTrimPrefix(t *testing.T) {
tests := []string{
"foo", "f", "oo",
"foo", "o", "foo",
}

n := len(tests)
for i := 0; i < n; i += 3 {
got, trimmed := TrimPrefix(tests[i], tests[i+1])
exp := tests[i+2]
assert.Equal(t, exp, got)
assert.Equal(t, trimmed, tests[i] != got, "%#v, %#v", tests[i], tests[i+1])
}
}

0 comments on commit b3f6c16

Please sign in to comment.