Skip to content

Commit 162e37a

Browse files
tecosaurvtjnash
andcommitted
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 <[email protected]>
1 parent 2e1235e commit 162e37a

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

base/strings/annotated.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ A string with metadata, in the form of annotated regions.
77
88
More specifically, this is a simple wrapper around any other
99
[`AbstractString`](@ref) that allows for regions of the wrapped string to be
10-
annotated with labeled values.
10+
annotated with labelled values. The underlying string can be extracted by
11+
calling a string constructor with the `AnnotatedString` as the argument.
1112
1213
```text
1314
C
@@ -125,7 +126,10 @@ AnnotatedString(s::AnnotatedString, annots::Vector{Tuple{UnitRange{Int}, Pair{Sy
125126
AnnotatedChar(c::AnnotatedChar, annots::Vector{Pair{Symbol, Any}}) =
126127
AnnotatedChar(c.char, vcat(c.annotations, annots))
127128

128-
String(s::AnnotatedString{String}) = s.string # To avoid pointless overhead
129+
# To allow for generically de-annotating a string.
130+
(::Type{T})(s::AnnotatedString) where {T <: AbstractString} = T(s.string)
131+
String(s::AnnotatedString{String}) = s.string # To avoid pointless overhead (and avoid ambiguity)
132+
AnnotatedString(s::AnnotatedString) = s # To resolve an ambiguity
129133

130134
## Conversion/promotion ##
131135

0 commit comments

Comments
 (0)