-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcomplex-string.go
267 lines (226 loc) · 6.68 KB
/
complex-string.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package handy
import (
"regexp"
"strings"
"unicode"
"unicode/utf8"
)
// HasNumber returns true if input string contains at least one digit/number
func HasNumber(s string) bool {
for _, s := range s {
if unicode.IsNumber(s) {
return true
}
}
return false
}
// HasLetter returns true if input string contains at least one letter
func HasLetter(s string) bool {
for _, s := range s {
if unicode.IsLetter(s) {
return true
}
}
return false
}
// HasSymbol returns true if input string contains at least one symbol
// If rune is not a space, letter nor a number, it's considered a symbol
func HasSymbol(s string) bool {
for _, s := range s {
if unicode.IsSymbol(s) || (!unicode.IsLetter(s) && !unicode.IsNumber(s) && !unicode.IsSpace(s)) {
return true
}
}
return false
}
const (
// CheckStrAllowEmpty allows empty string ""
CheckStrAllowEmpty = 1
// CheckStrDenySpaces forbids spaces, tabs, new lines and carriage return
CheckStrDenySpaces = 2
// CheckStrDenyNumbers forbids digits/numbers
CheckStrDenyNumbers = 4
// CheckStrDenyLetters forbids letters
CheckStrDenyLetters = 8
// CheckStrDenySymbols forbids symbols. if it's not a number, letter or space, is considered a symbol
CheckStrDenySymbols = 16
// CheckStrDenyMoreThanOneWord forbids more than one word
CheckStrDenyMoreThanOneWord = 32
// CheckStrDenyUpperCase forbids uppercase letters
CheckStrDenyUpperCase = 64
// CheckStrDenyLowercase forbids lowercase letters
CheckStrDenyLowercase = 128
// CheckStrDenyUnicode forbids non-ASCII characters
CheckStrDenyUnicode = 256
// CheckStrRequireNumbers demands at least 1 number within string
CheckStrRequireNumbers = 512
// CheckStrRequireLetters demands at least 1 letter within string
CheckStrRequireLetters = 1024
// CheckStrRequireSymbols demands at least 1 symbol within string. if it's not a number, letter or space, is considered a symbol
CheckStrRequireSymbols = 2048
// CheckStrRequireMoreThanOneWord demands at least 2 words in given string input
CheckStrRequireMoreThanOneWord = 4096
// CheckStrRequireUpperCase demands at least 1 uppercase letter within string
CheckStrRequireUpperCase = 8192
// CheckStrRequireLowercase demands at least 1 lowercase letter within string
CheckStrRequireLowercase = 16384
// CheckStrOk means "alright"
CheckStrOk = 0
// CheckStrEmptyDenied is self explained
CheckStrEmptyDenied = -1
// CheckStrTooShort is self explained
CheckStrTooShort = -2
// CheckStrTooLong is self explained
CheckStrTooLong = -4
// CheckStrSpaceDenied is self explained
CheckStrSpaceDenied = -5
// CheckStrNubersDenied is self explained
CheckStrNubersDenied = -6
// CheckStrLettersDenied is self explained
CheckStrLettersDenied = -7
// CheckStrSymbolsDenied is self explained
CheckStrSymbolsDenied = -8
// CheckStrMoreThanOneWordDenied is self explained
CheckStrMoreThanOneWordDenied = -9
// CheckStrUpperCaseDenied is self explained
CheckStrUpperCaseDenied = -10
// CheckStrLowercaseDenied is self explained
CheckStrLowercaseDenied = -11
// CheckStrUnicodeDenied is self explained
CheckStrUnicodeDenied = -12
// CheckStrNumbersNotFound is self explained
CheckStrNumbersNotFound = -13
// CheckStrLettersNotFound is self explained
CheckStrLettersNotFound = -14
// CheckStrSymbolsNotFound is self explained
CheckStrSymbolsNotFound = -15
// CheckStrMoreThanOneWordNotFound is self explained
CheckStrMoreThanOneWordNotFound = -16
// CheckStrUpperCaseNotFound is self explained
CheckStrUpperCaseNotFound = -17
// CheckStrLowercaseNotFound is self explained
CheckStrLowercaseNotFound = -18
)
var (
reEmailFinder = regexp.MustCompile(`([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)`)
)
// CheckStr validates a string according given complexity rules
// CheckStr first evaluates "Deny" rules, and then "Require" rules.
// minLen=0 means there's no minimum length
// maxLen=0 means there's no maximum length
func CheckStr(seq string, minLen, maxLen uint, rules uint64) int8 {
strLen := uint(utf8.RuneCountInString(seq))
if seq == "" {
if rules&CheckStrAllowEmpty == CheckStrAllowEmpty {
return CheckStrOk
}
return CheckStrEmptyDenied
}
if strLen < minLen {
return CheckStrTooShort
}
if maxLen > 0 && strLen > maxLen {
return CheckStrTooLong
}
if rules&CheckStrDenySpaces == CheckStrDenySpaces {
if strings.ContainsAny(seq, "\n\r\t ") {
return CheckStrSpaceDenied
}
}
containsNumbers := HasNumber(seq)
if rules&CheckStrDenyNumbers == CheckStrDenyNumbers {
if containsNumbers {
return CheckStrNubersDenied
}
}
containsLetters := HasLetter(seq)
if rules&CheckStrDenyLetters == CheckStrDenyLetters {
if containsLetters {
return CheckStrLettersDenied
}
}
containsSymbols := HasSymbol(seq)
if rules&CheckStrDenySymbols == CheckStrDenySymbols {
if containsSymbols {
return CheckStrSymbolsDenied
}
}
containsMoreThanOneWord := len(strings.Fields(seq)) > 1
if rules&CheckStrDenyMoreThanOneWord == CheckStrDenyMoreThanOneWord {
if containsMoreThanOneWord {
return CheckStrMoreThanOneWordDenied
}
}
containsUppercase := func(s string) bool {
if containsLetters {
for _, s := range s {
if unicode.IsUpper(s) {
return true
}
}
}
return false
}(seq)
if rules&CheckStrDenyUpperCase == CheckStrDenyUpperCase {
if containsUppercase {
return CheckStrUpperCaseDenied
}
}
containsLowercase := func(s string) bool {
if containsLetters {
for _, s := range s {
if unicode.IsLower(s) {
return true
}
}
}
return false
}(seq)
if rules&CheckStrDenyLowercase == CheckStrDenyLowercase {
if containsLowercase {
return CheckStrLowercaseDenied
}
}
if rules&CheckStrDenyUnicode == CheckStrDenyUnicode {
for _, s := range seq {
if s > unicode.MaxASCII {
return CheckStrUnicodeDenied
}
}
}
if rules&CheckStrRequireNumbers == CheckStrRequireNumbers {
if !containsNumbers {
return CheckStrNumbersNotFound
}
}
if rules&CheckStrRequireLetters == CheckStrRequireLetters {
if !containsLetters {
return CheckStrLettersNotFound
}
}
if rules&CheckStrRequireSymbols == CheckStrRequireSymbols {
if !containsSymbols {
return CheckStrSymbolsNotFound
}
}
if rules&CheckStrRequireMoreThanOneWord == CheckStrRequireMoreThanOneWord {
if !containsMoreThanOneWord {
return CheckStrMoreThanOneWordNotFound
}
}
if rules&CheckStrRequireUpperCase == CheckStrRequireUpperCase {
if !containsUppercase {
return CheckStrUpperCaseNotFound
}
}
if rules&CheckStrRequireLowercase == CheckStrRequireLowercase {
if !containsLowercase {
return CheckStrLowercaseNotFound
}
}
return CheckStrOk
}
// StrContainsEmail returns true if given string contains an email address
func StrContainsEmail(seq string) bool {
return reEmailFinder.MatchString(seq)
}