Skip to content

Commit 948e8be

Browse files
committed
Use a macro to avoid runtime eval
This should be a bit better in terms of codegen.
1 parent 4cc2f41 commit 948e8be

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/io.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ function _ansi_writer(io::IO, s::Union{<:AnnotatedString, SubString{<:AnnotatedS
248248
string_writer(io, s.string)
249249
elseif s isa SubString
250250
string_writer(
251-
io, eval(Expr(:new, SubString{typeof(s.string.string)}, s.string.string, s.offset, s.ncodeunits)))
251+
io, AnnotatedStrings.@raw_substring(typeof(s.string.string), s.string.string, s.offset, s.ncodeunits))
252252
end
253253
end
254254

src/strings/annotated.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,16 +206,16 @@ cmp(a::AnnotatedString, b::AnnotatedString) = cmp(a.string, b.string)
206206
# To prevent substring equality from hitting the generic fallback
207207

208208
function ==(a::SubString{<:AnnotatedString}, b::SubString{<:AnnotatedString})
209-
eval(Expr(:new, SubString{typeof(a.string.string)}, a.string.string, a.offset, a.ncodeunits)) ==
210-
eval(Expr(:new, SubString{typeof(b.string.string)}, b.string.string, b.offset, b.ncodeunits)) &&
209+
@raw_substring(typeof(a.string.string), a.string.string, a.offset, a.ncodeunits) ==
210+
@raw_substring(typeof(b.string.string), b.string.string, b.offset, b.ncodeunits) &&
211211
annotations(a) == annotations(b)
212212
end
213213

214214
==(a::SubString{<:AnnotatedString}, b::AnnotatedString) =
215-
annotations(a) == annotations(b) && eval(Expr(:new, SubString{typeof(a.string.string)}, a.string.string, a.offset, a.ncodeunits)) == b.string
215+
annotations(a) == annotations(b) && @raw_substring(typeof(a.string.string), a.string.string, a.offset, a.ncodeunits) == b.string
216216

217217
==(a::SubString{<:AnnotatedString}, b::AbstractString) =
218-
isempty(annotations(a)) && eval(Expr(:new, SubString{typeof(a.string.string)}, a.string.string, a.offset, a.ncodeunits)) == b
218+
isempty(annotations(a)) && @raw_substring(typeof(a.string.string), a.string.string, a.offset, a.ncodeunits) == b
219219

220220
==(a::AbstractString, b::SubString{<:AnnotatedString}) = b == a
221221

@@ -264,7 +264,7 @@ function annotatedstring(xs...)
264264
push!(annotations, (rstart:rstop, annot))
265265
end
266266
end
267-
print(s, eval(Expr(:new, SubString{typeof(x.string.string)}, x.string.string, x.offset, x.ncodeunits)))
267+
print(s, @raw_substring(typeof(x.string.string), x.string.string, x.offset, x.ncodeunits))
268268
elseif x isa AnnotatedChar
269269
for annot in x.annotations
270270
push!(annotations, (1+size:1+size, annot))

src/strings/regex.jl

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@ end
2020

2121
function _annotatedmatch(m::RegexMatch, str::AnnotatedString{S}) where {S<:AbstractString}
2222
AnnotatedRegexMatch{AnnotatedString{S}}(
23-
# It's surprisingly annoying to clone a substring,
24-
# thanks to that pesky inner constructor.
25-
eval(Expr(:new, SubString{AnnotatedString{S}}(
26-
str, m.match.offset, m.match.ncodeunits))),
23+
@raw_substring(AnnotatedString{S}, str, m.match.offset, m.match.ncodeunits),
2724
Union{Nothing,SubString{AnnotatedString{S}}}[
2825
if !isnothing(cap)
29-
eval(Expr(:new, SubString{AnnotatedString{S}}(
30-
str, cap.offset, cap.ncodeunits)))
26+
@raw_substring(AnnotatedString{S},
27+
str, cap.offset, cap.ncodeunits)
3128
end for cap in m.captures],
3229
m.offset, m.offsets, m.regex)
3330
end

src/strings/strings.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import Base: @propagate_inbounds, AbstractPipe, TTY, String, cmp, codepoint,
1111

1212
using ..Compat
1313

14+
# It's surprisingly annoying to clone a substring,
15+
# thanks to that pesky inner constructor.
16+
macro raw_substring(T, str, offset, ncu)
17+
esc(Expr(:new, Expr(:curly, :SubString, T), str, offset, ncu))
18+
end
19+
1420
include(joinpath(dirname(@__DIR__), "compat.jl"))
1521
include("annotated.jl")
1622

0 commit comments

Comments
 (0)