/
kits.go
100 lines (89 loc) · 1.72 KB
/
kits.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
package tools
// ///////////////////////////////////////////see github.com/go-pg/pg/internal
func isUpper(c byte) bool {
return c >= 'A' && c <= 'Z'
}
func isLower(c byte) bool {
return c >= 'a' && c <= 'z'
}
func toUpper(c byte) byte {
return c - 32
}
func toLower(c byte) byte {
return c + 32
}
// Underscore converts "CamelCasedString" to "camel_cased_string".
// Just work for ASCⅡ code
func Underscore(s string) string {
r := make([]byte, 0, len(s)+5)
for i := 0; i < len(s); i++ {
c := s[i]
if isUpper(c) {
if i > 0 && i+1 < len(s) && (isLower(s[i-1]) || isLower(s[i+1])) {
r = append(r, '_', toLower(c))
} else {
r = append(r, toLower(c))
}
} else {
r = append(r, c)
}
}
return string(r)
}
// CamelCased Just work for ASCⅡ code
func CamelCased(s string) string {
r := make([]byte, 0, len(s))
upperNext := true
for i := 0; i < len(s); i++ {
c := s[i]
if c == '_' {
upperNext = true
continue
}
if upperNext {
if isLower(c) {
c = toUpper(c)
}
upperNext = false
}
r = append(r, c)
}
return string(r)
}
// ToExported Just work for ASCⅡ code
func ToExported(s string) string {
if len(s) == 0 {
return s
}
if c := s[0]; isLower(c) {
b := []byte(s)
b[0] = toUpper(c)
return string(b)
}
return s
}
// UpperString Just work for ASCⅡ code
func UpperString(s string) string {
if isUpperString(s) {
return s
}
b := make([]byte, len(s))
for i := range b {
c := s[i]
if isLower(c) {
c = toUpper(c)
}
b[i] = c
}
return string(b)
}
func isUpperString(s string) bool {
for i := 0; i < len(s); i++ {
c := s[i]
if isLower(c) {
return false
}
}
return true
}
/////////////////////////////////////////////see github.com/go-pg/pg/internal