-
Notifications
You must be signed in to change notification settings - Fork 27
/
casing.go
140 lines (129 loc) · 3.32 KB
/
casing.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
package casing
import (
"strings"
"unicode"
"unicode/utf8"
)
// Caser converts strings from camel_case to UpperCamelCase.
type Caser struct {
acronyms map[string]string
}
func NewCaser() Caser {
return Caser{
acronyms: map[string]string{},
}
}
// AddAcronyms adds each acronym that's specially handled in conversion
// routines.
func (cs Caser) AddAcronyms(acros map[string]string) {
for a, b := range acros {
cs.acronyms[a] = b
}
}
// AddAcronym adds an acronym that's specially handled in conversion routines.
func (cs Caser) AddAcronym(str, acronym string) {
cs.acronyms[str] = acronym
}
// ToUpperGoIdent converts a string into a legal, capitalized Go identifier,
// respecting registered acronyms. Returns the empty string if no conversion
// is possible.
func (cs Caser) ToUpperGoIdent(s string) string {
san := sanitize(s)
if san == "" {
return ""
}
return cs.convert(san, cs.appendUpperCamel)
}
// ToLowerGoIdent converts a string into a legal, uncapitalized Go identifier,
// respecting registered acronyms. Returns the empty string if no conversion
// is possible.
func (cs Caser) ToLowerGoIdent(s string) string {
san := sanitize(s)
if san == "" {
return ""
}
con := cs.convert(san, cs.appendLowerCamel)
switch con {
case "func", "interface", "select", "case", "defer", "go", "map", "struct",
"chan", "else", "goto", "package", "switch", "const", "fallthrough", "if",
"range", "type", "continue", "for", "import", "return", "var":
return con + "_"
default:
return con
}
}
type converter func(*strings.Builder, []byte)
// convert converts a string using converter for each sub-word while
// respecting the registered acronyms.
func (cs Caser) convert(s string, converter converter) string {
s = strings.TrimSpace(s)
if len(s) == 0 {
return s
}
sb := &strings.Builder{}
sb.Grow(len(s))
chars := []byte(s)
lo := 0
isUpSpan := false
for hi := 0; hi < len(chars); {
ch, size := utf8.DecodeRune(chars[hi:])
switch {
case ch == '_':
isUpSpan = false
converter(sb, chars[lo:hi])
lo = hi + size // skip underscore
case unicode.IsUpper(ch):
isUpSpan = lo+1 == hi || isUpSpan
if !isUpSpan {
converter(sb, chars[lo:hi])
lo = hi
}
default:
isUpSpan = false
}
hi += size
}
converter(sb, chars[lo:])
return sb.String()
}
func (cs Caser) appendUpperCamel(sb *strings.Builder, chars []byte) {
if len(chars) == 0 {
return
}
if a, ok := cs.acronyms[strings.ToLower(string(chars))]; ok {
sb.WriteString(a)
return
}
firstCh, size := utf8.DecodeRune(chars)
sb.WriteRune(unicode.ToUpper(firstCh))
sb.Write(chars[size:])
}
func (cs Caser) appendLowerCamel(sb *strings.Builder, chars []byte) {
if len(chars) == 0 {
return
}
isFirst := sb.Len() == 0
if a, ok := cs.acronyms[strings.ToLower(string(chars))]; ok {
if isFirst {
// First word should be uncapitalized. We don't know exactly how to do
// that, so assume lower casing the acronym is sufficient.
sb.WriteString(strings.ToLower(a))
} else {
sb.WriteString(a)
}
return
}
firstCh, size := utf8.DecodeRune(chars)
if isFirst {
sb.WriteRune(unicode.ToLower(firstCh))
// Lowercase rest of first word.
for i := size; i < len(chars); {
ch, n := utf8.DecodeRune(chars[i:])
sb.WriteRune(unicode.ToLower(ch))
i += n
}
} else {
sb.WriteRune(unicode.ToUpper(firstCh))
sb.Write(chars[size:])
}
}