-
Notifications
You must be signed in to change notification settings - Fork 0
/
case.go
107 lines (95 loc) · 1.68 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
package util
func IsUpperCase(r rune) bool {
if r >= 'A' && r <= 'Z' {
return true
}
return false
}
func IsLowerCase(r rune) bool {
if r >= 'a' && r <= 'z' {
return true
}
return false
}
func ToSnakeCase(s string) string {
out := []rune{}
for index, r := range s {
if index == 0 {
out = append(out, ToLowerCase(r))
continue
}
if IsUpperCase(r) && index != 0 {
if IsLowerCase(rune(s[index-1])) {
out = append(out, '_', ToLowerCase(r))
continue
}
if index < len(s)-1 && IsLowerCase(rune(s[index+1])) {
out = append(out, '_', ToLowerCase(r))
continue
}
out = append(out, ToLowerCase(r))
continue
}
out = append(out, r)
}
return string(out)
}
func ToCamelCase(s string) string {
s = ToLower(s)
out := []rune{}
for index, r := range s {
if r == '_' {
continue
}
if index == 0 {
out = append(out, ToUpperCase(r))
continue
}
if index > 0 && s[index-1] == '_' {
out = append(out, ToUpperCase(r))
continue
}
out = append(out, r)
}
return string(out)
}
func ToLowerCase(r rune) rune {
dx := 'A' - 'a'
if IsUpperCase(r) {
return r - dx
}
return r
}
func ToUpperCase(r rune) rune {
dx := 'A' - 'a'
if IsLowerCase(r) {
return r + dx
}
return r
}
func ToLower(s string) string {
out := []rune{}
for _, r := range s {
out = append(out, ToLowerCase(r))
}
return string(out)
}
func ToUpper(s string) string {
out := []rune{}
for _, r := range s {
out = append(out, ToUpperCase(r))
}
return string(out)
}
func LowerFirst(s string) string {
if len(s) == 0 {
return s
}
return ToLower(s[:1]) + s[1:]
}
func UpperFirst(s string) string {
if len(s) == 0 {
return s
}
return ToUpper(s[:1]) + s[1:]
}