Skip to content

Commit 835a2e9

Browse files
authored
Merge pull request #10 from cubiest/fix_titlecase
Properly clean the name before title-casing
2 parents d65c8c3 + a72a57f commit 835a2e9

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

‎strmangle.go‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import (
1111
"sort"
1212
"strings"
1313
"sync"
14+
"unicode"
1415
)
1516

1617
var (
1718
idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz")
1819
smartQuoteRgx = regexp.MustCompile(`^(?i)"?[a-z_][_a-z0-9\-]*"?(\."?[_a-z][_a-z0-9]*"?)*(\.\*)?$`)
1920

20-
rgxEnum = regexp.MustCompile(`^enum(\.[a-zA-Z0-9_]+)?\([^\)]+\)$`)
21-
rgxWhitespace = regexp.MustCompile(`\s`)
22-
rgxAlphanumericAndUnderscores = regexp.MustCompile("[^a-zA-Z0-9_]+")
21+
rgxEnum = regexp.MustCompile(`^enum(\.[a-zA-Z0-9_]+)?\([^\)]+\)$`)
22+
rgxWhitespace = regexp.MustCompile(`\s`)
2323
)
2424

2525
func AddUppercase(s string) {
@@ -252,7 +252,16 @@ func TitleCase(n string) string {
252252
return val
253253
}
254254

255-
cleanN := rgxAlphanumericAndUnderscores.ReplaceAllLiteralString(n, "_")
255+
var cleanN string
256+
for _, r := range n {
257+
var char string
258+
if unicode.IsLetter(r) || unicode.IsDigit(r) {
259+
char = string(r)
260+
} else {
261+
char = "_"
262+
}
263+
cleanN += char
264+
}
256265

257266
// If the string is made up of only uppercase letters and underscores,
258267
// then return as is and do not strip the underscores
@@ -711,7 +720,7 @@ func ParseEnumName(s string) string {
711720
return s[startIndex+1:]
712721
}
713722

714-
//StripWhitespace removes all whitespace from a string
723+
// StripWhitespace removes all whitespace from a string
715724
func StripWhitespace(value string) string {
716725
return rgxWhitespace.ReplaceAllString(value, "")
717726
}

0 commit comments

Comments
 (0)