From 38607a6b8a535ce8ef0a596535510c39dfb363c5 Mon Sep 17 00:00:00 2001 From: Brandon Flores Date: Mon, 17 Jun 2024 18:09:45 -0500 Subject: [PATCH] Fixed method ambiguities for `Base.unsafe_convert()`, `Base.lstrip`, and `Base.rstrip` (#70) * Fixed method ambiguities for `Base.unsafe_convert()` * Fixed method ambiguities from `lstrip` and `rstrip` * Added tests for new `Base.lstrip`/`Base.rstrip` methods --- src/InlineStrings.jl | 13 +++++++++++++ test/runtests.jl | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/InlineStrings.jl b/src/InlineStrings.jl index db01276..6a107f1 100644 --- a/src/InlineStrings.jl +++ b/src/InlineStrings.jl @@ -168,6 +168,10 @@ Base.unsafe_convert(::Type{Ptr{UInt8}}, x::Ref{T}) where {T <: InlineString} = Ptr{UInt8}(pointer_from_objref(x)) Base.unsafe_convert(::Type{Ptr{Int8}}, x::Ref{T}) where {T <: InlineString} = Ptr{Int8}(pointer_from_objref(x)) +# Resolve method ambiguities +Base.unsafe_convert(P::Type{Ptr{UInt8}}, x::Ptr{<:InlineString}) = convert(P, x) +Base.unsafe_convert(P::Type{Ptr{Int8}}, x::Ptr{<:InlineString}) = convert(P, x) + Base.unsafe_convert(::Type{Cstring}, s::Ref{T}) where {T <: InlineString} = Cstring(Base.unsafe_convert(Ptr{Cchar}, s)) @@ -478,6 +482,9 @@ end return Base.or_int(s, _oftype(typeof(s), new_n)) end +throw_strip_argument_error() = + throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s")) + Base.lstrip(f, s::InlineString1) = lstrip(f, InlineString3(s)) function Base.lstrip(f, s::InlineString) nc = 0 @@ -493,6 +500,9 @@ function Base.lstrip(f, s::InlineString) return nc == 0 ? s : _chopprefix(s, nc, len) end +Base.lstrip(::AbstractString, ::InlineString) = throw_strip_argument_error() +Base.lstrip(::AbstractString, ::InlineString1) = throw_strip_argument_error() + if isdefined(Base, :chopsuffix) Base.chopsuffix(s::InlineString1, suffix::AbstractString) = chopsuffix(String3(s), suffix) @@ -534,6 +544,9 @@ function Base.rstrip(f, s::InlineString) return nc == 0 ? s : _chopsuffix(s, nc) end +Base.rstrip(::AbstractString, ::InlineString) = throw_strip_argument_error() +Base.rstrip(::AbstractString, ::InlineString1) = throw_strip_argument_error() + Base.chomp(s::InlineString1) = chomp(String3(s)) function Base.chomp(s::InlineString) i = lastindex(s) diff --git a/test/runtests.jl b/test/runtests.jl index 88faa13..5c11f97 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -210,12 +210,16 @@ S = InlineString7 @test rstrip(isnumeric, S("abc0123")) === S("abc") @test rstrip(S("ello"), ['e','o']) === S("ell") @test rstrip(InlineString1("x")) === InlineString3("x") +@test_throws ArgumentError rstrip("test", S(" a b c ")) +@test_throws ArgumentError rstrip("test", InlineString1("x")) @test lstrip(S(" a b c ")) isa S @test lstrip(S(" a b c ")) === S("a b c ") @test lstrip(isnumeric, S("0123abc")) === S("abc") @test lstrip(S("ello"), ['e','o']) === S("llo") @test lstrip(InlineString1("x")) === InlineString3("x") +@test_throws ArgumentError lstrip("test", S(" a b c ")) +@test_throws ArgumentError lstrip("test", InlineString1("x")) @test strip(InlineString1("x")) === InlineString3("x") S = InlineString3