/
case.go
179 lines (159 loc) · 3.99 KB
/
case.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
package strUtil
import (
"regexp"
"strings"
)
// 首字母大写
func UcFirst(s string) string {
n := len(s)
if n == 0 {
return s
} else if n == 1 {
return strings.ToUpper(s)
} else {
c := s[:1]
if u := strings.ToUpper(c); u == c {
return s
} else {
return u + s[1:]
}
}
}
// 首字母小写
func LcFirst(s string) string {
n := len(s)
if n == 0 {
return s
} else if n == 1 {
return strings.ToLower(s)
} else {
c := s[:1]
if u := strings.ToLower(c); u == c {
return s
} else {
return u + s[1:]
}
}
}
// 将驼峰命名法转换为首字母大写中划线分割格式(Http Header 格式)
func CamelToUpperKebab(s string) string {
for _, ss := range camelToKebabPattern.FindAllString(s, -1) {
s = strings.Replace(s, ss, ss[:1]+"-"+ss[1:], -1)
}
return UcFirst(s)
}
// 将驼峰命名法转换为小写下划线分割格式
func CamelToSnake(s string) string {
return PascalToSnake(s)
}
// 将 Pascal 命名法转换为小写下划线分割格式
func PascalToSnake(s string) string {
arr := caseSplit(s, pascalSplit, strings.ToLower)
return strings.Join(arr, "_")
}
func pascalSplit(prevCase, currentCase, nextCase letterCase) (split bool) {
if prevCase == upperCase {
return currentCase == upperCase && nextCase == lowerCase
} else if currentCase == upperCase {
return true
}
return false
}
// 将驼峰命名法转换为小写中划线分割格式
func CamelToLowerKebab(s string) string {
for _, ss := range camelToKebabPattern.FindAllString(s, -1) {
s = strings.Replace(s, ss, ss[:1]+"-"+ss[1:], -1)
}
return strings.ToLower(s)
}
var camelToKebabPattern = regexp.MustCompile("[a-z][A-Z]")
// 将中划线分割格式转换为驼峰命名法
func KebabToCamel(s string) string {
arr := strings.Split(s, "-")
for i, s := range arr {
if i == 0 {
arr[i] = LcFirst(s)
} else {
arr[i] = UcFirst(s)
}
}
return strings.Join(arr, "")
}
// 将中划线分割格式转换为下划线小写格式
func KebabToSnake(s string) string {
return strings.ToLower(strings.Replace(s, "-", "_", -1))
}
// 下划线小写格式转换为驼峰命名法
func SnakeToCamel(s string) string {
if s != "" {
arr := strings.Split(s, "_")
return strings.Join(Map(arr, func(i int, s string) string {
if i == 0 {
return LcFirst(s)
} else {
return UcFirst(s)
}
}), "")
}
return ""
}
// 下划线小写格式转换为 Pascal 格式
func SnakeToPascal(s string) string {
if s != "" {
arr := strings.Split(s, "_")
return strings.Join(Map(arr, func(i int, s string) string {
return UcFirst(s)
}), "")
}
return ""
}
// 下划线小写格式转换为首字母大写中划线分割格式(Http Header 格式)
func SnakeToUpperKebab(s string) string {
if s != "" {
arr := strings.Split(s, "_")
return strings.Join(Map(arr, func(i int, s string) string {
return UcFirst(s)
}), "-")
}
return ""
}
type letterCase int
const (
otherCase = letterCase(0)
upperCase = letterCase(1)
lowerCase = letterCase(2)
)
func caseSplit(str string, split func(prevCase, currentCase, nextCase letterCase) (split bool), format func(s string) string) []string {
if format == nil {
format = func(s string) string { return s }
}
length := len(str)
if length == 0 {
return []string{}
} else if length == 1 {
return []string{format(str)}
}
getCase := func(r byte) letterCase {
if r >= 'a' && r <= 'z' {
return lowerCase
} else if r >= 'A' && r <= 'Z' {
return upperCase
}
return otherCase
}
result, lastSplitPos := make([]string, 0, 4), 0
prevCase, currentCase := getCase(str[0]), getCase(str[1])
for i, n := 1, length-1; i < n; i++ {
nextCase := getCase(str[i+1])
if split(prevCase, currentCase, nextCase) {
result, lastSplitPos = append(result, format(str[lastSplitPos:i])), i
}
prevCase, currentCase = currentCase, nextCase
}
if split(prevCase, currentCase, otherCase) {
result = append(result, format(str[lastSplitPos:length-1]), format(str[length-1:]))
} else {
result = append(result, format(str[lastSplitPos:]))
}
return result
}