Skip to content

Commit

Permalink
Make New/Underscore a bit faster. (#38)
Browse files Browse the repository at this point in the history
Co-authored-by: Mark Bates <[email protected]>
  • Loading branch information
AtomicNibble and markbates authored Mar 3, 2020
1 parent 7a04ff1 commit 222210e
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 18 deletions.
29 changes: 16 additions & 13 deletions ident.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func toParts(s string) []string {
return []string{strings.ToUpper(s)}
}
var prev rune
var x string
var x strings.Builder
x.Grow(len(s))
for _, c := range s {
cs := string(c)
// fmt.Println("### cs ->", cs)
// fmt.Println("### unicode.IsControl(c) ->", unicode.IsControl(c))
// fmt.Println("### unicode.IsDigit(c) ->", unicode.IsDigit(c))
Expand All @@ -58,35 +58,38 @@ func toParts(s string) []string {
}

if isSpace(c) {
parts = xappend(parts, x)
x = cs
parts = xappend(parts, x.String())
x.Reset()
x.WriteRune(c)
prev = c
continue
}

if unicode.IsUpper(c) && !unicode.IsUpper(prev) {
parts = xappend(parts, x)
x = cs
parts = xappend(parts, x.String())
x.Reset()
x.WriteRune(c)
prev = c
continue
}
if unicode.IsUpper(c) && baseAcronyms[strings.ToUpper(x)] {
parts = xappend(parts, x)
x = cs
if unicode.IsUpper(c) && baseAcronyms[strings.ToUpper(x.String())] {
parts = xappend(parts, x.String())
x.Reset()
x.WriteRune(c)
prev = c
continue
}
if unicode.IsLetter(c) || unicode.IsDigit(c) || unicode.IsPunct(c) || c == '`' {
prev = c
x += cs
x.WriteRune(c)
continue
}

parts = xappend(parts, x)
x = ""
parts = xappend(parts, x.String())
x.Reset()
prev = c
}
parts = xappend(parts, x)
parts = xappend(parts, x.String())

return parts
}
Expand Down
46 changes: 46 additions & 0 deletions ident_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,49 @@ func Test_MarshalText(t *testing.T) {
r.NoError((&n).UnmarshalText([]byte("bates")))
r.Equal("bates", n.String())
}

func Benchmark_New(b *testing.B) {

table := []string{
"",
"widget",
"widget_id",
"WidgetID",
"Widget_ID",
"widget_ID",
"widget/ID",
"widgetID",
"widgetName",
"JWTName",
"JWTname",
"jwtname",
"sql",
"sQl",
"id",
"Id",
"iD",
"html",
"Html",
"HTML",
"with `code` inside",
"Donald E. Knuth",
"Random text with *(bad)* characters",
"Allow_Under_Scores",
"Trailing bad characters!@#",
"!@#Leading bad characters",
"Squeeze separators",
"Test with + sign",
"Malmö",
"Garçons",
"Opsů",
"Ærøskøbing",
"Aßlar",
"Japanese: 日本語",
}

for n := 0; n < b.N; n++ {
for i := range table {
New(table[i])
}
}
}
11 changes: 6 additions & 5 deletions underscore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ func Underscore(s string) string {
// Nice to see you! = nice_to_see_you
// widgetID = widget_id
func (i Ident) Underscore() Ident {
var out []string
out := make([]string, 0, len(i.Parts))
for _, part := range i.Parts {
var x string
var x strings.Builder
x.Grow(len(part))
for _, c := range part {
if unicode.IsLetter(c) || unicode.IsDigit(c) {
x += string(c)
x.WriteRune(c)
}
}
if x != "" {
out = append(out, x)
if x.Len() > 0 {
out = append(out, x.String())
}
}
return New(strings.ToLower(strings.Join(out, "_")))
Expand Down
19 changes: 19 additions & 0 deletions underscore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@ func Test_Underscore(t *testing.T) {
})
}
}

func Benchmark_Underscore(b *testing.B) {

table := []string{
"",
"bob dylan",
"Nice to see you!",
"*hello*",
"i've read a book! have you?",
"This is `code` ok",
"TLCForm",
}

for n := 0; n < b.N; n++ {
for i := range table {
Underscore(table[i])
}
}
}

0 comments on commit 222210e

Please sign in to comment.