Skip to content

Commit 092183a

Browse files
committed
reference benchmark for regexp decamel
1 parent fd67745 commit 092183a

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

internal/str/str.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,17 @@ import (
1111
)
1212

1313
var (
14-
re = regexp.MustCompile(`([A-Z]+)`)
14+
uncamel = regexp.MustCompile(`([A-Z]+)`)
15+
clean = regexp.MustCompile(`[^\w]`)
1516
)
1617

17-
// CamelRegexp return the given string as space delimeted. Note! it's slow. Use
18+
// DecamelRegexp return the given string as space delimeted. Note! it's slow. Use
1819
// Decamel instead.
19-
func CamelRegexp(str string) string {
20-
str = re.ReplaceAllString(str, ` $1`)
20+
func DecamelRegexp(str string) string {
21+
str = clean.ReplaceAllString(str, " ")
22+
str = uncamel.ReplaceAllString(str, ` $1`)
2123
str = strings.Trim(str, " ")
24+
str = strings.ToLower(str)
2225
return str
2326
}
2427

internal/str/str_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313

1414
func BenchmarkCamelRegexp(b *testing.B) {
1515
for n := 0; n < b.N; n++ {
16-
_ = str.CamelRegexp(camelStr)
16+
_ = str.DecamelRegexp(camelStr)
1717
}
1818
}
1919

@@ -23,6 +23,31 @@ func BenchmarkDecamel(b *testing.B) {
2323
}
2424
}
2525

26+
func TestCamel(t *testing.T) {
27+
t.Parallel()
28+
type args struct {
29+
s string
30+
}
31+
tests := []struct {
32+
name string
33+
args args
34+
want string
35+
}{
36+
{"simple", args{"CamelString"}, "camel string"},
37+
{"number", args{"CamelString2Testing"}, "camel string2 testing"},
38+
{"acronym", args{"ARMCamelString"}, "armcamel string"},
39+
{"acronym at end", args{"archIsARM"}, "arch is arm"},
40+
}
41+
for _, ttv := range tests {
42+
tt := ttv
43+
t.Run(tt.name, func(t *testing.T) {
44+
t.Parallel()
45+
got := str.DecamelRegexp(tt.args.s)
46+
test.RequireEqual(t, got, tt.want)
47+
})
48+
}
49+
}
50+
2651
func TestDecamel(t *testing.T) {
2752
t.Parallel()
2853
type args struct {

0 commit comments

Comments
 (0)