-
Notifications
You must be signed in to change notification settings - Fork 1
/
strings.go
243 lines (189 loc) · 5.98 KB
/
strings.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
package utils
import (
"bufio"
"strconv"
"strings"
"unicode"
normalize "github.com/pajlada/lidl-normalize"
)
const latinCapitalLetterBegin = 0x41
const latinCapitalLetterEnd = 0x5A
const latinSmallLetterBegin = 0x61
const latinSmallLetterEnd = 0x7A
const numbersBegin = 0x30
const numbersEnd = 0x39
func IsNotLatinLetter(r rune) bool {
return !((r >= latinSmallLetterBegin && r <= latinSmallLetterEnd) || (r >= latinCapitalLetterBegin && r <= latinCapitalLetterEnd))
}
func IsNotAlphanumeric(r rune) bool {
if r >= latinSmallLetterBegin && r <= latinSmallLetterEnd {
return false
}
if r >= latinCapitalLetterBegin && r <= latinCapitalLetterEnd {
return false
}
if r >= numbersBegin && r <= numbersEnd {
return false
}
return true
}
type RemoveFunc func(rune) bool
func RemoveInStringFunc(in string, predicate RemoveFunc) string {
outBytes := make([]rune, len(in))
length := 0
for _, r := range in {
if !predicate(r) {
outBytes[length] = r
length++
}
}
return string(outBytes[:length])
}
// InsertUnique inserts a string into a target string splice if it doesn't already exist in it
func InsertUnique(text string, target *[]string) {
for _, v := range *target {
if v == text {
return
}
}
*target = append(*target, text)
}
func lowercaseAll(in []string) []string {
out := make([]string, len(in))
for i, v := range in {
out[i] = strings.ToLower(v)
}
return out
}
// MakeVariations makes normal-case and lowercase variatinos of a string
func MakeVariations(text string, doNormalize bool) (originalVariations, lowercasedVariations []string, err error) {
originalVariations = []string{
// Full message
text,
}
// Full message with all spaces removed
InsertUnique(RemoveInStringFunc(text, unicode.IsSpace), &originalVariations)
// Full message with all spaces and non-latin letters removed
InsertUnique(RemoveInStringFunc(text, IsNotLatinLetter), &originalVariations)
// Message with all non-alpha-numberic letters removed
InsertUnique(RemoveInStringFunc(text, IsNotAlphanumeric), &originalVariations)
if doNormalize {
normalizedMessage, err := normalize.Normalize(text)
if err != nil {
return nil, nil, err
}
// Normalized message
InsertUnique(normalizedMessage, &originalVariations)
// Normalized message with all spaces removed
InsertUnique(RemoveInStringFunc(normalizedMessage, unicode.IsSpace), &originalVariations)
// Normalized message with all spaces non-latin letters removed
InsertUnique(RemoveInStringFunc(normalizedMessage, IsNotLatinLetter), &originalVariations)
// Normalized message with all spaces non-alpha-numberic letters removed
InsertUnique(RemoveInStringFunc(normalizedMessage, IsNotAlphanumeric), &originalVariations)
}
return originalVariations, lowercaseAll(originalVariations), nil
}
// IsValidUsername verifies that the given user name is not empty, and that it only contains valid characters (0-9 a-z A-Z _)
// It does not to any length-checks, as there are no unbroken rules as to how short or long a user name can be
func IsValidUsername(username string) bool {
if username == "" {
return false
}
for _, r := range username {
// Numbers || uppercase a-z || lowercase a-z || underscore
if (r >= 0x30 && r <= 0x39) || (r >= 0x41 && r <= 0x5A) || (r >= 0x61 && r <= 0x7A) || r == 0x5F {
continue
}
return false
}
return true
}
// IsValidUserID verifies that the given user id is not empty, and that it only contains valid characters
// It does not to any length-checks, as there are no clear rules as to how short or long a user ID can be
func IsValidUserID(userID string) bool {
if userID == "" {
return false
}
for _, r := range userID {
// Numbers
if r >= 0x30 && r <= 0x39 {
continue
}
return false
}
return true
}
func FilterChannelName(username string) string {
username = strings.TrimPrefix(username, "#")
if IsValidUsername(username) {
return username
}
return ""
}
func FilterUsername(username string) string {
username = strings.TrimPrefix(username, "@")
username = strings.TrimSuffix(username, ",")
username = strings.TrimSuffix(username, ":")
if IsValidUsername(username) {
return username
}
return ""
}
func FilterUsernames(potentialUsernames []string) (usernames []string) {
for _, s := range potentialUsernames {
if IsValidUsername(s) {
usernames = append(usernames, strings.ToLower(s))
}
}
return
}
func FilterUserIDs(potentialUserIDs []string) (userIDs []string) {
for _, s := range potentialUserIDs {
if IsValidUserID(s) {
userIDs = append(userIDs, s)
}
}
return
}
const noPing = string("\u05C4")
func UnpingifyUsername(username string) string {
return string(username[0]) + noPing + username[1:]
}
// ReadArg reads a string until \n and trims all whitespace
func ReadArg(reader *bufio.Reader) string {
untrimmed, _ := reader.ReadString('\n')
return strings.TrimSpace(untrimmed)
}
// GetTriggersKC returns a list of strings that have been parsed in accordance
// to the command rules, but keeps the case
func GetTriggersKC(message string) []string {
return strings.Split(strings.Replace(message, "!", "", 1), " ")
}
// RemoveNewlines replaces all \r and \n with spaces
func RemoveNewlines(s string) string {
s = strings.ReplaceAll(s, "\r", " ")
s = strings.ReplaceAll(s, "\n", " ")
return s
}
// GetTriggers returns a list of strings that have been parsed in accordance
// to the command rules
func GetTriggers(message string) []string {
return strings.Split(strings.Replace(strings.ToLower(message), "!", "", 1), " ")
}
// GetTriggersN returns a list of strings that have been parsed in accordance
// to the command rules. Offset by N
func GetTriggersN(message string, n int) []string {
triggers := GetTriggers(message)
if len(triggers) >= n {
return triggers[n:]
}
return []string{}
}
// NewStringPtr returns the pointer to the given string
func NewStringPtr(s string) *string {
return &s
}
// FloatToString converts a float value to a string with sane defaults
func Float32ToString(v float32) string {
return strconv.FormatFloat(float64(v), 'f', -1, 32)
}