Skip to content

Commit 228968b

Browse files
committed
Fix str_next and findfirst issues
1 parent 077b721 commit 228968b

5 files changed

Lines changed: 23 additions & 17 deletions

File tree

src/compare.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ end
5454
while pnt < fin
5555
str_done(b, pos) && return 1
5656
c1, pnt = _nextcp(C, pnt)
57-
ch, pos = str_next(b, pos)
57+
ch, pos = iterate(b, pos)
5858
c2 = ch%UInt32
5959
c1 == c2 || return ifelse(c1 < c2, -1, 1)
6060
end
@@ -93,7 +93,7 @@ function _cpeq(a::MaybeSub{T}, b) where {C<:CSE, T<:Str{C}}
9393
while pnt < fin
9494
str_done(b, pos) && return false
9595
c1, pnt = _nextcp(C, pnt)
96-
ch, pos = str_next(b, pos)
96+
ch, pos = iterate(b, pos)
9797
c1 == codepoint(ch) || return false
9898
end
9999
true

src/search.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ Base.findlast(a::Str, b::AbstractString) = nothing_sentinel(find(Last, a, b))
130130
Base.findnext(a::Str, b::AbstractString, i) = nothing_sentinel(find(Fwd, a, b, i))
131131
Base.findprev(a::Str, b::AbstractString, i) = nothing_sentinel(find(Rev, a, b, i))
132132

133+
# Fix ambiguities caused by addition of new findfirst definition to base
134+
Base.findfirst(a::AbstractChar, b::Str) = nothing_sentinel(find(First, a, b))
135+
Base.findlast(a::AbstractChar, b::Str) = nothing_sentinel(find(Last, a, b))
136+
Base.findnext(a::AbstractChar, b::Str, i) = nothing_sentinel(find(Fwd, a, b, i))
137+
Base.findprev(a::AbstractChar, b::Str, i) = nothing_sentinel(find(Rev, a, b, i))
138+
133139
function find(::Type{D}, fun::Function, str::AbstractString, pos::Integer) where {D<:Direction}
134140
pos < Int(D===Fwd) && (@boundscheck boundserr(str, pos); return 0)
135141
if pos > (len = ncodeunits(str))
@@ -189,7 +195,7 @@ function find(::Type{D}, needle::AbstractString, str::AbstractString,
189195
@inbounds is_valid(str, pos) || index_error(str, pos)
190196
(tlen = ncodeunits(needle)) == 0 && return pos:pos-1
191197
(cmp = CanContain(str, needle)) === NoCompare() && return _not_found
192-
@inbounds ch, nxt = str_next(needle, 1)
198+
@inbounds ch, nxt = iterate(needle, 1)
193199
is_valid(eltype(str), ch) || return _not_found
194200
# Check if single character
195201
if nxt > tlen
@@ -205,7 +211,7 @@ function find(::Type{T}, needle::AbstractString, str::AbstractString) where {T<:
205211
pos = T === First ? 1 : thisind(str, slen)
206212
(tlen = ncodeunits(needle)) == 0 && return pos:(pos-1)
207213
(cmp = CanContain(str, needle)) === NoCompare() && return _not_found
208-
@inbounds ch, nxt = str_next(needle, 1)
214+
@inbounds ch, nxt = iterate(needle, 1)
209215
is_valid(eltype(str), ch) || return _not_found
210216
# Check if single character
211217
if nxt > tlen
@@ -298,8 +304,8 @@ end
298304
"""Compare two strings, starting at nxtstr and nxtsub"""
299305
@inline function _cmp_str(str, strpos, endpos, sub, subpos, endsub)
300306
while strpos <= endpos
301-
c, strnxt = str_next(str, strpos)
302-
d, subpos = str_next(sub, subpos)
307+
c, strnxt = iterate(str, strpos)
308+
d, subpos = iterate(sub, subpos)
303309
c == d || break
304310
subpos > endsub && return strpos
305311
strpos = strnxt

src/support.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ function unsafe_check_string(str::T;
264264
totalchar = latin1byte = num2byte = num3byte = num4byte = invalids = 0
265265
pos = 1
266266
@inbounds while !str_done(str, pos)
267-
chr, nxt = str_next(str, pos)
267+
chr, nxt = iterate(str, pos)
268268
ch = chr%UInt32
269269
totalchar += 1
270270
if ch > 0x7f
@@ -288,7 +288,7 @@ function unsafe_check_string(str::T;
288288
break
289289
end
290290
# next character *must* be a trailing surrogate character
291-
chr, nxt = str_next(str, nxt)
291+
chr, nxt = iterate(str, nxt)
292292
if !is_surrogate_trail(chr)
293293
accept_invalids || strerror(StrErrors.NOT_TRAIL, pos, chr)
294294
invalids += 1

src/utf8.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ _iterate(::MultiCU, ::Type{T}, str::SubString{<:Str{RawUTF8CSE}}, pos::Int) wher
387387
end
388388

389389
_next(::MultiCU, ::Type{T}, str::Str{RawUTF8CSE}, pos::Int) where {T} =
390-
str_next(str.data, pos)
390+
iterate(str.data, pos)
391391
_next(::MultiCU, ::Type{T}, str::SubString{<:Str{RawUTF8CSE}}, pos::Int) where {T} =
392-
str_next(SubString(str.string.data, str.offset + pos, str.offset + ncodeunits(str)), 1)
392+
iterate(SubString(str.string.data, str.offset + pos, str.offset + ncodeunits(str)), 1)
393393

394394
## overload methods for efficiency ##
395395

test/basic.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ let
305305

306306
@test lastindex(srep) == 7
307307

308-
@test str_next(srep, 3) == ('β',5)
309-
@test str_next(srep, 7) == ('β',9)
308+
@test iterate(srep, 3) == ('β',5)
309+
@test iterate(srep, 7) == ('β',9)
310310

311311
@test srep[7] == 'β'
312312
@test_throws StringIndexError srep[8]
@@ -340,8 +340,8 @@ end
340340
@test_throws MethodError codeunit(tstr, true)
341341
@test_throws MethodError isvalid(tstr, 1)
342342
@test_throws MethodError isvalid(tstr, true)
343-
@test_throws MethodError str_next(tstr, 1)
344-
@test_throws MethodError str_next(tstr, true)
343+
@test_throws MethodError iterate(tstr, 1)
344+
@test_throws MethodError iterate(tstr, true)
345345
@test_throws MethodError lastindex(tstr)
346346

347347
gstr = GenericString("12")
@@ -611,7 +611,7 @@ end
611611
for st in ("Hello", "Σ", "こんにちは", "😊😁")
612612
local s
613613
s = ST(st)
614-
@test str_next(s, lastindex(s))[2] > sizeof(s)
614+
@test iterate(s, lastindex(s))[2] > sizeof(s)
615615
@test nextind(s, lastindex(s)) > sizeof(s)
616616
end
617617
end
@@ -915,7 +915,7 @@ function testbin(::Type{ST}) where {ST}
915915
b"\xf8\x9f\x98\x84", b"\xf8\x9f\x98\x84z")),
916916
s in lst
917917
st = ST(s)
918-
@test str_next(st, 1)[2] == 2
918+
@test iterate(st, 1)[2] == 2
919919
@test nextind(st, 1) == 2
920920
end
921921

@@ -930,7 +930,7 @@ function testbin(::Type{ST}) where {ST}
930930
(s, r) in lst
931931
st = ST(s)
932932
(ST === BinaryStr || ST === Text1Str) && (r = 2)
933-
@test str_next(st, 1)[2] == r
933+
@test iterate(st, 1)[2] == r
934934
@test nextind(st, 1) == r
935935
end
936936
end

0 commit comments

Comments
 (0)