Optimize TitleCase more.

- Remove support for insane cases of 9test as well as id9e9
- Code cleanup
This commit is contained in:
Aaron L 2016-08-21 22:49:57 -07:00
parent 604fbea7de
commit 248c45d1f6
2 changed files with 10 additions and 50 deletions

View file

@ -181,49 +181,19 @@ func TitleCase(n string) string {
} }
word := name[start:end] word := name[start:end]
wordLen := len(word)
var vowels bool var vowels bool
numStart := -1 numStart := wordLen
numBroken := false
for i, c := range word { for i, c := range word {
// If the word starts with digits, it does not vowels = vowels || (c == 97 || c == 101 || c == 105 || c == 111 || c == 117 || c == 121)
// fit the criteria for "uppercasedWords"
if i == 0 && c > 47 && c < 58 {
numBroken = true
break
}
if c > 47 && c < 58 && numStart == -1 { if c > 47 && c < 58 && numStart == wordLen {
numStart = i numStart = i
continue
} else if c > 47 && c < 58 {
continue
}
// A letter inbetween numbers will set numBroken to true, eg: 9e9
if numStart != -1 {
numBroken = true
break
}
// If on the last loop and we have not found any digits, update
// numStart to len of word, so we can get the full word below
if i == len(word)-1 && numStart == -1 && !numBroken {
numStart = len(word)
} }
} }
var match bool _, match := uppercaseWords[string(word[:numStart])]
if !numBroken {
_, match = uppercaseWords[string(word[:numStart])]
}
if !match {
for _, c := range word {
if found := c == 97 || c == 101 || c == 105 || c == 111 || c == 117 || c == 121; found {
vowels = true
break
}
}
}
if match || !vowels { if match || !vowels {
// Uppercase all a-z characters // Uppercase all a-z characters
@ -235,16 +205,8 @@ func TitleCase(n string) string {
} }
} }
} else { } else {
// If the first char is a-z, then uppercase it buf.WriteByte(word[0] - 32)
// by subtracting 32, and append the rest of the word. buf.Write(word[1:])
// Otherwise, it's probably a digit, so just append
// the whole word as is.
if word[0] > 96 && word[0] < 123 {
buf.WriteByte(word[0] - 32)
buf.Write(word[1:])
} else {
buf.Write(word)
}
} }
start = end + 1 start = end + 1

View file

@ -173,12 +173,10 @@ func TestTitleCase(t *testing.T) {
{"thing_zxc_stuff_vxz", "ThingZXCStuffVXZ"}, {"thing_zxc_stuff_vxz", "ThingZXCStuffVXZ"},
{"zxc_thing_vxz_stuff", "ZXCThingVXZStuff"}, {"zxc_thing_vxz_stuff", "ZXCThingVXZStuff"},
{"zxc_vdf9c9_hello9", "ZXCVDF9C9Hello9"}, {"zxc_vdf9c9_hello9", "ZXCVDF9C9Hello9"},
{"id9_uid911_guid9e9", "ID9UID911Guid9e9"}, {"id9_uid911_guid9e9", "ID9UID911GUID9E9"},
{"zxc_vdf0c0_hello0", "ZXCVDF0C0Hello0"}, {"zxc_vdf0c0_hello0", "ZXCVDF0C0Hello0"},
{"id0_uid000_guid0e0", "ID0UID000Guid0e0"}, {"id0_uid000_guid0e0", "ID0UID000GUID0E0"},
{"9id_zxc9d9", "9idZXC9D9"}, {"ab_5zxc5d5", "Ab5ZXC5D5"},
{"0id_zxc0d0", "0idZXC0D0"},
{"5id_zxc5d5", "5idZXC5D5"},
} }
for i, test := range tests { for i, test := range tests {