Skip to content

Commit

Permalink
Make AnnotateChar equality consider annotations
Browse files Browse the repository at this point in the history
This is needed for consistency with the view of equality we've adopted
for AnnotatedStrings. By making AnnotateChar require annotations to be
equal, we preserve the quality that two strings are equal if all their
characters are equal.
  • Loading branch information
tecosaur committed Aug 11, 2024
1 parent 2e1235e commit cc009e1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
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

0 comments on commit cc009e1

Please sign in to comment.