-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrings.go
48 lines (43 loc) · 1.14 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
package foxkit
import (
"net/mail"
"strings"
)
// returns false if a given parameter is false, minSize, maxSize, ascii
// TODO: Test
func CheckStringFull(testString string, minLength, maxLength uint32) (minSize bool, maxSize bool, ascii bool) {
if strings.Count(testString, "") > int(maxLength+1) {
maxSize = false
} else {
maxSize = true
}
if strings.Count(testString, "") < int(minLength+1) {
minSize = false
} else {
minSize = true
}
return minSize, maxSize, IsASCII(testString)
}
// returns true if all parameters are true
func CheckString(testString string, minLength, maxLength uint32, asciiOnly bool) bool {
minSize, maxSize, ascii := CheckStringFull(testString, minLength, maxLength)
if minSize && maxSize && ((asciiOnly && ascii) || !asciiOnly) {
return true
} else {
return false
}
}
// returns true if the string only contains ascii characters
func IsASCII(s string) bool {
for _, c := range s {
if c > 126 || c < 33 { // ascii goes from 33 to 126
return false
}
}
return true
}
// returns true if the given string is an email
func CheckEmail(value string) bool {
_, err := mail.ParseAddress(value)
return err == nil
}