-
Notifications
You must be signed in to change notification settings - Fork 1
/
changecase.go
279 lines (252 loc) · 5.65 KB
/
changecase.go
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
package changecase
import (
"bytes"
"strings"
"unicode"
)
const (
INITIAL_STATE = iota
EXPECT_FOLLOWING_SMALL_LETTER = iota
IN_CONSECUTIVE_CAPITALS = iota
IN_WORD = iota
SEEK_FOR_NEXT_WORD = iota
)
type CaseTranslator struct {
FirstLetter func(rune) rune
LetterInWord func(rune) rune
FirstLetterOfWord func(rune) rune
Separator rune
}
type processor struct {
state int
buffer *bytes.Buffer
tr *CaseTranslator
bufferedRune rune
}
func passThrough(r rune) rune {
return r
}
func NewProcessor(t *CaseTranslator) *processor {
p := new(processor)
p.state = INITIAL_STATE
p.buffer = bytes.NewBuffer(nil)
p.tr = t
return p
}
func (p *processor) flushRuneBuffer() {
if p.bufferedRune != 0 {
p.writeRune(p.bufferedRune)
}
}
func (p *processor) putCharInRuneBuffer(r rune) {
if p.bufferedRune != 0 {
p.charInWord(p.bufferedRune)
}
p.bufferedRune = r
}
func (p *processor) writeRune(r rune) {
p.buffer.WriteRune(r)
}
func (p *processor) firstLetter(r rune) {
p.writeRune(p.tr.FirstLetter(r))
if unicode.IsUpper(r) {
p.state = EXPECT_FOLLOWING_SMALL_LETTER
} else {
p.state = IN_WORD
}
}
func (p *processor) foundNewWord(r rune) {
if p.tr.Separator != 0 {
p.writeRune(p.tr.Separator)
}
p.writeRune(p.tr.FirstLetterOfWord(r))
if unicode.IsUpper(r) {
p.state = EXPECT_FOLLOWING_SMALL_LETTER
} else {
p.state = IN_WORD
}
}
func (p *processor) charInWord(r rune) {
r = p.tr.LetterInWord(r)
p.writeRune(r)
}
func (p *processor) firstLetterOfWord(r rune) {
r = p.tr.FirstLetterOfWord(r)
p.writeRune(r)
}
func (p *processor) convert(s string) string {
p.buffer.Grow(len(s))
for _, r := range s {
isNumber := unicode.Is(unicode.Number, r)
isWord := unicode.Is(unicode.Letter, r) || isNumber
switch p.state {
case INITIAL_STATE:
if isWord {
p.firstLetter(r)
}
case EXPECT_FOLLOWING_SMALL_LETTER:
if isWord {
if unicode.IsUpper(r) {
p.putCharInRuneBuffer(r)
p.state = IN_CONSECUTIVE_CAPITALS
} else {
p.flushRuneBuffer()
p.charInWord(r)
p.state = IN_WORD
}
} else {
p.putCharInRuneBuffer(0)
p.state = SEEK_FOR_NEXT_WORD
}
case IN_CONSECUTIVE_CAPITALS:
if isWord {
if unicode.IsUpper(r) || isNumber {
p.putCharInRuneBuffer(r)
} else {
p.foundNewWord(p.bufferedRune)
p.bufferedRune = 0
p.charInWord(r)
p.state = IN_WORD
}
} else {
p.putCharInRuneBuffer(0)
p.state = SEEK_FOR_NEXT_WORD
}
case IN_WORD:
if isWord {
if unicode.IsUpper(r) {
p.foundNewWord(r)
} else {
p.charInWord(r)
}
} else {
p.state = SEEK_FOR_NEXT_WORD
}
case SEEK_FOR_NEXT_WORD:
if isWord {
p.foundNewWord(r)
}
}
}
if p.bufferedRune != 0 {
p.charInWord(p.bufferedRune)
}
return p.buffer.String()
}
func (p *processor) Convert(s string) string {
return p.convert(s)
}
func NewLowerProcessor(separator rune) *processor {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToLower,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToLower,
Separator: separator,
})
}
func Camel(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToLower,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToUpper,
}).Convert(s)
}
func Constant(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToUpper,
LetterInWord: unicode.ToUpper,
FirstLetterOfWord: unicode.ToUpper,
Separator: '_',
}).Convert(s)
}
func Dot(s string) string {
return NewLowerProcessor('.').Convert(s)
}
func Header(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToUpper,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToUpper,
Separator: '-',
}).Convert(s)
}
func IsLower(s string) bool {
for _, r := range s {
if !unicode.IsLower(r) {
return false
}
}
return true
}
func IsUpper(s string) bool {
for _, r := range s {
if !unicode.IsUpper(r) {
return false
}
}
return true
}
func Lower(s string) string {
return strings.ToLower(s)
}
func LcFirst(s string) string {
return strings.ToLower(s[0:1]) + s[1:len(s)]
}
func No(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToLower,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToLower,
Separator: ' ',
}).Convert(s)
}
func Param(s string) string {
return NewLowerProcessor('-').Convert(s)
}
func Pascal(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToUpper,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToUpper,
}).Convert(s)
}
func Path(s string) string {
return NewLowerProcessor('/').Convert(s)
}
func Sentence(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToUpper,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToLower,
Separator: ' ',
}).Convert(s)
}
func Snake(s string) string {
return NewLowerProcessor('_').Convert(s)
}
func Swap(s string) string {
b := bytes.NewBuffer(nil)
b.Grow(len(s))
for _, r := range s {
if unicode.IsUpper(r) {
b.WriteRune(unicode.ToLower(r))
} else {
b.WriteRune(unicode.ToUpper(r))
}
}
return b.String()
}
func Title(s string) string {
return NewProcessor(&CaseTranslator{
FirstLetter: unicode.ToUpper,
LetterInWord: unicode.ToLower,
FirstLetterOfWord: unicode.ToUpper,
Separator: ' ',
}).Convert(s)
}
func Upper(s string) string {
return strings.ToUpper(s)
}
func UcFirst(s string) string {
return strings.ToUpper(s[0:1]) + s[1:len(s)]
}