forked from libgo/mysqlx
/
mapper.go
79 lines (63 loc) · 1.16 KB
/
mapper.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
package mysqlx
// mapper conver snakecase to related upper case.
func snakecase(s string) string {
b := make([]byte, 0, 64)
l := len(s)
i := 0
// loop until we reached the end of the string
for i < l {
// skip leading bytes that aren't letters or numbers
for i < l && !isWord(s[i]) {
i++
}
if i < l && len(b) != 0 {
b = append(b, '_')
}
// Append all leading uppercase or digits
for i < l {
c := s[i]
if !isHead(c) {
break
}
b = append(b, toLower(c))
i++
}
// Append all trailing lowercase or digits
for i < l {
c := s[i]
if !isTail(c) {
break
}
b = append(b, c)
i++
}
}
return string(b)
}
func isHead(c byte) bool {
return isUpper(c) || isDigit(c)
}
func isTail(c byte) bool {
return isLower(c) || isDigit(c)
}
func isWord(c byte) bool {
return isLetter(c) || isDigit(c)
}
func isLetter(c byte) bool {
return isLower(c) || isUpper(c)
}
func isUpper(c byte) bool {
return c >= 'A' && c <= 'Z'
}
func isLower(c byte) bool {
return c >= 'a' && c <= 'z'
}
func isDigit(c byte) bool {
return c >= '0' && c <= '9'
}
func toLower(c byte) byte {
if isUpper(c) {
return c + ('a' - 'A')
}
return c
}