Skip to content

Commit 65f5a07

Browse files
authored
Merge pull request #56 from SasSwart/bugfix/toTitle
Remove pesky while loop from toTitle
2 parents e69cdf7 + ed7905b commit 65f5a07

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

render/go/renderer.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ func isAlphaNum(r rune) bool {
130130
return isAlpha(r) || ('0' <= r && r <= '9')
131131
}
132132

133+
func flatten[T any](nestedList [][]T) []T {
134+
var flattenedList []T
135+
for _, subList := range nestedList {
136+
for _, innerMostElement := range subList {
137+
flattenedList = append(flattenedList, innerMostElement)
138+
}
139+
}
140+
return flattenedList
141+
}
142+
133143
func ToTitle(s string) (ret string) {
134144
caser := cases.Title(language.English)
135145
var splitBy []rune
@@ -139,27 +149,12 @@ func ToTitle(s string) (ret string) {
139149
}
140150
}
141151
buf := []string{s}
142-
for 0 < len(splitBy) {
143-
for i, word := range buf {
144-
if strings.HasPrefix(word, string(splitBy[0])) {
145-
buf[i] = strings.TrimPrefix(word, string(splitBy[0]))
146-
splitBy = splitBy[1:]
147-
continue
148-
}
149-
if strings.HasSuffix(word, string(splitBy[0])) {
150-
buf[i] = strings.TrimSuffix(word, string(splitBy[0]))
151-
splitBy = splitBy[1:]
152-
continue
153-
}
154-
if strings.Contains(word, string(splitBy[0])) {
155-
if len(buf) == 1 {
156-
buf = strings.Split(word, string(splitBy[0]))
157-
} else {
158-
buf = append(buf[:i], strings.Split(word, string(splitBy[0]))...)
159-
}
160-
splitBy = splitBy[1:]
161-
}
152+
for _, delim := range splitBy {
153+
splitbuf := make([][]string, 0)
154+
for _, word := range buf {
155+
splitbuf = append(splitbuf, strings.Split(word, string(delim)))
162156
}
157+
buf = flatten[string](splitbuf)
163158
}
164159
for _, word := range buf {
165160
ret += caser.String(word)

0 commit comments

Comments
 (0)