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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StringViews"
uuid = "354b36f9-a18e-4713-926e-db85100087ba"
authors = ["Steven G. Johnson <stevenj@alum.mit.edu>"]
version = "1.3.5"
version = "1.3.6"

[compat]
julia = "1.6"
Expand Down
11 changes: 8 additions & 3 deletions src/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

nothing_sentinel(x) = iszero(x) ? nothing : x

function first_utf8_byte(x::Char)::UInt8
u = reinterpret(UInt32, x)
(u >> 24) % UInt8
end

function Base.findnext(pred::Base.Fix2{<:Union{typeof(isequal),typeof(==)},<:AbstractChar},
s::StringViewAndSub, i::Integer)
if i < 1 || i > sizeof(s)
i == sizeof(s) + 1 && return nothing
throw(BoundsError(s, i))
end
@inbounds isvalid(s, i) || Base.string_index_err(s, i)
c = pred.x
c = Char(pred.x)::Char
c ≤ '\x7f' && return nothing_sentinel(Base._search(s, c % UInt8, i))
while true
i = Base._search(s, Base.first_utf8_byte(c), i)
i = Base._search(s, first_utf8_byte(c), i)
i == 0 && return nothing
pred(s[i]) && return i
i = nextind(s, i)
Expand Down Expand Up @@ -45,7 +50,7 @@ function Base.findprev(pred::Base.Fix2{<:Union{typeof(isequal),typeof(==)},<:Abs
s::StringViewAndSub, i::Integer)
c = pred.x
c ≤ '\x7f' && return nothing_sentinel(Base._rsearch(s, c % UInt8, i))
b = Base.first_utf8_byte(c)
b = first_utf8_byte(c)
while true
i = Base._rsearch(s, b, i)
i == 0 && return nothing
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ end
pat = r"(\.[\d]{3})\d+" => s"\g<1>"
@test replace(String(copy(v)), pat) == replace(StringView(v), pat)
end

@test findfirst(==('ø'), StringView("abc")) === nothing
@test findfirst(==('ø'), StringView("abæø")) == 5
@test findlast(==('ø'), StringView("abc")) === nothing
@test findlast(==('ø'), StringView("abæø")) == 5
end

@testset "replace" begin
Expand Down
Loading