@@ -158,10 +158,29 @@ eltype(::Type{<:AnnotatedString{S}}) where {S} = AnnotatedChar{eltype(S)}
158158firstindex (s:: AnnotatedString ) = firstindex (s. string)
159159lastindex (s:: AnnotatedString ) = lastindex (s. string)
160160
161- plain_substring (s:: AnnotatedString ) = @inbounds unsafe_substring (s. string, 1 , ncodeunits (s))
161+ """
162+ unannotate(s::AnnotatedString{S})::S
163+ unannotate(s::SubString{AnnotatedString{S}})::SubString{S}
164+
165+ Get the underlying string of `s`, without copying.
166+
167+ # Examples
168+ ```jldoctest; setup=:(using Base: AnnotatedString)
169+ julia> s = AnnotatedString("abcde", [(1:3, :A, 4)])
170+ "abcde"
171+
172+ julia> u = unannotate(s)
173+ "abcde"
174+
175+ julia> typeof(u)
176+ String
177+ ```
178+ """
179+ unannotate (s:: AnnotatedString ) = s. string
162180
163- function plain_substring (s:: SubString{<:AnnotatedString} )
164- @inbounds unsafe_substring (s. string. string, s. offset + 1 , s. ncodeunits)
181+ function unannotate (s:: SubString{<:AnnotatedString} )
182+ start_index = first (parentindices (s)[1 ])
183+ @inbounds unsafe_substring (parent (s). string, start_index, ncodeunits (s))
165184end
166185
167186
@@ -211,14 +230,14 @@ cmp(a::AnnotatedString, b::AnnotatedString) = cmp(a.string, b.string)
211230# To prevent substring equality from hitting the generic fallback
212231
213232function == (a:: SubString{<:AnnotatedString} , b:: SubString{<:AnnotatedString} )
214- plain_substring (a) == plain_substring (b) && annotations (a) == annotations (b)
233+ unannotate (a) == unannotate (b) && annotations (a) == annotations (b)
215234end
216235
217236== (a:: SubString{<:AnnotatedString} , b:: AnnotatedString ) =
218- annotations (a) == annotations (b) && plain_substring (a) == b. string
237+ annotations (a) == annotations (b) && unannotate (a) == b. string
219238
220239== (a:: SubString{<:AnnotatedString} , b:: AbstractString ) =
221- isempty (annotations (a)) && plain_substring (a) == b
240+ isempty (annotations (a)) && unannotate (a) == b
222241
223242== (a:: AbstractString , b:: SubString{<:AnnotatedString} ) = b == a
224243
@@ -267,7 +286,7 @@ function annotatedstring(xs...)
267286 push! (annotations, setindex (annot, rstart: rstop, :region ))
268287 end
269288 end
270- print (s, plain_substring (x))
289+ print (s, unannotate (x))
271290 elseif x isa AnnotatedChar
272291 for annot in x. annotations
273292 push! (annotations, (region= 1 + size: 1 + size, annot... ))
0 commit comments