-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.go
223 lines (194 loc) · 5.04 KB
/
misc.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
package objectid
import (
"errors"
"fmt"
"strconv"
"strings"
"unicode"
)
var (
printf func(string, ...any) (int, error) = fmt.Printf
sprintf func(string, ...any) string = fmt.Sprintf
atoi func(string) (int, error) = strconv.Atoi
puint64 func(string, int, int) (uint64, error) = strconv.ParseUint
contains func(string, string) bool = strings.Contains
eq func(string, string) bool = strings.EqualFold
fields func(string) []string = strings.Fields
hasPrefix func(string, string) bool = strings.HasPrefix
hasSuffix func(string, string) bool = strings.HasSuffix
indexRune func(string, rune) int = strings.IndexRune
join func([]string, string) string = strings.Join
split func(string, string) []string = strings.Split
splitAfter func(string, string) []string = strings.SplitAfter
splitN func(string, string, int) []string = strings.SplitN
trimS func(string) string = strings.TrimSpace
trimL func(string, string) string = strings.TrimLeft
trimR func(string, string) string = strings.TrimRight
isDigit func(rune) bool = unicode.IsDigit
isLetter func(rune) bool = unicode.IsLetter
isLower func(rune) bool = unicode.IsLower
isUpper func(rune) bool = unicode.IsUpper
)
func errorf(msg any, x ...any) (err error) {
switch tv := msg.(type) {
case string:
err = errors.New(sprintf(tv, x...))
case error:
err = errors.New(sprintf(tv.Error(), x...))
}
return
}
/*
strInSlice returns a Boolean value indicative of whether the
specified string (str) is present within slice. Please note
that case is a significant element in the matching process.
*/
func strInSlice(str string, slice []string) bool {
for i := 0; i < len(slice); i++ {
if str == slice[i] {
return true
}
}
return false
}
/*
strInSliceFold returns a Boolean value indicative of whether
the specified string (str) is present within slice. Case is
not significant in the matching process.
*/
func strInSliceFold(str string, slice []string) bool {
for i := 0; i < len(slice); i++ {
if eq(str, slice[i]) {
return true
}
}
return false
}
func isPowerOfTwo(x int) bool {
return x&(x-1) == 0
}
/*
is 'val' an unsigned number?
*/
func isNumber(val string) bool {
if len(val) == 0 {
return false
}
for i := 0; i < len(val); i++ {
if !isDigit(rune(val[i])) {
return false
}
}
return true
}
/*
isAlnum returns a Boolean value indicative of whether rune r represents
an alphanumeric character. Specifically, one (1) of the following ranges
must evaluate as true:
- 0-9 (ASCII characters 48 through 57)
- A-Z (ASCII characters 65 through 90)
- a-z (ASCII characters 97 through 122)
*/
func isAlnum(r rune) bool {
return isLower(r) || isUpper(r) || isDigit(r)
}
/*
IsIdentifier scans the input string val and judges whether it appears to
qualify as an [ITU-T Rec. X.680] Identifier, in that:
- It is non-zero in length
- It begins with a lower alpha, ends in an alphanumeric
- It contains only alphanumeric characters or hyphens
- It contains no consecutive hyphens
[ITU-T Rec. X.680]: https://www.itu.int/rec/T-REC-X.680
*/
func IsIdentifier(val string) bool {
return isIdentifier(val)
}
func isIdentifier(val string) bool {
if len(val) == 0 {
return false
}
// must begin with a lower alpha.
if !isLower(rune(val[0])) {
return false
}
// can only end in alnum.
if !isAlnum(rune(val[len(val)-1])) {
return false
}
// watch hyphens to avoid contiguous use
var lastHyphen bool
// iterate all characters in val, checking
// each one for validity.
for i := 0; i < len(val); i++ {
ch := rune(val[i])
switch {
case isAlnum(ch):
lastHyphen = false
case ch == '-':
if lastHyphen {
// cannot use consecutive hyphens
return false
}
lastHyphen = true
default:
// invalid character (none of [a-zA-Z0-9\-])
return false
}
}
return true
}
/*
compare slice members of two (2) []int instances.
*/
func intSliceEqual(s1, s2 []int) (equal bool) {
if len(s1)|len(s2) == 0 || len(s1) != len(s2) {
return
}
for i := 0; i < len(s1); i++ {
if equal = s1[i] == s2[i]; !equal {
break
}
}
return
}
/*
compare slice members of two (2) []string instances.
*/
func strSliceEqual(s1, s2 []string) (equal bool) {
if len(s1)|len(s2) == 0 || len(s1) != len(s2) {
return
}
for i := 0; i < len(s1); i++ {
if equal = s1[i] == s2[i]; !equal {
break
}
}
return
}
/*
condenseWHSP returns input string b with all contiguous
WHSP characters condensed into single space characters.
*/
func condenseWHSP(b string) (a string) {
// remove leading and trailing
// WHSP characters ...
b = trimS(b)
var last bool
for i := 0; i < len(b); i++ {
c := rune(b[i])
switch c {
case ' ':
if !last {
last = true
a += string(c)
}
default:
if last {
last = false
}
a += string(c)
}
}
return
}