forked from Khan/genqlient
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
48 lines (42 loc) · 957 Bytes
/
util.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
package generate
import (
"strings"
"unicode"
"unicode/utf8"
)
func reverse(slice []string) {
for left, right := 0, len(slice)-1; left < right; left, right = left+1, right-1 {
slice[left], slice[right] = slice[right], slice[left]
}
}
func changeFirst(s string, f func(rune) rune) string {
c, n := utf8.DecodeRuneInString(s)
if c == utf8.RuneError { // empty or invalid
return s
}
return string(f(c)) + s[n:]
}
func lowerFirst(s string) string {
return changeFirst(strings.TrimLeft(s, "_"), unicode.ToLower)
}
func upperFirst(s string) string {
return changeFirst(strings.TrimLeft(s, "_"), unicode.ToUpper)
}
func goConstName(s string) string {
if strings.TrimLeft(s, "_") == "" {
return s
}
var prev rune
return strings.Map(func(r rune) rune {
var ret rune
if r == '_' {
ret = -1
} else if prev == '_' || prev == 0 {
ret = unicode.ToUpper(r)
} else {
ret = unicode.ToLower(r)
}
prev = r
return ret
}, s)
}