@@ -22,7 +22,7 @@ findfirst(pattern::AbstractString, string::AbstractString) =
22
22
findnext (pattern, string, start (string))
23
23
24
24
# AbstractString implementation of the generic findnext interface
25
- function findnext (testf:: Function , s:: AbstractString , i:: Integer = start (s) )
25
+ function findnext (testf:: Function , s:: AbstractString , i:: Integer )
26
26
@boundscheck (i < 1 || i > nextind (s,endof (s))) && throw (BoundsError (s, i))
27
27
@inbounds while ! done (s,i)
28
28
d, j = next (s,i)
@@ -45,7 +45,7 @@ function _searchindex(s::Union{AbstractString,ByteArray},
45
45
end
46
46
t1, j2 = next (t,start (t))
47
47
while true
48
- i = _searchindex (s,t1 ,i)
48
+ i = findnext ( equalto (t1),s ,i)
49
49
if i == 0 return 0 end
50
50
c, ii = next (s,i)
51
51
j = j2; k = ii
@@ -187,12 +187,12 @@ function _search(s, t, i::Integer)
187
187
end
188
188
189
189
"""
190
- findnext(pattern::AbstractString, string::AbstractString, [ start::Integer] )
191
- findnext(pattern::Regex, string::String, [ start::Integer] )
190
+ findnext(pattern::AbstractString, string::AbstractString, start::Integer)
191
+ findnext(pattern::Regex, string::String, start::Integer)
192
192
193
- Find the first occurrence of `pattern` in `string`. `pattern` can be either a
194
- string, or a regular expression, in which case `string` must be of type `String`.
195
- `start` optionally specifies a starting index .
193
+ Find the next occurrence of `pattern` in `string` starting at position `start`.
194
+ `pattern` can be either a string, or a regular expression, in which case `string`
195
+ must be of type `String` .
196
196
197
197
The return value is a range of indexes where the matching sequence is found, such that
198
198
`s[findnext(x, s, i)] == x`:
@@ -212,47 +212,50 @@ julia> findnext("Julia", "JuliaLang", 2)
212
212
1:5
213
213
```
214
214
"""
215
- findnext (t:: AbstractString , s:: AbstractString , i:: Integer = start (s)) = _search (s, t, i)
216
- findnext (t:: ByteArray , s:: ByteArray , i:: Integer = start (s)) = _search (s, t, i)
217
-
218
- function rsearch (s:: AbstractString , c:: Chars )
219
- f = c isa Char ? f = equalto (c) : x -> x in c
220
- j = findfirst (f, RevString (s))
221
- j == 0 && return 0
222
- endof (s)- j+ 1
223
- end
215
+ findnext (t:: AbstractString , s:: AbstractString , i:: Integer ) = _search (s, t, i)
216
+ # TODO : remove?
217
+ findnext (t:: ByteArray , s:: ByteArray , i:: Integer ) = _search (s, t, i)
224
218
225
219
"""
226
- rsearch(s::AbstractString, chars::Chars, [start::Integer])
220
+ findlast(pattern::AbstractString, string::AbstractString)
221
+ findlast(pattern::Regex, string::String)
227
222
228
- Similar to `search` but returning the last occurrence of the given characters within the
229
- given string, searching in reverse from `start` .
223
+ Find the last occurrence of `pattern` in `string`. Equivalent to
224
+ [`findlast(pattern, string, endof(s))`](@ref) .
230
225
231
226
# Examples
232
227
```jldoctest
233
- julia> rsearch("aaabbb","b")
234
- 6:6
228
+ julia> findlast("o", "Hello to the world")
229
+ 15:15
230
+
231
+ julia> findfirst("Julia", "JuliaLang")
232
+ 1:5
235
233
```
236
234
"""
237
- function rsearch (s:: AbstractString , c:: Chars , i:: Integer )
238
- f = c isa Char ? f = equalto (c) : x -> x in c
235
+ findlast (pattern:: AbstractString , string:: AbstractString ) =
236
+ findprev (pattern, string, endof (string))
237
+
238
+ # AbstractString implementation of the generic findprev interface
239
+ function findprev (testf:: Function , s:: AbstractString , i:: Integer )
239
240
e = endof (s)
240
- j = findnext (f , RevString (s), e- i+ 1 )
241
+ j = findnext (testf , RevString (s), e- i+ 1 )
241
242
j == 0 && return 0
242
243
e- j+ 1
243
244
end
244
245
245
- function _rsearchindex (s, t, i)
246
+ function _rsearchindex (s:: AbstractString ,
247
+ t:: Union{AbstractString,Char,Int8,UInt8} ,
248
+ i:: Integer )
246
249
if isempty (t)
247
250
return 1 <= i <= nextind (s,endof (s)) ? i :
248
251
throw (BoundsError (s, i))
249
252
end
250
- t = RevString (t)
253
+ t = t isa AbstractString ? RevString (t) : t
251
254
rs = RevString (s)
252
255
l = endof (s)
253
256
t1, j2 = next (t,start (t))
254
257
while true
255
- i = rsearch (s,t1 ,i)
258
+ i = findprev ( equalto (t1),s ,i)
256
259
if i == 0 return 0 end
257
260
c, ii = next (rs,l- i+ 1 )
258
261
j = j2; k = ii
@@ -276,7 +279,10 @@ function _rsearchindex(s, t, i)
276
279
end
277
280
end
278
281
279
- function _rsearchindex (s:: Union{String,ByteArray} , t:: Union{String,ByteArray} , k)
282
+ _rsearchindex (s:: String , t:: String , i:: Integer ) =
283
+ _rsearchindex (Vector {UInt8} (s), Vector {UInt8} (t), i)
284
+
285
+ function _rsearchindex (s:: ByteArray , t:: ByteArray , k:: Integer )
280
286
n = sizeof (t)
281
287
m = sizeof (s)
282
288
@@ -285,7 +291,7 @@ function _rsearchindex(s::Union{String,ByteArray}, t::Union{String,ByteArray}, k
285
291
elseif m == 0
286
292
return 0
287
293
elseif n == 1
288
- return rsearch (s, _nthbyte (t,1 ), k)
294
+ return findprev ( equalto ( _nthbyte (t,1 )), s , k)
289
295
end
290
296
291
297
w = m - n
@@ -342,7 +348,7 @@ rsearchindex(s::ByteArray, t::ByteArray, i::Integer) = _rsearchindex(s,t,i)
342
348
"""
343
349
rsearchindex(s::AbstractString, substring, [start::Integer])
344
350
345
- Similar to [ `rsearch`](@ref) , but return only the start index at which the substring is found, or `0` if it is not.
351
+ Similar to `rsearch`, but return only the start index at which the substring is found, or `0` if it is not.
346
352
347
353
# Examples
348
354
```jldoctest
@@ -360,7 +366,7 @@ function rsearchindex(s::String, t::String)
360
366
# Check for fast case of a single byte
361
367
# (for multi-byte UTF-8 sequences, use rsearchindex instead)
362
368
if endof (t) == 1
363
- rsearch (s, t[1 ])
369
+ findprev ( equalto ( t[1 ]), s )
364
370
else
365
371
_rsearchindex (s, t, sizeof (s))
366
372
end
@@ -370,7 +376,7 @@ function rsearchindex(s::String, t::String, i::Integer)
370
376
# Check for fast case of a single byte
371
377
# (for multi-byte UTF-8 sequences, use rsearchindex instead)
372
378
if endof (t) == 1
373
- rsearch (s, t[1 ], i)
379
+ findprev ( equalto ( t[1 ]), s , i)
374
380
elseif endof (t) != 0
375
381
_rsearchindex (s, t, nextind (s, i)- 1 )
376
382
elseif i > sizeof (s)
@@ -391,8 +397,9 @@ function _rsearch(s, t, i::Integer)
391
397
end
392
398
end
393
399
394
- rsearch (s:: AbstractString , t:: AbstractString , i:: Integer = endof (s)) = _rsearch (s, t, i)
395
- rsearch (s:: ByteArray , t:: ByteArray , i:: Integer = endof (s)) = _rsearch (s, t, i)
400
+ findprev (t:: AbstractString , s:: AbstractString , i:: Integer ) = _rsearch (s, t, i)
401
+ # TODO : remove?
402
+ findprev (t:: ByteArray , s:: ByteArray , i:: Integer ) = _rsearch (s, t, i)
396
403
397
404
"""
398
405
contains(haystack::AbstractString, needle::Union{AbstractString,Char})
0 commit comments