Skip to content

Commit

Permalink
Handle edge case: Zero width delimiter
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Oct 10, 2023
1 parent efddaee commit 7967578
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,7 @@ julia> collect(eachrsplit(a, ".")) == ["ch", "r", "Ma"]
true
julia> collect(eachrsplit(a, "."; limit=2)) == ["ch", "Ma.r"]
true
```
"""
function eachrsplit end
Expand Down Expand Up @@ -668,7 +669,12 @@ function Base.iterate(it::RSplitIterator, (to, remaining_splits)=(lastindex(it.s
break
else
from = nextind(it.str, last(pos))
next_to = prevind(it.str, first(pos))
# pos can be empty if we search for a zero-width delimiter, in which
# case pos is to:to-1.
# In this case, next_to must be to - 1, except if to is 0 or 1, in
# which case, we must stop iteration for some reason.
next_to = (isempty(pos) & (to < 2)) ? -1 : prevind(it.str, first(pos))

# If the element we emit is empty, discard it based on keepempty
if from > to && !(it.keepempty)
to = next_to
Expand Down

0 comments on commit 7967578

Please sign in to comment.