@@ -188,8 +188,7 @@ function _search_bloom_mask(c)
188
188
end
189
189
190
190
_nthbyte (s:: String , i) = codeunit (s, i)
191
- _nthbyte (a:: Union{Vector{UInt8},Vector{Int8}} , i) = a[i]
192
- _nthbyte (t:: AbstractVector , index) = t[firstindex (t) + (index- 1 )]
191
+ _nthbyte (t:: AbstractVector , index) = t[index + (firstindex (t)- 1 )]
193
192
194
193
function _searchindex (s:: String , t:: String , i:: Integer )
195
194
# Check for fast case of a single byte
@@ -199,29 +198,29 @@ end
199
198
200
199
function _searchindex (s:: AbstractVector{<:Union{Int8,UInt8}} ,
201
200
t:: AbstractVector{<:Union{Int8,UInt8}} ,
202
- i :: Integer )
201
+ _i :: Integer )
203
202
n = length (t)
204
203
m = length (s)
205
- f_s = firstindex (s)
206
- i < f_s && throw (BoundsError (s, i ))
204
+ i = Int (_i) - ( firstindex (s) - 1 )
205
+ i < 1 && throw (BoundsError (s, _i ))
207
206
208
207
if n == 0
209
- return f_s <= i <= m+ 1 ? max (f_s , i) : 0
208
+ return 1 <= i <= m+ 1 ? max (1 , i) : 0
210
209
elseif m == 0
211
210
return 0
212
211
elseif n == 1
213
212
return something (findnext (isequal (_nthbyte (t,1 )), s, i), 0 )
214
213
end
215
214
216
215
w = m - n
217
- if w < 0 || i - f_s > w
216
+ if w < 0 || i - 1 > w
218
217
return 0
219
218
end
220
219
221
220
bloom_mask = UInt64 (0 )
222
- skip = n - f_s
221
+ skip = n - 1
223
222
tlast = _nthbyte (t,n)
224
- for j in eachindex (t)
223
+ for j in 1 : n
225
224
bloom_mask |= _search_bloom_mask (_nthbyte (t,j))
226
225
if _nthbyte (t,j) == tlast && j < n
227
226
skip = n - j - 1
@@ -242,7 +241,8 @@ function _searchindex(s::AbstractVector{<:Union{Int8,UInt8}},
242
241
243
242
# match found
244
243
if j == n - 1
245
- return i+ f_s
244
+ # restore in case `s` is an OffSetArray
245
+ return i+ firstindex (s)
246
246
end
247
247
248
248
# no match, try to rule out the next character
@@ -333,17 +333,20 @@ Find the next occurrence of the sequence `pattern` in vector `A` starting at pos
333
333
334
334
# Examples
335
335
```jldoctest
336
- julia> findnext([0x52, 0x62], [0x52, 0x62, 0x72], 5 ) === nothing
336
+ julia> findnext([0x52, 0x62], [0x52, 0x62, 0x72], 3 ) === nothing
337
337
true
338
338
339
339
julia> findnext([0x52, 0x62], [0x40, 0x52, 0x62, 0x52, 0x62], 3)
340
340
4:5
341
341
```
342
342
"""
343
- findnext (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
344
- A:: AbstractVector{<:Union{Int8,UInt8}} ,
345
- start:: Integer ) =
343
+ function findnext (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
344
+ A:: AbstractVector{<:Union{Int8,UInt8}} ,
345
+ start:: Integer )
346
+ (start == (lastindex (A)+ 1 )) && return nothing
347
+ (start > (lastindex (A)+ 1 )) && throw (BoundsError (A, start))
346
348
_search (A, pattern, start)
349
+ end
347
350
348
351
"""
349
352
findlast(pattern::AbstractString, string::AbstractString)
@@ -376,9 +379,10 @@ julia> findlast([0x52, 0x62], [0x52, 0x62, 0x52, 0x62])
376
379
3:4
377
380
```
378
381
"""
379
- findlast (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
380
- A:: AbstractVector{<:Union{Int8,UInt8}} ) =
382
+ function findlast (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
383
+ A:: AbstractVector{<:Union{Int8,UInt8}} )
381
384
findprev (pattern, A, lastindex (A))
385
+ end
382
386
"""
383
387
findlast(ch::AbstractChar, string::AbstractString)
384
388
@@ -452,29 +456,29 @@ function _rsearchindex(s::String, t::String, i::Integer)
452
456
end
453
457
end
454
458
455
- function _rsearchindex (s:: AbstractVector{<:Union{Int8,UInt8}} , t:: AbstractVector{<:Union{Int8,UInt8}} , k :: Integer )
459
+ function _rsearchindex (s:: AbstractVector{<:Union{Int8,UInt8}} , t:: AbstractVector{<:Union{Int8,UInt8}} , _k :: Integer )
456
460
n = length (t)
457
461
m = length (s)
458
- f_s = firstindex (s)
459
- k < f_s && throw (BoundsError (s, k ))
462
+ k = Int (_k) - ( firstindex (s) - 1 )
463
+ k < 1 && throw (BoundsError (s, _k ))
460
464
461
465
if n == 0
462
- return 0 <= k <= m ? max (f_s, k ) : 0
466
+ return 0 <= k <= m ? max (k, 1 ) : 0
463
467
elseif m == 0
464
468
return 0
465
469
elseif n == 1
466
470
return something (findprev (isequal (_nthbyte (t,1 )), s, k), 0 )
467
471
end
468
472
469
473
w = m - n
470
- if w < 0 || k <= f_s
474
+ if w < 0 || k <= 0
471
475
return 0
472
476
end
473
477
474
478
bloom_mask = UInt64 (0 )
475
479
skip = n - 1
476
480
tfirst = _nthbyte (t,1 )
477
- for j in reverse ( eachindex (t))
481
+ for j in n : - 1 : 1
478
482
bloom_mask |= _search_bloom_mask (_nthbyte (t,j))
479
483
if _nthbyte (t,j) == tfirst && j > 1
480
484
skip = j - 2
@@ -495,7 +499,7 @@ function _rsearchindex(s::AbstractVector{<:Union{Int8,UInt8}}, t::AbstractVector
495
499
496
500
# match found
497
501
if j == n
498
- return i + f_s - 1
502
+ return i - 1 + firstindex (s)
499
503
end
500
504
501
505
# no match, try to rule out the next character
@@ -587,10 +591,13 @@ julia> findprev([0x52, 0x62], [0x40, 0x52, 0x62, 0x52, 0x62], 3)
587
591
2:3
588
592
```
589
593
"""
590
- findprev (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
591
- A:: AbstractVector{<:Union{Int8,UInt8}} ,
592
- start:: Integer ) =
594
+ function findprev (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
595
+ A:: AbstractVector{<:Union{Int8,UInt8}} ,
596
+ start:: Integer )
597
+ (start == (lastindex (A)+ 1 )) && return nothing
598
+ (start > (lastindex (A)+ 1 )) && throw (BoundsError (A, start))
593
599
_rsearch (A, pattern, start)
600
+ end
594
601
"""
595
602
occursin(needle::Union{AbstractString,Regex,AbstractChar}, haystack::AbstractString)
596
603
0 commit comments