-
Notifications
You must be signed in to change notification settings - Fork 182
/
check.go
171 lines (138 loc) · 4.67 KB
/
check.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
package strutil
import (
"strings"
"unicode"
"unicode/utf8"
)
// Equal check
var Equal = strings.EqualFold
// IsNumeric returns true if the given character is a numeric, otherwise false.
func IsNumeric(c byte) bool {
return c >= '0' && c <= '9'
}
// IsAlphabet char
func IsAlphabet(char uint8) bool {
// A 65 -> Z 90
if char >= 'A' && char <= 'Z' {
return true
}
// a 97 -> z 122
if char >= 'a' && char <= 'z' {
return true
}
return false
}
// IsAlphaNum reports whether the byte is an ASCII letter, number, or underscore
func IsAlphaNum(c uint8) bool {
return c == '_' || '0' <= c && c <= '9' || 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z'
}
// StrPos alias of the strings.Index
func StrPos(s, sub string) int {
return strings.Index(s, sub)
}
// BytePos alias of the strings.IndexByte
func BytePos(s string, bt byte) int {
return strings.IndexByte(s, bt)
}
// RunePos alias of the strings.IndexRune
func RunePos(s string, ru rune) int {
return strings.IndexRune(s, ru)
}
// HasOneSub substr in the given string.
func HasOneSub(s string, subs []string) bool {
for _, sub := range subs {
if strings.Contains(s, sub) {
return true
}
}
return false
}
// HasAllSubs all substr in the given string.
func HasAllSubs(s string, subs []string) bool {
for _, sub := range subs {
if !strings.Contains(s, sub) {
return false
}
}
return true
}
// IsStartsOf alias of the HasOnePrefix
func IsStartsOf(s string, prefixes []string) bool {
return HasOnePrefix(s, prefixes)
}
// HasOnePrefix the string start withs one of the subs
func HasOnePrefix(s string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
// HasPrefix substr in the given string.
func HasPrefix(s string, prefix string) bool { return strings.HasPrefix(s, prefix) }
// IsStartOf alias of the strings.HasPrefix
func IsStartOf(s, prefix string) bool { return strings.HasPrefix(s, prefix) }
// HasSuffix substr in the given string.
func HasSuffix(s string, suffix string) bool { return strings.HasSuffix(s, suffix) }
// IsEndOf alias of the strings.HasSuffix
func IsEndOf(s, suffix string) bool { return strings.HasSuffix(s, suffix) }
// Len of the string
func Len(s string) int { return len(s) }
// RuneLen of the string
func RuneLen(s string) int { return len([]rune(s)) }
// Utf8Len of the string
func Utf8Len(s string) int { return utf8.RuneCountInString(s) }
// Utf8len of the string
func Utf8len(s string) int { return utf8.RuneCountInString(s) }
// IsValidUtf8 valid utf8 string check
func IsValidUtf8(s string) bool { return utf8.ValidString(s) }
// ----- refer from github.com/yuin/goldmark/util
// refer from github.com/yuin/goldmark/util
var spaceTable = [256]int8{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
// IsSpace returns true if the given character is a space, otherwise false.
func IsSpace(c byte) bool {
return spaceTable[c] == 1
}
// IsSpaceRune returns true if the given rune is a space, otherwise false.
func IsSpaceRune(r rune) bool {
return r <= 256 && IsSpace(byte(r)) || unicode.IsSpace(r)
}
// IsEmpty returns true if the given string is empty.
func IsEmpty(s string) bool { return len(s) == 0 }
// IsBlank returns true if the given string is all space characters.
func IsBlank(s string) bool {
return IsBlankBytes([]byte(s))
}
// IsNotBlank returns true if the given string is not blank.
func IsNotBlank(s string) bool {
return !IsBlankBytes([]byte(s))
}
// IsBlankBytes returns true if the given []byte is all space characters.
func IsBlankBytes(bs []byte) bool {
for _, b := range bs {
if !IsSpace(b) {
return false
}
}
return true
}
// IsSymbol reports whether the rune is a symbolic character.
func IsSymbol(r rune) bool {
return unicode.IsSymbol(r)
}
// VersionCompare for two version string.
func VersionCompare(v1, v2, op string) bool {
switch op {
case ">":
return v1 > v2
case "<":
return v1 < v2
case ">=":
return v1 >= v2
case "<=":
return v1 <= v2
default:
return v1 == v2
}
}