-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstring.go
46 lines (43 loc) · 872 Bytes
/
string.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
package gutil
import "strings"
// CamelString xx_yy to XxYy)
func CamelString(s string) string {
data := make([]byte, 0, len(s))
j := false
k := false
num := len(s) - 1
for i := 0; i <= num; i++ {
d := s[i]
if k == false && d >= 'A' && d <= 'Z' {
k = true
}
if d >= 'a' && d <= 'z' && (j || k == false) {
d = d - 32
j = false
k = true
}
if k && d == '_' && num > i && s[i+1] >= 'a' && s[i+1] <= 'z' {
j = true
continue
}
data = append(data, d)
}
return BytesToString(data[:])
}
// SnakeString XxYy to xx_yy
func SnakeString(s string) string {
data := make([]byte, 0, len(s)*2)
j := false
for _, d := range StringToBytes(s) {
if d >= 'A' && d <= 'Z' {
if j {
data = append(data, '_')
j = false
}
} else if d != '_' {
j = true
}
data = append(data, d)
}
return strings.ToLower(BytesToString(data))
}