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

Make AnnotateChar equality consider annotations #55457

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,20 @@ cmp(a::AbstractString, b::AnnotatedString) = cmp(a, b.string)
# To avoid method ambiguity
cmp(a::AnnotatedString, b::AnnotatedString) = cmp(a.string, b.string)

# Annotated strings are equal if their strings and annotations are equal
==(a::AnnotatedString, b::AnnotatedString) =
a.string == b.string && a.annotations == b.annotations

==(a::AnnotatedString, b::AbstractString) = isempty(a.annotations) && a.string == b
==(a::AbstractString, b::AnnotatedString) = isempty(b.annotations) && a == b.string

# Annotated chars are equal if their chars and annotations are equal
==(a::AnnotatedChar, b::AnnotatedChar) =
a.char == b.char && a.annotations == b.annotations

==(a::AnnotatedChar, b::AbstractChar) = isempty(a.annotations) && a.char == b
==(a::AbstractChar, b::AnnotatedChar) = isempty(b.annotations) && a == b.char

# To prevent substring equality from hitting the generic fallback

function ==(a::SubString{<:AnnotatedString}, b::SubString{<:AnnotatedString})
Expand Down
6 changes: 3 additions & 3 deletions test/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ end
chr = Base.AnnotatedChar('c')
@test chr == Base.AnnotatedChar(chr.char, Pair{Symbol, Any}[])
str = Base.AnnotatedString("hmm", [(1:1, :attr => "h0h0"),
(1:2, :attr => "h0m1"),
(2:3, :attr => "m1m2")])
@test str[1] == Base.AnnotatedChar('h', Pair{Symbol, Any}[:attr => "h0h0"])
(1:2, :attr => "h0m1"),
(2:3, :attr => "m1m2")])
@test str[1] == Base.AnnotatedChar('h', Pair{Symbol, Any}[:attr => "h0h0", :attr => "h0m1"])
@test str[2] == Base.AnnotatedChar('m', Pair{Symbol, Any}[:attr => "h0m1", :attr => "m1m2"])
@test str[3] == Base.AnnotatedChar('m', Pair{Symbol, Any}[:attr => "m1m2"])
end
Expand Down