-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
161 lines (135 loc) · 2.8 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
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
package opts
import (
"bytes"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
func camel2const(s string) string {
b := bytes.Buffer{}
var c rune
start := 0
end := 0
for end, c = range s {
if c >= 'A' && c <= 'Z' {
//uppercase all prior letters and add an underscore
if start < end {
b.WriteString(strings.ToTitle(s[start:end] + "_"))
start = end
}
}
}
//write remaining string
b.WriteString(strings.ToTitle(s[start : end+1]))
return b.String()
}
func nletters(r rune, n int) string {
str := make([]rune, n)
for i, _ := range str {
str[i] = r
}
return string(str)
}
func str2str(src string, dst *string) {
if src != "" {
*dst = src
}
}
func str2bool(src string, dst *bool) {
if src != "" {
*dst = strings.ToLower(src) == "true" || src == "1"
}
}
func str2int(src string, dst *int) {
if src != "" {
n, err := strconv.Atoi(src)
if err == nil {
*dst = n
}
}
}
func constrain(str string, width int) string {
words := anyspace.Split(str, -1)
n := 0
for i, w := range words {
d := width - n
wn := len(w) + 1 //+space
n += wn
if n > width && n-width > d {
n = wn
w = "\n" + w
}
words[i] = w
}
return strings.Join(words, " ")
}
//borrowed from https://github.com/huandu/xstrings/blob/master/convert.go#L77
func camel2dash(str string) string {
if len(str) == 0 {
return ""
}
buf := &bytes.Buffer{}
var prev, r0, r1 rune
var size int
r0 = '-'
for len(str) > 0 {
prev = r0
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
switch {
case r0 == utf8.RuneError:
buf.WriteByte(byte(str[0]))
case unicode.IsUpper(r0):
if prev != '-' {
buf.WriteRune('-')
}
buf.WriteRune(unicode.ToLower(r0))
if len(str) == 0 {
break
}
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
if !unicode.IsUpper(r0) {
buf.WriteRune(r0)
break
}
// find next non-upper-case character and insert `_` properly.
// it's designed to convert `HTTPServer` to `http_server`.
// if there are more than 2 adjacent upper case characters in a word,
// treat them as an abbreviation plus a normal word.
for len(str) > 0 {
r1 = r0
r0, size = utf8.DecodeRuneInString(str)
str = str[size:]
if r0 == utf8.RuneError {
buf.WriteRune(unicode.ToLower(r1))
buf.WriteByte(byte(str[0]))
break
}
if !unicode.IsUpper(r0) {
if r0 == '-' || r0 == ' ' || r0 == '_' {
r0 = '-'
buf.WriteRune(unicode.ToLower(r1))
} else {
buf.WriteRune('-')
buf.WriteRune(unicode.ToLower(r1))
buf.WriteRune(r0)
}
break
}
buf.WriteRune(unicode.ToLower(r1))
}
if len(str) == 0 || r0 == '-' {
buf.WriteRune(unicode.ToLower(r0))
break
}
default:
if r0 == ' ' || r0 == '_' {
r0 = '-'
}
buf.WriteRune(r0)
}
}
return buf.String()
}