-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcases.go
More file actions
156 lines (138 loc) · 3.1 KB
/
cases.go
File metadata and controls
156 lines (138 loc) · 3.1 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
package stringx
import (
"strings"
"unicode"
)
// CamelCase converts a string to camel case.
func CamelCase(s string) string {
return studlyCase(s, false)
}
// PascalCase converts a string to pascal case.
func PascalCase(s string) string {
return studlyCase(s, true)
}
func studlyCase(s string, upperFirst bool) string {
if len(s) == 0 {
return s
}
var (
isFirst = true
afterSep = false
)
b := strings.Builder{}
for _, r := range s {
if r == '-' || r == '_' || unicode.IsSpace(r) {
afterSep = true
continue
}
if isFirst {
if upperFirst {
r = unicode.ToUpper(r)
} else {
r = unicode.ToLower(r)
}
isFirst = false
afterSep = false
} else if afterSep {
r = unicode.ToUpper(r)
afterSep = false
}
b.WriteRune(r)
}
return b.String()
}
// SnakeCase converts a string to snake case.
func SnakeCase(s string) string {
return snakeCase(s, '_')
}
// KebabCase converts a string to kebab case.
func KebabCase(s string) string {
return snakeCase(s, '-')
}
func snakeCase(s string, sep byte) string {
if len(s) == 0 {
return s
}
type runeKind int
const (
runeKindUnknown runeKind = iota
runeKindLower
runeKindSep
runeKindUpper
)
confirmRuneKine := func(r rune) runeKind {
if r == '-' || r == '_' || unicode.IsSpace(r) {
return runeKindSep
} else if unicode.IsUpper(r) {
return runeKindUpper
}
return runeKindLower
}
var (
prevRune rune
prevRuneKind runeKind
prevWriteKind runeKind
)
writeSep := func(b *strings.Builder, sep byte) {
if prevWriteKind != runeKindUnknown && prevWriteKind != runeKindSep {
b.WriteByte(sep)
prevWriteKind = runeKindSep
}
}
writeRune := func(b *strings.Builder, r rune, kind runeKind) {
b.WriteRune(r)
prevWriteKind = kind
}
b := strings.Builder{}
for _, r := range s {
kind := confirmRuneKine(r)
switch kind {
case runeKindLower:
if prevRuneKind == runeKindUnknown {
// do nothing
} else if prevRuneKind == runeKindLower {
// do nothing
} else if prevRuneKind == runeKindSep {
// do nothing
} else if prevRuneKind == runeKindUpper {
writeSep(&b, sep)
writeRune(&b, prevRune, prevRuneKind)
}
writeRune(&b, r, kind)
case runeKindSep:
if prevRuneKind == runeKindUnknown {
// do nothing
} else if prevRuneKind == runeKindLower {
writeSep(&b, sep)
} else if prevRuneKind == runeKindSep {
// do nothing
} else if prevRuneKind == runeKindUpper {
writeRune(&b, prevRune, prevRuneKind)
writeSep(&b, sep)
}
case runeKindUpper:
r = unicode.ToLower(r) // force lowercase
if prevRuneKind == runeKindUnknown {
// do nothing
} else if prevRuneKind == runeKindLower {
writeSep(&b, sep)
} else if prevRuneKind == runeKindSep {
// do nothing
} else if prevRuneKind == runeKindUpper {
writeRune(&b, prevRune, prevRuneKind)
}
}
prevRuneKind = kind
prevRune = r
}
// do not ignore the last uppercase character
if prevRuneKind == runeKindUpper {
writeRune(&b, prevRune, prevRuneKind)
}
// remove trailing separator
s = b.String()
if prevRuneKind == runeKindSep {
s = s[:len(s)-1]
}
return s
}