-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathheck.go
348 lines (301 loc) · 7.45 KB
/
heck.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package bin
import (
"strings"
"unicode"
)
// Ported from https://github.com/withoutboats/heck
// https://github.com/withoutboats/heck/blob/master/LICENSE-APACHE
// https://github.com/withoutboats/heck/blob/master/LICENSE-MIT
// ToPascalCase converts a string to upper camel case.
func ToPascalCase(s string) string {
return transform(
s,
capitalize,
func(*strings.Builder) {},
)
}
func transform(
s string,
with_word func(string, *strings.Builder),
boundary func(*strings.Builder),
) string {
builder := new(strings.Builder)
first_word := true
words := splitIntoWords(s)
for _, word := range words {
char_indices := newReader(word)
init := 0
mode := _Boundary
for char_indices.Move() {
i, c := char_indices.This()
// Skip underscore characters
if c == '_' {
if init == i {
init += 1
}
continue
}
if next_i, next := char_indices.Peek(); next_i != -1 {
// The mode including the current character, assuming the
// current character does not result in a word boundary.
next_mode := func() _WordMode {
if unicode.IsLower(c) {
return _Lowercase
} else if unicode.IsUpper(c) {
return _Uppercase
} else {
return mode
}
}()
// Word boundary after if next is underscore or current is
// not uppercase and next is uppercase
if next == '_' || (next_mode == _Lowercase && unicode.IsUpper(next)) {
if !first_word {
// boundary(f)?;
boundary(builder)
}
{
// with_word(&word[init..next_i], f)?;
with_word(word[init:next_i], builder)
}
first_word = false
init = next_i
mode = _Boundary
// Otherwise if current and previous are uppercase and next
// is lowercase, word boundary before
} else if mode == _Uppercase && unicode.IsUpper(c) && unicode.IsLower(next) {
if !first_word {
// boundary(f)?;
boundary(builder)
} else {
first_word = false
}
{
// with_word(&word[init..i], f)?;
with_word(word[init:i], builder)
}
init = i
mode = _Boundary
// Otherwise no word boundary, just update the mode
} else {
mode = next_mode
}
} else {
// Collect trailing characters as a word
if !first_word {
// boundary(f)?;
boundary(builder)
} else {
first_word = false
}
{
// with_word(&word[init..], f)?;
with_word(word[init:], builder)
}
break
}
}
}
return builder.String()
}
// fn capitalize(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
// let mut char_indices = s.char_indices();
// if let Some((_, c)) = char_indices.next() {
// write!(f, "{}", c.to_uppercase())?;
// if let Some((i, _)) = char_indices.next() {
// lowercase(&s[i..], f)?;
// }
// }
// Ok(())
// }
func capitalize(s string, f *strings.Builder) {
char_indices := newReader(s)
if i, c := char_indices.Next(); i != -1 {
f.WriteString(strings.ToUpper(string(c)))
if i, _ := char_indices.Next(); i != -1 {
lowercase(s[i:], f)
}
}
}
// fn lowercase(s: &str, f: &mut fmt::Formatter) -> fmt::Result {
// let mut chars = s.chars().peekable();
// while let Some(c) = chars.next() {
// if c == 'Σ' && chars.peek().is_none() {
// write!(f, "ς")?;
// } else {
// write!(f, "{}", c.to_lowercase())?;
// }
// }
// Ok(())
// }
func lowercase(s string, f *strings.Builder) {
chars := newReader(s)
for chars.Move() {
_, c := chars.This()
if c == 'Σ' && chars.PeekNext() == 0 {
f.WriteString("ς")
} else {
f.WriteString(strings.ToLower(string(c)))
}
}
}
func ToSnakeForSighash(s string) string {
return ToRustSnakeCase(s)
}
type reader struct {
runes []rune
index int
}
func splitStringByRune(s string) []rune {
var res []rune
iterateStringAsRunes(s, func(r rune) bool {
res = append(res, r)
return true
})
return res
}
func iterateStringAsRunes(s string, callback func(r rune) bool) {
for _, rn := range s {
doContinue := callback(rn)
if !doContinue {
return
}
}
}
func newReader(s string) *reader {
return &reader{
runes: splitStringByRune(s),
index: -1,
}
}
func (r reader) This() (int, rune) {
return r.index, r.runes[r.index]
}
func (r reader) HasNext() bool {
return r.index < len(r.runes)-1
}
func (r *reader) Next() (int, rune) {
if r.HasNext() {
r.index++
return r.index, r.runes[r.index]
}
return -1, rune(0)
}
func (r reader) Peek() (int, rune) {
if r.HasNext() {
return r.index + 1, r.runes[r.index+1]
}
return -1, rune(0)
}
func (r reader) PeekNext() rune {
if r.HasNext() {
return r.runes[r.index+1]
}
return rune(0)
}
func (r *reader) Move() bool {
if r.HasNext() {
r.index++
return true
}
return false
}
// #[cfg(not(feature = "unicode"))]
func splitIntoWords(s string) []string {
parts := strings.FieldsFunc(s, func(r rune) bool {
return !(unicode.IsLetter(r) || unicode.IsDigit(r))
})
return parts
}
type _WordMode int
const (
/// There have been no lowercase or uppercase characters in the current
/// word.
_Boundary _WordMode = iota
/// The previous cased character in the current word is lowercase.
_Lowercase
/// The previous cased character in the current word is uppercase.
_Uppercase
)
// ToRustSnakeCase converts the given string to a snake_case string.
// Ported from https://github.com/withoutboats/heck/blob/c501fc95db91ce20eaef248a511caec7142208b4/src/lib.rs#L75 as used by Anchor.
func ToRustSnakeCase(s string) string {
builder := new(strings.Builder)
first_word := true
words := splitIntoWords(s)
for _, word := range words {
char_indices := newReader(word)
init := 0
mode := _Boundary
for char_indices.Move() {
i, c := char_indices.This()
// Skip underscore characters
if c == '_' {
if init == i {
init += 1
}
continue
}
if next_i, next := char_indices.Peek(); next_i != -1 {
// The mode including the current character, assuming the
// current character does not result in a word boundary.
next_mode := func() _WordMode {
if unicode.IsLower(c) {
return _Lowercase
} else if unicode.IsUpper(c) {
return _Uppercase
} else {
return mode
}
}()
// Word boundary after if next is underscore or current is
// not uppercase and next is uppercase
if next == '_' || (next_mode == _Lowercase && unicode.IsUpper(next)) {
if !first_word {
// boundary(f)?;
builder.WriteRune('_')
}
{
// with_word(&word[init..next_i], f)?;
builder.WriteString(strings.ToLower(word[init:next_i]))
}
first_word = false
init = next_i
mode = _Boundary
// Otherwise if current and previous are uppercase and next
// is lowercase, word boundary before
} else if mode == _Uppercase && unicode.IsUpper(c) && unicode.IsLower(next) {
if !first_word {
// boundary(f)?;
builder.WriteRune('_')
} else {
first_word = false
}
{
// with_word(&word[init..i], f)?;
builder.WriteString(strings.ToLower(word[init:i]))
}
init = i
mode = _Boundary
// Otherwise no word boundary, just update the mode
} else {
mode = next_mode
}
} else {
// Collect trailing characters as a word
if !first_word {
// boundary(f)?;
builder.WriteRune('_')
} else {
first_word = false
}
{
// with_word(&word[init..], f)?;
builder.WriteString(strings.ToLower(word[init:]))
}
break
}
}
}
return builder.String()
}