@@ -125,7 +125,7 @@ findfirst(ch::AbstractChar, string::AbstractString) = findfirst(==(ch), string)
125
125
126
126
"""
127
127
findfirst(pattern::AbstractVector{<:Union{Int8,UInt8}},
128
- A::AbstractVector{<:Union{Int8,UInt8}}) where {T<:Union{Int8,UInt8}}
128
+ A::AbstractVector{<:Union{Int8,UInt8}})
129
129
130
130
Find the first occurrence of sequence `pattern` in vector `A`.
131
131
199
199
200
200
function _searchindex (s:: AbstractVector{<:Union{Int8,UInt8}} ,
201
201
t:: AbstractVector{<:Union{Int8,UInt8}} ,
202
- i:: Integer ) where T <: Union{Int8,UInt8}
202
+ i:: Integer )
203
203
n = length (t)
204
204
m = length (s)
205
205
f_s = firstindex (s)
@@ -342,7 +342,7 @@ julia> findnext([0x52, 0x62], [0x40, 0x52, 0x62, 0x52, 0x62], 3)
342
342
"""
343
343
findnext (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
344
344
A:: AbstractVector{<:Union{Int8,UInt8}} ,
345
- start:: Integer ) where T <: Union{Int8,UInt8} =
345
+ start:: Integer ) =
346
346
_search (A, pattern, start)
347
347
348
348
"""
@@ -363,6 +363,22 @@ julia> findfirst("Julia", "JuliaLang")
363
363
findlast (pattern:: AbstractString , string:: AbstractString ) =
364
364
findprev (pattern, string, lastindex (string))
365
365
366
+ """
367
+ findlast(pattern::AbstractVector{<:Union{Int8,UInt8}},
368
+ A::AbstractVector{<:Union{Int8,UInt8}})
369
+
370
+ Find the last occurrence of `pattern` in array `A`. Equivalent to
371
+ [`findprev(pattern, A, lastindex(A))`](@ref).
372
+
373
+ # Examples
374
+ ```jldoctest
375
+ julia> findlast([0x52, 0x62], [0x52, 0x62, 0x52, 0x62])
376
+ 3:4
377
+ ```
378
+ """
379
+ findlast (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
380
+ A:: AbstractVector{<:Union{Int8,UInt8}} ) =
381
+ findprev (pattern, A, lastindex (A))
366
382
"""
367
383
findlast(ch::AbstractChar, string::AbstractString)
368
384
@@ -436,27 +452,29 @@ function _rsearchindex(s::String, t::String, i::Integer)
436
452
end
437
453
end
438
454
439
- function _rsearchindex (s:: ByteArray , t:: ByteArray , k:: Integer )
440
- n = sizeof (t)
441
- m = sizeof (s)
455
+ function _rsearchindex (s:: AbstractVector{<:Union{Int8,UInt8}} , t:: AbstractVector{<:Union{Int8,UInt8}} , k:: Integer )
456
+ n = length (t)
457
+ m = length (s)
458
+ f_s = firstindex (s)
459
+ k < f_s && throw (BoundsError (s, k))
442
460
443
461
if n == 0
444
- return 0 <= k <= m ? max (k, 1 ) : 0
462
+ return 0 <= k <= m ? max (f_s, k ) : 0
445
463
elseif m == 0
446
464
return 0
447
465
elseif n == 1
448
466
return something (findprev (isequal (_nthbyte (t,1 )), s, k), 0 )
449
467
end
450
468
451
469
w = m - n
452
- if w < 0 || k <= 0
470
+ if w < 0 || k <= f_s
453
471
return 0
454
472
end
455
473
456
474
bloom_mask = UInt64 (0 )
457
475
skip = n - 1
458
476
tfirst = _nthbyte (t,1 )
459
- for j in n : - 1 : 1
477
+ for j in reverse ( eachindex (t))
460
478
bloom_mask |= _search_bloom_mask (_nthbyte (t,j))
461
479
if _nthbyte (t,j) == tfirst && j > 1
462
480
skip = j - 2
@@ -477,7 +495,7 @@ function _rsearchindex(s::ByteArray, t::ByteArray, k::Integer)
477
495
478
496
# match found
479
497
if j == n
480
- return i
498
+ return i + f_s - 1
481
499
end
482
500
483
501
# no match, try to rule out the next character
@@ -497,9 +515,9 @@ function _rsearchindex(s::ByteArray, t::ByteArray, k::Integer)
497
515
0
498
516
end
499
517
500
- function _rsearch (s:: Union{AbstractString,ByteArray } ,
501
- t:: Union{AbstractString,AbstractChar,Int8,UInt8} ,
502
- i:: Integer )
518
+ function _rsearch (s:: Union{AbstractString,AbstractVector{<:Union{Int8,UInt8}} } ,
519
+ t:: Union{AbstractString,AbstractChar,AbstractVector{<:Union{ Int8,UInt8}} } ,
520
+ i:: Integer )
503
521
idx = _rsearchindex (s,t,i)
504
522
if isempty (t)
505
523
idx: idx- 1
@@ -552,9 +570,27 @@ julia> findprev('o', "Hello to the world", 18)
552
570
15
553
571
```
554
572
"""
555
- findprev (ch:: AbstractChar , string:: AbstractString , ind :: Integer ) =
556
- findprev (== (ch), string, ind )
573
+ findprev (ch:: AbstractChar , string:: AbstractString , start :: Integer ) =
574
+ findprev (== (ch), string, start )
557
575
576
+ """
577
+ findprev(pattern::AbstractVector{<:Union{Int8,UInt8}}, A::AbstractVector{<:Union{Int8,UInt8}}, start::Integer)
578
+
579
+ Find the previous occurrence of the sequence `pattern` in vector `A` starting at position `start`.
580
+
581
+ !!! compat "Julia 1.6"
582
+ This method requires at least Julia 1.6.
583
+
584
+ # Examples
585
+ ```jldoctest
586
+ julia> findprev([0x52, 0x62], [0x40, 0x52, 0x62, 0x52, 0x62], 3)
587
+ 2:3
588
+ ```
589
+ """
590
+ findprev (pattern:: AbstractVector{<:Union{Int8,UInt8}} ,
591
+ A:: AbstractVector{<:Union{Int8,UInt8}} ,
592
+ start:: Integer ) =
593
+ _rsearch (A, pattern, start)
558
594
"""
559
595
occursin(needle::Union{AbstractString,Regex,AbstractChar}, haystack::AbstractString)
560
596
0 commit comments