Skip to content

Commit fa1e7cb

Browse files
committed
Add special handling for UPPER_SNAKE_CASE in TitleCase
1 parent ce10828 commit fa1e7cb

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

strmangle.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ var (
1717
idAlphabet = []byte("abcdefghijklmnopqrstuvwxyz")
1818
smartQuoteRgx = regexp.MustCompile(`^(?i)"?[a-z_][_a-z0-9\-]*"?(\."?[_a-z][_a-z0-9]*"?)*(\.\*)?$`)
1919

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

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

255-
name := []byte(rgxAlphanumeric.ReplaceAllLiteralString(n, "_"))
255+
cleanN := rgxAlphanumericAndUnderscores.ReplaceAllLiteralString(n, "_")
256+
257+
// If the string is made up of only uppercase letters and underscores,
258+
// then return as is and do not strip the underscores
259+
// This keeps strings such as PUBLIC_KEY readable and not make it PUBLICKEY
260+
if len(n) == len(cleanN) && n == strings.ToUpper(n) {
261+
// Cache the title case as the same string
262+
mut.Lock()
263+
titleCaseCache[n] = n
264+
mut.Unlock()
265+
266+
return n
267+
}
268+
269+
name := []byte(cleanN)
256270
ln := len(name)
257271
buf := GetBuffer()
258272

0 commit comments

Comments
 (0)