forked from layeh/radius
/
util.go
100 lines (91 loc) · 1.64 KB
/
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
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
package dictionarygen
import (
"bytes"
"io"
"strings"
"unicode"
)
func p(w io.Writer, s ...string) {
for _, v := range s {
io.WriteString(w, v)
}
io.WriteString(w, "\n")
}
var firstCharacterReplacements = map[byte]string{
'0': "Zero",
'1': "One",
'2': "Two",
'3': "Three",
'4': "Four",
'5': "Five",
'6': "Six",
'7': "Seven",
'8': "Eight",
'9': "Nine",
}
var characterReplacer = strings.NewReplacer(
`+`, `Plus`,
)
func identifier(name string) string {
if len(name) == 0 {
return ""
}
if replacement, ok := firstCharacterReplacements[name[0]]; ok {
name = replacement + name[1:]
}
name = characterReplacer.Replace(name)
fields := strings.FieldsFunc(name, func(r rune) bool {
return !unicode.IsNumber(r) && !unicode.IsLetter(r)
})
var id bytes.Buffer
for _, field := range fields {
fieldUpper := strings.ToUpper(field)
if commonInitialisms[fieldUpper] {
id.WriteString(fieldUpper)
} else {
id.WriteString(strings.Title(field))
}
}
return id.String()
}
// from golint
var commonInitialisms = map[string]bool{
"ACL": true,
"API": true,
"ASCII": true,
"CPU": true,
"CSS": true,
"DNS": true,
"EOF": true,
"GUID": true,
"HTML": true,
"HTTP": true,
"HTTPS": true,
"ID": true,
"IP": true,
"JSON": true,
"LHS": true,
"QPS": true,
"RAM": true,
"RHS": true,
"RPC": true,
"SLA": true,
"SMTP": true,
"SQL": true,
"SSH": true,
"TCP": true,
"TLS": true,
"TTL": true,
"UDP": true,
"UI": true,
"UID": true,
"UUID": true,
"URI": true,
"URL": true,
"UTF8": true,
"VM": true,
"XML": true,
"XMPP": true,
"XSRF": true,
"XSS": true,
}