aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ettle/strcase/convert.go
blob: 70fedb1449a7842b48330adb890438d2fa078ae1 (plain)
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
package strcase

import "strings"

// WordCase is an enumeration of the ways to format a word.
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()
	CamelCase
)

// 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) * 2) // 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 {
		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 = 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) * 2) // 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 {
			key := strings.ToUpper(string(runes[start:end]))
			if golintInitialisms[key] {
				if !firstWord || wordCase != CamelCase {
					b.WriteString(key)
					firstWord = false
					return
				}
			}
		}

		for i := start; i < end; i++ {
			r := runes[i]
			switch wordCase {
			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) * 2) // 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
			for i := start; i < end; i++ {
				word.WriteRune(toUpper(runes[i]))
			}
			key := word.String()
			if initialisms[key] {
				if !firstWord || wordCase != CamelCase {
					b.WriteString(key)
					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 {
			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()
}