-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner_rev.go
250 lines (220 loc) · 6.26 KB
/
scanner_rev.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
package tok
import (
"strings"
"unicode/utf8"
)
// -----------------------------------------------------------------------------
// Creates a new Scanner to scan the str string and moves the scanner to the end.
func NewRevScanner(str string) *Scanner {
sca := NewScanner(str)
sca.ToEnd()
return sca
}
// ---------------------------------------------------------------------- string
// Moves s the length of str backward if Head() has str as the prefix.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIf(str string) bool {
if strings.HasSuffix(s.Head(), str) {
return s.Move(-len(str))
}
return false
}
// Moves s the length fo the value in strs backward if Head() is the suffix.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIfAny(strs []string) bool {
for _, str := range strs {
if s.RevIf(str) {
return true
}
}
return false
}
// Moves s to the last appearance of str in Head().
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevTo(str string) bool {
itr := makeRevItr(s.Head())
for itr.next() {
if strings.HasSuffix(itr.Str, str) {
return s.Move(-itr.pos())
}
}
return false
}
// ------------------------------------------------------------------------ fold
// Moves s the length of str backward if Head() has str as the suffix under Unicode case-folding.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIfFold(str string) bool {
i := len(str)
head := s.Head()
suffix := head[len(head)-i:]
if strings.EqualFold(suffix, str) {
return s.Move(-i)
}
return false
}
// Moves s to the last appearance of str in Head() under Unicode case-folding.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevToFold(str string) bool {
i := len(str)
itr := makeRevItr(s.Head())
for itr.next() {
suffix := itr.Str[len(itr.Str)-i:]
if strings.EqualFold(suffix, str) {
return s.Move(-itr.pos())
}
}
return false
}
// ------------------------------------------------------------------------ rune
// Moves s one rune value backward if the last rune in Head() equals r.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIfRune(r rune) bool {
last, i := utf8.DecodeLastRuneInString(s.Head())
if i == -1 || last != r {
return false
}
return s.Move(-i)
}
// Moves s to the last rune in Head() that matches with r.
// The function does not move s if not values matches with r.
// Returns true if a match was found, otherwise false.
func (s *Scanner) RevToRune(r rune) bool {
itr := makeRevItr(s.Head())
for itr.next() {
if itr.Val == r {
return s.Move(-itr.pos())
}
}
return false
}
// Moves s to the last rune in Head() that does not match with r.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevWhileRune(r rune) bool {
if len(s.Head()) == 0 {
return false
}
itr := makeRevItr(s.Head())
for itr.next() {
if itr.Val != r {
if itr.pos() == len(s.Head()) {
return false
}
return s.Move(-itr.pos())
}
}
return s.Move(-itr.pos())
}
// --------------------------------------------------------------------- anyrune
// Moves s one rune backward if the last rune in Head() is any of the runes in str.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIfAnyRune(str string) bool {
last, i := utf8.DecodeLastRuneInString(s.Head())
if i != -1 && strings.ContainsRune(str, last) {
return s.Move(-i)
}
return false
}
// Moves s to the last rune in Head() that matches any of the runes in str.
// Returns true if a match was found, otherwise false.
func (s *Scanner) RevToAnyRune(str string) bool {
itr := makeRevItr(s.Head())
for itr.next() {
if strings.ContainsRune(str, itr.Val) {
return s.Move(-itr.pos())
}
}
return false
}
// Moves s to the last rune in Head() that does not match any of the runes in str.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevWhileAnyRune(str string) bool {
if len(s.Head()) == 0 {
return false
}
itr := makeRevItr(s.Head())
for itr.next() {
if !strings.ContainsRune(str, itr.Val) {
if itr.pos() == len(s.Head()) {
return false
}
return s.Move(-itr.pos())
}
}
return s.Move(-itr.pos())
}
// --------------------------------------------------------------------- between
// Moves s one rune backward if the last rune in Head() is >= min and <= max.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIfBetween(min, max rune) bool {
last, i := utf8.DecodeLastRuneInString(s.Head())
if i == -1 || inRange(min, last, max) {
return false
}
return s.Move(-i)
}
// Moves s to the last rune in Head() that is >= min and <= max.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevToBetween(min, max rune) bool {
itr := makeRevItr(s.Head())
for itr.next() {
if inRange(min, itr.Val, max) {
return s.Move(-itr.pos())
}
}
return false
}
// Moves s to the last rune in Head() that is < min or > max.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevWhileBetween(min, max rune) bool {
if len(s.Head()) == 0 {
return false
}
itr := makeRevItr(s.Head())
for itr.next() {
if !inRange(min, itr.Val, max) {
if itr.pos() == len(s.Head()) {
return false
}
return s.Move(-itr.pos())
}
}
return s.Move(-itr.pos())
}
// ---------------------------------------------------------------------- match
// Moves s one rune backward if the last rune in Head() passes the check.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevIfMatch(check MatchFunc) bool {
last, i := utf8.DecodeLastRuneInString(s.Head())
if i == -1 || check(last) {
return false
}
return s.Move(-i)
}
// Moves s to the last rune in Head() that passes the ckeck.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevToMatch(check MatchFunc) bool {
itr := makeRevItr(s.Head())
for itr.next() {
if check(itr.Val) {
return s.Move(-itr.pos())
}
}
return false
}
// Moves s to the last rune in Head() that does not pass the ckeck.
// Returns true if s was moved, otherwise false.
func (s *Scanner) RevWhileMatch(check MatchFunc) bool {
if len(s.Head()) == 0 {
return false
}
itr := makeRevItr(s.Head())
for itr.next() {
if !check(itr.Val) {
if itr.pos() == len(s.Head()) {
return false
}
return s.Move(-itr.pos())
}
}
return s.Move(-itr.pos())
}