Skip to content

Commit 6510b05

Browse files
committed
Add public function unannotate
1 parent c3f944f commit 6510b05

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

base/public.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ public
108108
# Strings
109109
escape_raw_string,
110110
unsafe_substring,
111+
unannotate,
111112

112113
# IO
113114
# types

base/strings/annotated.jl

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,29 @@ eltype(::Type{<:AnnotatedString{S}}) where {S} = AnnotatedChar{eltype(S)}
158158
firstindex(s::AnnotatedString) = firstindex(s.string)
159159
lastindex(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))
165184
end
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

213232
function ==(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)
215234
end
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

Comments
 (0)