Skip to content

Commit cc009e1

Browse files
committed
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.
1 parent 2e1235e commit cc009e1

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

base/strings/annotated.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,20 @@ cmp(a::AbstractString, b::AnnotatedString) = cmp(a, b.string)
197197
# To avoid method ambiguity
198198
cmp(a::AnnotatedString, b::AnnotatedString) = cmp(a.string, b.string)
199199

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

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

207+
# Annotated chars are equal if their chars and annotations are equal
208+
==(a::AnnotatedChar, b::AnnotatedChar) =
209+
a.char == b.char && a.annotations == b.annotations
210+
211+
==(a::AnnotatedChar, b::AbstractChar) = isempty(a.annotations) && a.char == b
212+
==(a::AbstractChar, b::AnnotatedChar) = isempty(b.annotations) && a == b.char
213+
206214
# To prevent substring equality from hitting the generic fallback
207215

208216
function ==(a::SubString{<:AnnotatedString}, b::SubString{<:AnnotatedString})

test/strings/annotated.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ end
6565
chr = Base.AnnotatedChar('c')
6666
@test chr == Base.AnnotatedChar(chr.char, Pair{Symbol, Any}[])
6767
str = Base.AnnotatedString("hmm", [(1:1, :attr => "h0h0"),
68-
(1:2, :attr => "h0m1"),
69-
(2:3, :attr => "m1m2")])
70-
@test str[1] == Base.AnnotatedChar('h', Pair{Symbol, Any}[:attr => "h0h0"])
68+
(1:2, :attr => "h0m1"),
69+
(2:3, :attr => "m1m2")])
70+
@test str[1] == Base.AnnotatedChar('h', Pair{Symbol, Any}[:attr => "h0h0", :attr => "h0m1"])
7171
@test str[2] == Base.AnnotatedChar('m', Pair{Symbol, Any}[:attr => "h0m1", :attr => "m1m2"])
7272
@test str[3] == Base.AnnotatedChar('m', Pair{Symbol, Any}[:attr => "m1m2"])
7373
end

0 commit comments

Comments
 (0)