@@ -6,7 +6,7 @@ function findnext(pred::EqualTo{Char}, s::String, i::Integer)
6
6
throw (BoundsError (s, i))
7
7
end
8
8
@inbounds isvalid (s, i) || string_index_err (s, i)
9
- c = pred. x
9
+ c = pred. x
10
10
c ≤ ' \x 7f' && return _search (s, c % UInt8, i)
11
11
while true
12
12
i = _search (s, first_utf8_byte (c), i)
146
146
_nthbyte (s:: String , i) = codeunit (s, i)
147
147
_nthbyte (a:: ByteArray , i) = a[i]
148
148
149
- _searchindex (s:: String , t:: String , i:: Integer ) =
149
+ function _searchindex (s:: String , t:: String , i:: Integer )
150
+ # Check for fast case of a single byte
151
+ endof (t) == 1 && return findnext (equalto (t[1 ]), s, i)
150
152
_searchindex (Vector {UInt8} (s), Vector {UInt8} (t), i)
153
+ end
151
154
152
155
function _searchindex (s:: ByteArray , t:: ByteArray , i:: Integer )
153
156
n = sizeof (t)
@@ -210,43 +213,10 @@ function _searchindex(s::ByteArray, t::ByteArray, i::Integer)
210
213
0
211
214
end
212
215
213
- searchindex (s:: ByteArray , t:: ByteArray , i:: Integer ) = _searchindex (s,t,i)
214
-
215
- """
216
- searchindex(s::AbstractString, substring, [start::Integer])
217
-
218
- Similar to `search`, but return only the start index at which
219
- the substring is found, or `0` if it is not.
220
-
221
- # Examples
222
- ```jldoctest
223
- julia> searchindex("Hello to the world", "z")
224
- 0
225
-
226
- julia> searchindex("JuliaLang","Julia")
227
- 1
228
-
229
- julia> searchindex("JuliaLang","Lang")
230
- 6
231
- ```
232
- """
233
- searchindex (s:: AbstractString , t:: AbstractString , i:: Integer ) = _searchindex (s,t,i)
234
- searchindex (s:: AbstractString , t:: AbstractString ) = searchindex (s,t,start (s))
235
- searchindex (s:: AbstractString , c:: Char , i:: Integer ) = _searchindex (s,c,i)
236
- searchindex (s:: AbstractString , c:: Char ) = searchindex (s,c,start (s))
237
-
238
- function searchindex (s:: String , t:: String , i:: Integer = 1 )
239
- # Check for fast case of a single byte
240
- # (for multi-byte UTF-8 sequences, use searchindex on byte arrays instead)
241
- if endof (t) == 1
242
- findnext (equalto (t[1 ]), s, i)
243
- else
244
- _searchindex (s, t, i)
245
- end
246
- end
247
-
248
- function _search (s, t, i:: Integer )
249
- idx = searchindex (s,t,i)
216
+ function _search (s:: Union{AbstractString,ByteArray} ,
217
+ t:: Union{AbstractString,Char,Int8,UInt8} ,
218
+ i:: Integer )
219
+ idx = _searchindex (s,t,i)
250
220
if isempty (t)
251
221
idx: idx- 1
252
222
else
@@ -281,8 +251,6 @@ julia> findnext("Julia", "JuliaLang", 2)
281
251
```
282
252
"""
283
253
findnext (t:: AbstractString , s:: AbstractString , i:: Integer ) = _search (s, t, i)
284
- # TODO : remove?
285
- findnext (t:: ByteArray , s:: ByteArray , i:: Integer ) = _search (s, t, i)
286
254
287
255
"""
288
256
findlast(pattern::AbstractString, string::AbstractString)
@@ -353,8 +321,21 @@ function _rsearchindex(s::AbstractString,
353
321
end
354
322
end
355
323
356
- _rsearchindex (s:: String , t:: String , i:: Integer ) =
357
- _rsearchindex (Vector {UInt8} (s), Vector {UInt8} (t), i)
324
+ function _rsearchindex (s:: String , t:: String , i:: Integer )
325
+ # Check for fast case of a single byte
326
+ if endof (t) == 1
327
+ return findprev (equalto (t[1 ]), s, i)
328
+ elseif endof (t) != 0
329
+ j = i ≤ ncodeunits (s) ? nextind (s, i)- 1 : i
330
+ return _rsearchindex (Vector {UInt8} (s), Vector {UInt8} (t), j)
331
+ elseif i > sizeof (s)
332
+ return 0
333
+ elseif i == 0
334
+ return 1
335
+ else
336
+ return i
337
+ end
338
+ end
358
339
359
340
function _rsearchindex (s:: ByteArray , t:: ByteArray , k:: Integer )
360
341
n = sizeof (t)
@@ -417,54 +398,10 @@ function _rsearchindex(s::ByteArray, t::ByteArray, k::Integer)
417
398
0
418
399
end
419
400
420
- rsearchindex (s:: ByteArray , t:: ByteArray , i:: Integer ) = _rsearchindex (s,t,i)
421
-
422
- """
423
- rsearchindex(s::AbstractString, substring, [start::Integer])
424
-
425
- Similar to `rsearch`, but return only the start index at which the substring is found, or `0` if it is not.
426
-
427
- # Examples
428
- ```jldoctest
429
- julia> rsearchindex("aaabbb","b")
430
- 6
431
-
432
- julia> rsearchindex("aaabbb","a")
433
- 3
434
- ```
435
- """
436
- rsearchindex (s:: AbstractString , t:: AbstractString , i:: Integer ) = _rsearchindex (s,t,i)
437
- rsearchindex (s:: AbstractString , t:: AbstractString ) = (isempty (s) && isempty (t)) ? 1 : rsearchindex (s,t,endof (s))
438
-
439
- function rsearchindex (s:: String , t:: String )
440
- # Check for fast case of a single byte
441
- # (for multi-byte UTF-8 sequences, use rsearchindex instead)
442
- if endof (t) == 1
443
- findprev (equalto (t[1 ]), s)
444
- else
445
- _rsearchindex (s, t, sizeof (s))
446
- end
447
- end
448
-
449
- function rsearchindex (s:: String , t:: String , i:: Integer )
450
- # Check for fast case of a single byte
451
- # (for multi-byte UTF-8 sequences, use rsearchindex instead)
452
- if endof (t) == 1
453
- findprev (equalto (t[1 ]), s, i)
454
- elseif endof (t) != 0
455
- j = i ≤ ncodeunits (s) ? nextind (s, i)- 1 : i
456
- _rsearchindex (s, t, j)
457
- elseif i > sizeof (s)
458
- return 0
459
- elseif i == 0
460
- return 1
461
- else
462
- return i
463
- end
464
- end
465
-
466
- function _rsearch (s, t, i:: Integer )
467
- idx = rsearchindex (s,t,i)
401
+ function _rsearch (s:: Union{AbstractString,ByteArray} ,
402
+ t:: Union{AbstractString,Char,Int8,UInt8} ,
403
+ i:: Integer )
404
+ idx = _rsearchindex (s,t,i)
468
405
if isempty (t)
469
406
idx: idx- 1
470
407
else
@@ -499,8 +436,6 @@ julia> findprev("Julia", "JuliaLang", 6)
499
436
```
500
437
"""
501
438
findprev (t:: AbstractString , s:: AbstractString , i:: Integer ) = _rsearch (s, t, i)
502
- # TODO : remove?
503
- findprev (t:: ByteArray , s:: ByteArray , i:: Integer ) = _rsearch (s, t, i)
504
439
505
440
"""
506
441
contains(haystack::AbstractString, needle::Union{AbstractString,Char})
@@ -513,6 +448,7 @@ julia> contains("JuliaLang is pretty cool!", "Julia")
513
448
true
514
449
```
515
450
"""
516
- contains (haystack:: AbstractString , needle:: Union{AbstractString,Char} ) = searchindex (haystack,needle)!= 0
451
+ contains (haystack:: AbstractString , needle:: Union{AbstractString,Char} ) =
452
+ _searchindex (haystack, needle, start (haystack)) != 0
517
453
518
454
in (:: AbstractString , :: AbstractString ) = error (" use contains(x,y) for string containment" )
0 commit comments