Skip to content

Commit

Permalink
Add specialised AnnotatedString pipe read/writes
Browse files Browse the repository at this point in the history
Ensure that when an AnnotatedIOBuffer is wrapped in an IOContext (or
similar AnnotatedPipe-based construct), that writes of annotated
strings/chars and reading out an AnnotatedString is unimpeded by the
IOContext wrapping.

Without these specialisations, the generic pipe_reader/pipe_writer
fallbacks will directly access the underlying IOBuffer and annotations
will be lost.

There are a number of scenarios in which one might want to combine an
AnnotatedIOBuffer and IOContext (for example setting the compact
property). Losing annotations in such scenarios is highly undesirable.

The read specialisation isn't actually needed to avoid this headache,
but seems nice to have for completeness.
  • Loading branch information
tecosaur committed Mar 21, 2024
1 parent bc7ba3d commit 0daade5
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,13 @@ function write(dest::AnnotatedIOBuffer, src::AnnotatedIOBuffer)
nb
end

# So that read/writes with `IOContext` (and any similar `AbstractPipe` wrappers)
# work as expected.
write(io::AbstractPipe, s::Union{AnnotatedString, SubString{<:AnnotatedString}}) =
write(pipe_writer(io), s)
write(io::AbstractPipe, c::AnnotatedChar) = write(pipe_writer(io), c)
read(io::AbstractPipe, T::Type{<:AnnotatedString}) = read(io.io, T)

function _clear_annotations_in_region!(annotations::Vector{Tuple{UnitRange{Int}, Pair{Symbol, Any}}}, span::UnitRange{Int})
# Clear out any overlapping pre-existing annotations.
filter!(((region, _),) -> first(region) < first(span) || last(region) > last(span), annotations)
Expand Down

0 comments on commit 0daade5

Please sign in to comment.