Skip to content

Commit 6f27021

Browse files
committed
Specialised substring equality for annotated strs
The fact that only a string without annotations is equal to a non-annotated string (in order to preserve the transitivity of equality), makes the generic fallback methods for string comparison insufficient. As such, ==(::AnnoatedString, ::AbstractString) is implemented in annotated.jl, but this issue re-appears when dealing with substrings. The obvious solution is to just implement a specialised method for substrings. This does seem potentially a bit whack-a-mole, but I'm worried that cleverer solutions might come with subtle issues of their own. For now, let's try the simple and obvious solution, and improve it later if we can work out a nicer way of handling this issue in general.
1 parent 6023ad6 commit 6f27021

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

base/strings/annotated.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,24 @@ cmp(a::AnnotatedString, b::AnnotatedString) = cmp(a.string, b.string)
176176
==(a::AnnotatedString, b::AbstractString) = isempty(a.annotations) && a.string == b
177177
==(a::AbstractString, b::AnnotatedString) = isempty(b.annotations) && a == b.string
178178

179+
# To prevent substring equality from hitting the generic fallback
180+
181+
function ==(a::SubString{<:AnnotatedString}, b::SubString{<:AnnotatedString})
182+
SubString(a.string.string, a.offset, a.ncodeunits, Val(:noshift)) ==
183+
SubString(b.string.string, b.offset, b.ncodeunits, Val(:noshift)) &&
184+
annotations(a) == annotations(b)
185+
end
186+
187+
==(a::SubString{<:AnnotatedString}, b::AnnotatedString) =
188+
annotations(a) == annotations(b) && SubString(a.string.string, a.offset, a.ncodeunits, Val(:noshift)) == b.string
189+
190+
==(a::SubString{<:AnnotatedString}, b::AbstractString) =
191+
isempty(annotations(a)) && SubString(a.string.string, a.offset, a.ncodeunits, Val(:noshift)) == b
192+
193+
==(a::AbstractString, b::SubString{<:AnnotatedString}) = b == a
194+
195+
==(a::AnnotatedString, b::SubString{<:AnnotatedString}) = b == a
196+
179197
"""
180198
annotatedstring(values...)
181199

test/strings/annotated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
@test "a" * str == Base.AnnotatedString("asome string")
1313
@test str * "a" == Base.AnnotatedString("some stringa")
1414
@test str * str == Base.AnnotatedString("some stringsome string")
15+
@test str[3:4] == SubString("me")
16+
@test SubString("me") == str[3:4]
1517
Base.annotate!(str, 1:4, :thing => 0x01)
1618
Base.annotate!(str, 6:11, :other => 0x02)
1719
Base.annotate!(str, 1:11, :all => 0x03)
@@ -21,6 +23,8 @@
2123
# └───┰─────┘
2224
# :all
2325
@test str[3:4] == SubString(str, 3, 4)
26+
@test str[3:4] != SubString("me")
27+
@test SubString("me") != str[3:4]
2428
@test Base.AnnotatedString(str[3:4]) ==
2529
Base.AnnotatedString("me", [(1:2, :thing => 0x01), (1:2, :all => 0x03)])
2630
@test Base.AnnotatedString(str[3:6]) ==

0 commit comments

Comments
 (0)