Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add back * for 1.10 without needing method overwriting #103

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 11 additions & 21 deletions src/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,17 @@ _isannotated(s) = _isannotated(typeof(s))
# actually from substring.jl
_isannotated(::SubString{T}) where {T} = _isannotated(T)

# NOTE Too invalidating:

# function (*)(s1::Union{AnnotatedChar, AnnotatedString}, s2::Union{AbstractChar, AbstractString})
# annotatedstring(s1, s2)
# end

# function (*)(s1::Union{AbstractChar, AbstractString}, s2::Union{AnnotatedChar, AnnotatedString})
# annotatedstring(s1, s2)
# end

# function (*)(s1::Union{AnnotatedChar, AnnotatedString}, s2::Union{AnnotatedChar, AnnotatedString})
# annotatedstring(s1, s2)
# end

# function (*)(s1::Union{AbstractChar, AbstractString}, s2::Union{AbstractChar, AbstractString}, ss::Union{AbstractChar, AbstractString}...)
# if _isannotated(s1) || _isannotated(s2) || any(_isannotated, ss)
# annotatedstring(s1, s2, ss...)
# else
# string(s1, s2, ss...)
# end
# end
# The following meta-programming will generate code like:
# Base.:*(s_annot::AnnotatedString, s::Union{AbstractChar,AbstractString}...) = annotatedstring(s_annot, s...)
# Base.:*(s1::Union{AbstractChar,AbstractString}, s_annot::AnnotatedString, s::Union{AbstractChar,AbstractString}...) = annotatedstring(s1, s_annot, s...)
# and so on.
for i in 0:31
front = [Symbol('s', j) for j in 1:i]
front_typed = [:($s::Union{AbstractChar,AbstractString}) for s in front]
@eval function Base.:*($(front_typed...), s_annot::AnnotatedString, s::Union{AbstractChar,AbstractString}...)
annotatedstring($(front...), s_annot, s...)
end
end

# # From io.jl
# join(iterator) = _join_preserve_annotations(iterator)
Expand Down
42 changes: 42 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,45 @@ end
@test printstyled(aio, "e", color=:green) |> isnothing
@test read(seekstart(aio), AnnotatedString) == styled"{bold:a}{italic:b}{underline:c}{inverse:d}{(fg=green):e}"
end

@testset "concatenation via *" begin
function check_annotated_equal(a::AbstractString, b::AbstractString)
if a isa AnnotatedString && b isa AnnotatedString
String(a) == String(b) && annotations(a) == annotations(b)
else
false
end
end

s_annot = styled"{red:hello}"

# Test (styled, styled)
@test check_annotated_equal(
s_annot * styled"{blue:world}",
styled"{red:hello}{blue:world}"
)

# Test (styled, regular)
@test check_annotated_equal(
s_annot * "world",
styled"{red:hello}world"
)

# Test (regular, styled)
@test check_annotated_equal(
"hello" * s_annot,
styled"hello{red:hello}"
)

# Test (styled, regular, styled)
@test check_annotated_equal(
s_annot * " " * styled"{blue:world}",
styled"{red:hello} {blue:world}"
)

# Test (regular, regular, styled)
@test check_annotated_equal(
"hello" * " " * s_annot,
styled"hello {red:hello}"
)
end
Loading