-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathconvert.go
More file actions
355 lines (325 loc) · 8.8 KB
/
convert.go
File metadata and controls
355 lines (325 loc) · 8.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package strcase
import (
"strings"
"unicode"
)
// WordCase is an enumeration of the ways to format a word.
// The first 16 bits are base casers
// The second 16 bits are options
type WordCase int
const (
// Original - Preserve the original input strcase
Original WordCase = iota
// LowerCase - All letters lower cased (example)
LowerCase
// UpperCase - All letters upper cased (EXAMPLE)
UpperCase
// TitleCase - Only first letter upper cased (Example)
TitleCase
// CamelCase - TitleCase except lower case first word (exampleText)
// Notably, even if the first word is an initialism, it will be lower
// cased. This is important for code generators where capital letters
// mean exported functions. i.e. jsonString(), not JSONString()
//
// Use CamelCase|InitialismFirstWord (see options below) if you want to
// have initialisms like JSONString
CamelCase
)
const (
wordCaseMask = 0xFFFF
// InitialismFirstWord will allow CamelCase to start with an upper case
// letter if it is an initialism. Only impacts CamelCase.
// LowerCase will initialize all specified initialisms, regardless of position.
//
// e.g ToGoCase("jsonString", CamelCase|InitialismFirstWord, 0) == "JSONString"
InitialismFirstWord WordCase = 1 << 17
// PreserveInitialism will treat any capitalized words as initialisms
// If the entire word is all upper case, keep them upper case.
//
// Note that you may also use InitialismFirstWord with PreserveInitialism
// e.g. CamelCase|InitialismFirstWord|PreserveInitialism: NASA-rocket -> NASARocket
// e.g. CamelCase|PreserveInitialism: NASA-rocket -> nasaRocket
//
// Works for LowerCase, TitleCase, and CamelCase. No impact on Original
// and UpperCase.
//
// Not recommended when the input is in SCREAMING_SNAKE_CASE
// as all words will be treated as initialisms.
PreserveInitialism WordCase = 1 << 16
)
// We have 3 convert functions for performance reasons
// The general convert could handle everything, but is not optimized
//
// The other two functions are optimized for the general use cases - that is the non-custom caser functions
// Case 1: Any Case and supports Go Initialisms
// Case 2: UpperCase words, which don't need to support initialisms since everything is in upper case
// convertWithoutInitialims only works for to UpperCase and LowerCase
//
//nolint:gocyclo
func convertWithoutInitialisms(input string, delimiter rune, wordCase WordCase) string {
input = strings.TrimSpace(input)
runes := []rune(input)
if len(runes) == 0 {
return ""
}
var b strings.Builder
b.Grow(len(input) + 4) // In case we need to write delimiters where they weren't before
var prev, curr rune
next := runes[0] // 0 length will have already returned so safe to index
inWord := false
firstWord := true
for i := 0; i < len(runes); i++ {
prev = curr
curr = next
if i+1 == len(runes) {
next = 0
} else {
next = runes[i+1]
}
switch defaultSplitFn(prev, curr, next) {
case SkipSplit:
if inWord && delimiter != 0 {
b.WriteRune(delimiter)
}
inWord = false
continue
case Split:
if inWord && delimiter != 0 {
b.WriteRune(delimiter)
}
inWord = false
}
switch wordCase & wordCaseMask {
case UpperCase:
b.WriteRune(toUpper(curr))
case LowerCase:
b.WriteRune(toLower(curr))
case TitleCase:
if inWord {
b.WriteRune(toLower(curr))
} else {
b.WriteRune(toUpper(curr))
}
case CamelCase:
if inWord {
b.WriteRune(toLower(curr))
} else if firstWord {
b.WriteRune(toLower(curr))
firstWord = false
} else {
b.WriteRune(toUpper(curr))
}
default:
// Must be original case
b.WriteRune(curr)
}
inWord = true
}
return b.String()
}
// convertWithGoInitialisms changes a input string to a certain case with a
// delimiter, respecting go initialisms but not skip runes
//
//nolint:gocyclo
func convertWithGoInitialisms(input string, delimiter rune, wordCase WordCase) string {
input = strings.TrimSpace(input)
runes := []rune(input)
if len(runes) == 0 {
return ""
}
var b strings.Builder
b.Grow(len(input) + 4) // In case we need to write delimiters where they weren't before
firstWord := true
addWord := func(start, end int) {
if start == end {
return
}
if !firstWord && delimiter != 0 {
b.WriteRune(delimiter)
}
// Don't bother with initialisms if the word is longer than 5
// A quick proxy to avoid the extra memory allocations
if end-start <= 5 {
var word strings.Builder
word.Grow(end - start)
for i := start; i < end; i++ {
word.WriteRune(toUpper(runes[i]))
}
w := word.String()
if golintInitialisms[w] {
if !firstWord || wordCase&wordCaseMask != CamelCase || wordCase&InitialismFirstWord != 0 {
b.WriteString(w)
firstWord = false
return
}
}
}
for i := start; i < end; i++ {
r := runes[i]
switch wordCase & wordCaseMask {
case UpperCase:
panic("use convertWithoutInitialisms instead")
case LowerCase:
b.WriteRune(toLower(r))
case TitleCase:
if i == start {
b.WriteRune(toUpper(r))
} else {
b.WriteRune(toLower(r))
}
case CamelCase:
if !firstWord && i == start {
b.WriteRune(toUpper(r))
} else {
b.WriteRune(toLower(r))
}
default:
b.WriteRune(r)
}
}
firstWord = false
}
var prev, curr rune
next := runes[0] // 0 length will have already returned so safe to index
wordStart := 0
for i := 0; i < len(runes); i++ {
prev = curr
curr = next
if i+1 == len(runes) {
next = 0
} else {
next = runes[i+1]
}
switch defaultSplitFn(prev, curr, next) {
case Split:
addWord(wordStart, i)
wordStart = i
case SkipSplit:
addWord(wordStart, i)
wordStart = i + 1
}
}
if wordStart != len(runes) {
addWord(wordStart, len(runes))
}
return b.String()
}
// convert changes a input string to a certain case with a delimiter,
// respecting arbitrary initialisms and skip characters
//
//nolint:gocyclo
func convert(input string, fn SplitFn, delimiter rune, wordCase WordCase,
initialisms map[string]bool) string {
input = strings.TrimSpace(input)
runes := []rune(input)
if len(runes) == 0 {
return ""
}
var b strings.Builder
b.Grow(len(input) + 4) // In case we need to write delimiters where they weren't before
firstWord := true
var skipIndexes []int
addWord := func(start, end int) {
// If you have nothing good to say, say nothing at all
if start == end || len(skipIndexes) == end-start {
skipIndexes = nil
return
}
// If you have something to say, start with a delimiter
if !firstWord && delimiter != 0 {
b.WriteRune(delimiter)
}
// Check if you're an initialism
// Note - we don't check skip characters here since initialisms
// will probably never have junk characters in between
// I'm open to it if there is a use case
if initialisms != nil {
var word strings.Builder
word.Grow(end - start)
for i := start; i < end; i++ {
word.WriteRune(toUpper(runes[i]))
}
w := word.String()
if initialisms[w] {
if !firstWord || wordCase&wordCaseMask != CamelCase || wordCase&InitialismFirstWord != 0 {
b.WriteString(w)
firstWord = false
return
}
}
}
// If we're preserving initialism, check to see if the entire word is
// an initialism.
// Note we don't support preserving initialisms if they are followed
// by a number and we're not spliting before numbers
if !firstWord || wordCase&InitialismFirstWord != 0 || wordCase&wordCaseMask != CamelCase {
if wordCase&PreserveInitialism != 0 {
allCaps := true
for i := start; i < end; i++ {
allCaps = allCaps && (isUpper(runes[i]) || !unicode.IsLetter(runes[i]))
}
if allCaps {
b.WriteString(string(runes[start:end]))
firstWord = false
return
}
}
}
skipIdx := 0
for i := start; i < end; i++ {
if len(skipIndexes) > 0 && skipIdx < len(skipIndexes) && i == skipIndexes[skipIdx] {
skipIdx++
continue
}
r := runes[i]
switch wordCase & wordCaseMask {
case UpperCase:
b.WriteRune(toUpper(r))
case LowerCase:
b.WriteRune(toLower(r))
case TitleCase:
if i == start {
b.WriteRune(toUpper(r))
} else {
b.WriteRune(toLower(r))
}
case CamelCase:
if !firstWord && i == start {
b.WriteRune(toUpper(r))
} else {
b.WriteRune(toLower(r))
}
default:
b.WriteRune(r)
}
}
firstWord = false
skipIndexes = nil
}
var prev, curr rune
next := runes[0] // 0 length will have already returned so safe to index
wordStart := 0
for i := 0; i < len(runes); i++ {
prev = curr
curr = next
if i+1 == len(runes) {
next = 0
} else {
next = runes[i+1]
}
switch fn(prev, curr, next) {
case Skip:
skipIndexes = append(skipIndexes, i)
case Split:
addWord(wordStart, i)
wordStart = i
case SkipSplit:
addWord(wordStart, i)
wordStart = i + 1
}
}
if wordStart != len(runes) {
addWord(wordStart, len(runes))
}
return b.String()
}