-
Notifications
You must be signed in to change notification settings - Fork 100
/
strings.go
273 lines (242 loc) · 5.51 KB
/
strings.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package compiler
import (
"bytes"
"net/url"
"path"
"strings"
"unicode"
"github.com/NYTimes/openapi2proto/openapi"
)
// since we're not considering unicode here, we're not using unicode.*
func isAlphaNum(r rune) bool {
return (r >= 0x41 && r <= 0x5a) || // A-Z
(r >= 0x61 && r <= 0x7a) || // a-z
(r >= 0x30 && r <= 0x39) // 0-9
}
func allCaps(s string) string {
var buf bytes.Buffer
for _, r := range s {
// replace all non-alpha-numeric characters with an underscore
if !isAlphaNum(r) {
r = '_'
} else {
r = unicode.ToUpper(r)
}
buf.WriteRune(r)
}
return buf.String()
}
func normalizeFieldName(s string) string {
var wasUnderscore bool
var buf bytes.Buffer
for _, r := range s {
if !isAlphaNum(r) {
if !wasUnderscore {
buf.WriteRune('_')
}
wasUnderscore = true
continue
}
wasUnderscore = false
buf.WriteRune(r)
}
return buf.String()
}
func dedupe(s string, r rune) string {
var buf bytes.Buffer
var wasTarget bool
for _, r1 := range s {
if r1 == r {
if !wasTarget {
buf.WriteRune(r1)
wasTarget = true
}
continue
}
wasTarget = false
buf.WriteRune(r1)
}
return buf.String()
}
func removeNonAlphaNum(s string) string {
var buf bytes.Buffer
for _, r := range s {
if !isAlphaNum(r) {
switch r {
case '_', '-', ' ':
r = '_'
default:
continue
}
}
buf.WriteRune(r)
}
return buf.String()
}
func snakeCase(s string) string {
var wasUnderscore bool
// pass 1: remove all non-alpha-numeric characters EXCEPT for underscore
s = removeNonAlphaNum(s)
// pass 2: dedupe '_'
s = dedupe(s, '_')
var runes []rune
for _, r := range s {
if !isAlphaNum(r) {
if !wasUnderscore {
runes = append(runes, '_')
wasUnderscore = true
}
continue
}
wasUnderscore = false
runes = append(runes, r)
}
for len(runes) > 0 && runes[0] == '_' {
runes = runes[1:]
}
for len(runes) > 0 && runes[len(runes)-1] == '_' {
runes = runes[:len(runes)-1]
}
// pass 2: for each consecutive upper case characters, insert
// a break between the last upper case character and its
// predecessor
var wasUpper int
var buf bytes.Buffer
for i := 0; i < len(runes); i++ {
if !isAlphaNum(runes[i]) {
if !wasUnderscore {
buf.WriteRune('_')
wasUnderscore = true
}
continue
}
// if it's upper cased, check if we have a succession of
// uppercase letters.
if unicode.IsUpper(runes[i]) {
if wasUpper == 0 && buf.Len() > 0 && !wasUnderscore {
buf.WriteRune('_')
}
wasUpper++
} else {
if wasUpper > 1 && buf.Len() != 1 {
if len(runes) > 1 && runes[i-2] != '_' && runes[i-1] != '_' {
buf.Truncate(buf.Len() - 1) // remove last upper case letter
buf.WriteRune('_') // insert rune
buf.WriteRune(unicode.ToLower(runes[i-1])) // re-insert last letter
}
}
wasUpper = 0
}
wasUnderscore = false
buf.WriteRune(unicode.ToLower(runes[i]))
}
return buf.String()
}
func camelCase(s string) string {
var first = true
var wasUnderscore bool
var buf bytes.Buffer
for _, r := range s {
// replace all non-alpha-numeric characters with an underscore
if !isAlphaNum(r) {
r = '_'
}
if r == '_' {
wasUnderscore = true
continue
}
if first || wasUnderscore {
r = unicode.ToUpper(r)
}
first = false
wasUnderscore = false
buf.WriteRune(r)
}
return buf.String()
}
// takes strings like "foo bar baz" and turns it into "foobarbaz"
// if title is true, then "FooBarBaz"
func concatSpaces(s string, title bool) string {
var buf bytes.Buffer
var wasSpace bool
for i, r := range s {
if unicode.IsSpace(r) {
wasSpace = true
continue
}
if i == 0 || (wasSpace && title) {
r = unicode.ToUpper(r)
}
buf.WriteRune(r)
wasSpace = false
}
return buf.String()
}
func packageName(s string) string {
return cleanCharacters(strings.ToLower(concatSpaces(s, false)))
}
func normalizeServiceName(s string) string {
return camelCase(concatSpaces(s, true) + "Service")
}
func cleanCharacters(input string) string {
var buf bytes.Buffer
for _, r := range input {
// anything other than a-z, A-Z, 0-9 should be converted
// to an underscore
if !isAlphaNum(r) {
r = '_'
}
buf.WriteRune(r)
}
return buf.String()
}
func normalizeEndpointName(e *openapi.Endpoint) string {
if opID := e.OperationID; len(opID) > 0 {
return operationIDToName(opID)
}
// Strip out file suffix
p := strings.TrimSuffix(e.Path, path.Ext(e.Path))
// Strip query strings. Note that query strings are illegal
// in swagger paths, but some tooling seems to tolerate them.
if i := strings.LastIndexByte(p, '?'); i > 0 {
p = p[:i]
}
var buf bytes.Buffer
for _, r := range p {
switch r {
case '_', '-', '.', '/':
// turn these into spaces
r = ' '
case '{', '}', '[', ']', '(', ')':
// Strip out illegal-for-identifier characters in the path
// (XXX Shouldn't we be white-listing this instead of
// removing black-listed characters?)
continue
}
buf.WriteRune(r)
}
var name = camelCase(buf.String())
return camelCase(e.Verb) + name
}
func looksLikeInteger(s string) bool {
for _, r := range s {
// charcter should be between "0" to "9" or else.
if 0x30 > r || 0x39 < r {
return false
}
}
return true
}
func normalizeEnumName(s string) string {
s = strings.Replace(s, "&", " AND ", -1)
// XXX This is a special case for things like
// N.Y.%20%2F%20Region
if v, err := url.QueryUnescape(s); err == nil {
s = v
}
s = allCaps(snakeCase(s))
return s
}
func operationIDToName(s string) string {
return camelCase(snakeCase(s))
}