From a2cc90b1d9d5e19bcdab6541f0823113d565b2e4 Mon Sep 17 00:00:00 2001 From: TEC Date: Sun, 11 Aug 2024 16:26:39 +0800 Subject: [PATCH] Allow for generically extracting unannotated str As raised by Alexander Plavin, you may want to obtain a the underlying string of an AnnotatedString in non-String cases. However, there's no public API for doing so. Instead of just implementing this functionality for the String type, we can make it more generic. Co-authored-by: Jameson Nash --- base/strings/annotated.jl | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/base/strings/annotated.jl b/base/strings/annotated.jl index f077c577237d0..a7ccf00c258cd 100644 --- a/base/strings/annotated.jl +++ b/base/strings/annotated.jl @@ -7,7 +7,8 @@ A string with metadata, in the form of annotated regions. More specifically, this is a simple wrapper around any other [`AbstractString`](@ref) that allows for regions of the wrapped string to be -annotated with labeled values. +annotated with labelled values. The underlying string can be extracted by +calling a string constructor with the `AnnotatedString` as the argument. ```text C @@ -125,7 +126,10 @@ AnnotatedString(s::AnnotatedString, annots::Vector{Tuple{UnitRange{Int}, Pair{Sy AnnotatedChar(c::AnnotatedChar, annots::Vector{Pair{Symbol, Any}}) = AnnotatedChar(c.char, vcat(c.annotations, annots)) -String(s::AnnotatedString{String}) = s.string # To avoid pointless overhead +# To allow for generically de-annotating a string. +(::Type{T})(s::AnnotatedString{T}) where {T <: AbstractString} = T(s.string) +String(s::AnnotatedString{String}) = s.string # To avoid pointless overhead (and avoid ambiguity) +AnnotatedString(s::AnnotatedString) = s # To resolve an ambiguity ## Conversion/promotion ##