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