From cc009e1f59dacbaf03a6165c6084e58bc5c0bb48 Mon Sep 17 00:00:00 2001 From: TEC Date: Sun, 11 Aug 2024 16:11:59 +0800 Subject: [PATCH] Make AnnotateChar equality consider annotations 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. --- base/strings/annotated.jl | 8 ++++++++ test/strings/annotated.jl | 6 +++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/base/strings/annotated.jl b/base/strings/annotated.jl index f077c577237d0..2b8e6db6ce03c 100644 --- a/base/strings/annotated.jl +++ b/base/strings/annotated.jl @@ -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}) diff --git a/test/strings/annotated.jl b/test/strings/annotated.jl index e985c0b421a51..973afe47f6657 100644 --- a/test/strings/annotated.jl +++ b/test/strings/annotated.jl @@ -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