Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Construct substring end index using thisind #86

Merged
merged 1 commit into from
Sep 8, 2024
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
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
Loading