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 c5d8484
Showing 1 changed file with 8 additions and 0 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

0 comments on commit c5d8484

Please sign in to comment.