-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
205 lines (181 loc) · 4.46 KB
/
search.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
package str
import (
"strings"
"unicode/utf8"
"github.com/askasoft/pango/str/rabinkarp"
)
// CountByte counts the number of b in s.
func CountByte(s string, b byte) int {
n := 0
for {
i := IndexByte(s, b)
if i == -1 {
return n
}
n++
s = s[i+1:]
}
}
// CountRune counts the number of c in s.
func CountRune(s string, c rune) int {
n := 0
for _, r := range s {
if r == c {
n++
}
}
return n
}
// CountAny counts the number of non-overlapping instances of any character of chars in s.
// If chars is an empty string, Count returns 1 + the number of Unicode code points in s.
func CountAny(s, chars string) int {
// special case
if len(chars) < 2 {
return strings.Count(s, chars)
}
n := 0
for _, c := range s {
if strings.ContainsRune(chars, c) {
n++
}
}
return n
}
// ContainsFold reports whether substr is within s (case insensitive).
func ContainsFold(s, substr string) bool {
return IndexFold(s, substr) >= 0
}
// ContainsByte reports whether b is within s.
func ContainsByte(s string, b byte) bool {
return strings.IndexByte(s, b) >= 0
}
// IndexFold returns the index of the first instance of substr in s (case insensitive), or -1 if substr is not present in s.
func IndexFold(s, substr string) int {
ns := len(s)
nb := len(substr)
if ns < nb {
return -1
}
if nb == 0 {
return 0
}
if ns == nb {
if strings.EqualFold(s, substr) {
return 0
}
return -1
}
l := ns - nb
for i := 0; i <= l; {
src := s[i : i+nb]
if strings.EqualFold(src, substr) {
return i
}
_, z := utf8.DecodeRuneInString(src)
i += z
}
return -1
}
// LastIndexRune returns the index of the last instance of the Unicode code point
// r, or -1 if rune is not present in s.
// If r is utf8.RuneError, it returns the last instance of any
// invalid UTF-8 byte sequence.
func LastIndexRune(s string, r rune) int {
switch {
case 0 <= r && r < utf8.RuneSelf:
return strings.LastIndexByte(s, byte(r))
case r == utf8.RuneError:
n := -1
for i, r := range s {
if r == utf8.RuneError {
n = i
}
}
return n
case !utf8.ValidRune(r):
return -1
default:
return strings.LastIndex(s, string(r))
}
}
// LastIndexFold returns the index of the last instance of substr in s (case insensitive), or -1 if substr is not present in s.
func LastIndexFold(s, substr string) int {
n := len(substr)
switch {
case n == 0:
return len(s)
case n == 1:
lc := ToLower(substr)
uc := ToUpper(substr)
if lc == uc {
return LastIndexByte(s, substr[0])
}
return LastIndexAny(s, lc+uc)
case n == len(s):
if EqualFold(s, substr) {
return 0
}
return -1
case n > len(s):
return -1
}
// Rabin-Karp search from the end of the string
hashss, pow := rabinkarp.HashStrRev(substr)
last := len(s) - n
var h uint32
for i := len(s) - 1; i >= last; i-- {
h = h*rabinkarp.PrimeRK + uint32(s[i])
}
if h == hashss && EqualFold(s[last:], substr) {
return last
}
for i := last - 1; i >= 0; i-- {
h *= rabinkarp.PrimeRK
h += uint32(s[i])
h -= pow * uint32(s[i+n])
if h == hashss && EqualFold(s[i:i+n], substr) {
return i
}
}
return -1
}
// HasPrefixFold Tests if the string s starts with the specified prefix (case insensitive).
func HasPrefixFold(s, prefix string) bool {
return len(s) >= len(prefix) && EqualFold(s[:len(prefix)], prefix)
}
// HasSuffixFold Tests if the string s ends with the specified suffix (case insensitive).
func HasSuffixFold(s, suffix string) bool {
return len(s) >= len(suffix) && EqualFold(s[len(s)-len(suffix):], suffix)
}
// StartsWith Tests if the string s starts with the specified prefix.
func StartsWith(s, prefix string) bool {
return strings.HasPrefix(s, prefix)
}
// EndsWith Tests if the string s ends with the specified suffix.
func EndsWith(s, suffix string) bool {
return strings.HasSuffix(s, suffix)
}
// StartsWithFold Tests if the string s starts with the specified prefix (case insensitive).
func StartsWithFold(s, prefix string) bool {
return HasPrefixFold(s, prefix)
}
// EndsWithFold Tests if the string s ends with the specified suffix (case insensitive).
func EndsWithFold(s, suffix string) bool {
return HasSuffixFold(s, suffix)
}
// StartsWithByte Tests if the byte slice s starts with the specified prefix b.
func StartsWithByte(s string, b byte) bool {
if s == "" {
return false
}
a := s[0]
return a == b
}
// EndsWithByte Tests if the byte slice bs ends with the specified suffix b.
func EndsWithByte(s string, b byte) bool {
if s == "" {
return false
}
a := s[len(s)-1]
return a == b
}