Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [Unreleased]

### Fixed

- `findlast`, `findprev`, and searches with an inline string needle in a
`String` haystack (`occursin`, `findfirst`, `replace`, `split`, ...) now give
the same results as `String`. They previously used Base's generic
character-level search, which disagreed with `String` on malformed UTF-8,
threw for `findlast("", String7(""))`, and threw for a `findprev(==(c), s, i)`
start index inside a character.

## 2.0.0

### Breaking changes
Expand Down
13 changes: 13 additions & 0 deletions src/InlineStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,19 @@ function Base._searchindex(s::InlineString, t::Union{String, SubString{String},
return 0
end

# The other searches go through `String`'s methods so their results match
# `String`. Base's generic `AbstractString` fallbacks compare characters rather
# than bytes (different on malformed UTF-8), reject an empty needle at index 0,
# and reject a `findprev` start index inside a character.
Base._searchindex(s::Union{String, SubString{String}}, t::InlineString, i::Integer) =
Base._searchindex(s, String(t), i)
Base._rsearchindex(s::InlineString, t::Union{String, SubString{String}, InlineString}, i::Integer) =
Base._rsearchindex(String(s), String(t), i)
Base._rsearchindex(s::Union{String, SubString{String}}, t::InlineString, i::Integer) =
Base._rsearchindex(s, String(t), i)
Base.findprev(pred::Base.Fix2{<:Union{typeof(isequal), typeof(==)}, <:AbstractChar}, s::InlineString, i::Integer) =
findprev(pred, String(s), i)

function _searchbyte(s::InlineString, b::UInt8, i::Int)
bytes = _bytes(s)
@inbounds for k = i:ncodeunits(s)
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,25 @@ end
@test_throws BoundsError findnext(==('h'), s, 0)
@test_throws BoundsError findnext(==('h'), s, ncodeunits(s) + 2)
end
# Searching forward or backward, with the inline string as the haystack, the
# needle, or both, gives the `String` result, including for malformed UTF-8
# and empty needles.
for (str, t) in (("x\xe9\xa9", "x\xe9"), ("", ""), ("ab", ""), ("a\xe2\x82a\xe2", "a\xe2"))
for S in (String7, String255), (h, n) in ((S(str), t), (S(str), S(t)), (str, S(t)))
@test findfirst(n, h) == findfirst(t, str)
@test findlast(n, h) == findlast(t, str)
@test occursin(n, h) == occursin(t, str)
@test replace(h, n => "-") == replace(str, t => "-")
for i in 0:ncodeunits(str)+1
ref = try findprev(t, str, i) catch e; e end
if ref isa Exception
@test_throws typeof(ref) findprev(n, h, i)
else
@test findprev(n, h, i) == ref
end
end
end
end
end

@testset "Aqua" begin
Expand Down
Loading