Skip to content

Commit

Permalink
More meticulously check indices in eachregion
Browse files Browse the repository at this point in the history
Instead of just assuming that pos-1 is a valid string index, along with
all other regions, use lastind/thisind to ensure they are.
  • Loading branch information
serenity4 authored and tecosaur committed Sep 8, 2024
1 parent 02cd20b commit a0d557e
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/regioniterator.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function eachregion(s::AnnotatedString, subregion::UnitRange{Int}=firstindex(s):
annots = Vector{Vector{Pair{Symbol, Any}}}()
pos = first(events).pos
if pos > first(subregion)
push!(regions, first(subregion):pos-1)
push!(regions, thisind(s, first(subregion)):prevind(s, pos))
push!(annots, [])
end
activelist = Int[]
Expand All @@ -63,7 +63,7 @@ function eachregion(s::AnnotatedString, subregion::UnitRange{Int}=firstindex(s):
end
end
if last(events).pos < nextind(s, last(subregion))
push!(regions, last(events).pos:last(subregion))
push!(regions, last(events).pos:thisind(s, last(subregion)))
push!(annots, [])
end
RegionIterator(s.string, regions, annots)
Expand Down Expand Up @@ -95,7 +95,7 @@ function annotation_events(s::AbstractString, annots::Vector{Tuple{UnitRange{Int
if !isempty(intersect(subregion, region))
start, stop = max(first(subregion), first(region)), min(last(subregion), last(region))
start <= stop || continue # Currently can't handle empty regions
push!(events, (pos=start, active=true, index=i))
push!(events, (pos=thisind(s, start), active=true, index=i))
push!(events, (pos=nextind(s, stop), active=false, index=i))
end
end
Expand Down
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ choppkg(s::String) = chopprefix(s, "StyledStrings.")
("b", [:face => :blue, :face => :red, :face => :green, :face => :yellow]),
("c", [:face => :blue, :face => :red, :face => :green]),
("d", [:face => :blue])]
# Region starting after a character spanning multiple codepoints.
@test annregions("𝟏x", [(1:4, :face => :red)]) ==
[("𝟏", [:face => :red]),
("x", [])]
end

@testset "SimpleColor" begin
Expand Down

0 comments on commit a0d557e

Please sign in to comment.