Skip to content

Commit 448314b

Browse files
serenity4tecosaur
andcommitted
More meticulously check indices in eachregion
Instead of just assuming that pos-1 is a valid string index, along with all other regions, use lastind/thisind to ensure they are. Co-authored-by: TEC <[email protected]>
1 parent 02cd20b commit 448314b

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/regioniterator.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function eachregion(s::AnnotatedString, subregion::UnitRange{Int}=firstindex(s):
4646
annots = Vector{Vector{Pair{Symbol, Any}}}()
4747
pos = first(events).pos
4848
if pos > first(subregion)
49-
push!(regions, first(subregion):pos-1)
49+
push!(regions, thisind(s, first(subregion)):prevind(s, pos))
5050
push!(annots, [])
5151
end
5252
activelist = Int[]
@@ -63,7 +63,7 @@ function eachregion(s::AnnotatedString, subregion::UnitRange{Int}=firstindex(s):
6363
end
6464
end
6565
if last(events).pos < nextind(s, last(subregion))
66-
push!(regions, last(events).pos:last(subregion))
66+
push!(regions, last(events).pos:thisind(s, last(subregion)))
6767
push!(annots, [])
6868
end
6969
RegionIterator(s.string, regions, annots)
@@ -95,7 +95,7 @@ function annotation_events(s::AbstractString, annots::Vector{Tuple{UnitRange{Int
9595
if !isempty(intersect(subregion, region))
9696
start, stop = max(first(subregion), first(region)), min(last(subregion), last(region))
9797
start <= stop || continue # Currently can't handle empty regions
98-
push!(events, (pos=start, active=true, index=i))
98+
push!(events, (pos=thisind(s, start), active=true, index=i))
9999
push!(events, (pos=nextind(s, stop), active=false, index=i))
100100
end
101101
end

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ choppkg(s::String) = chopprefix(s, "StyledStrings.")
7777
("b", [:face => :blue, :face => :red, :face => :green, :face => :yellow]),
7878
("c", [:face => :blue, :face => :red, :face => :green]),
7979
("d", [:face => :blue])]
80+
# Region starting after a character spanning multiple codepoints.
81+
@test annregions("𝟏x", [(1:4, :face => :red)]) ==
82+
[("𝟏", [:face => :red]),
83+
("x", [])]
8084
end
8185

8286
@testset "SimpleColor" begin

0 commit comments

Comments
 (0)