Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialised substring equality for annotated strs #54302

Merged
merged 1 commit into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions base/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,24 @@ cmp(a::AnnotatedString, b::AnnotatedString) = cmp(a.string, b.string)
==(a::AnnotatedString, b::AbstractString) = isempty(a.annotations) && a.string == b
==(a::AbstractString, b::AnnotatedString) = isempty(b.annotations) && a == b.string

# To prevent substring equality from hitting the generic fallback

function ==(a::SubString{<:AnnotatedString}, b::SubString{<:AnnotatedString})
SubString(a.string.string, a.offset, a.ncodeunits, Val(:noshift)) ==
SubString(b.string.string, b.offset, b.ncodeunits, Val(:noshift)) &&
annotations(a) == annotations(b)
end

==(a::SubString{<:AnnotatedString}, b::AnnotatedString) =
annotations(a) == annotations(b) && SubString(a.string.string, a.offset, a.ncodeunits, Val(:noshift)) == b.string

==(a::SubString{<:AnnotatedString}, b::AbstractString) =
isempty(annotations(a)) && SubString(a.string.string, a.offset, a.ncodeunits, Val(:noshift)) == b

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

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

"""
annotatedstring(values...)

Expand Down
4 changes: 4 additions & 0 deletions test/strings/annotated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
@test "a" * str == Base.AnnotatedString("asome string")
@test str * "a" == Base.AnnotatedString("some stringa")
@test str * str == Base.AnnotatedString("some stringsome string")
@test str[3:4] == SubString("me")
@test SubString("me") == str[3:4]
Base.annotate!(str, 1:4, :thing => 0x01)
Base.annotate!(str, 6:11, :other => 0x02)
Base.annotate!(str, 1:11, :all => 0x03)
Expand All @@ -21,6 +23,8 @@
# └───┰─────┘
# :all
@test str[3:4] == SubString(str, 3, 4)
@test str[3:4] != SubString("me")
@test SubString("me") != str[3:4]
@test Base.AnnotatedString(str[3:4]) ==
Base.AnnotatedString("me", [(1:2, :thing => 0x01), (1:2, :all => 0x03)])
@test Base.AnnotatedString(str[3:6]) ==
Expand Down