Skip to content

Commit 38607a6

Browse files
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
1 parent bca08b0 commit 38607a6

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/InlineStrings.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ Base.unsafe_convert(::Type{Ptr{UInt8}}, x::Ref{T}) where {T <: InlineString} =
168168
Ptr{UInt8}(pointer_from_objref(x))
169169
Base.unsafe_convert(::Type{Ptr{Int8}}, x::Ref{T}) where {T <: InlineString} =
170170
Ptr{Int8}(pointer_from_objref(x))
171+
# Resolve method ambiguities
172+
Base.unsafe_convert(P::Type{Ptr{UInt8}}, x::Ptr{<:InlineString}) = convert(P, x)
173+
Base.unsafe_convert(P::Type{Ptr{Int8}}, x::Ptr{<:InlineString}) = convert(P, x)
174+
171175
Base.unsafe_convert(::Type{Cstring}, s::Ref{T}) where {T <: InlineString} =
172176
Cstring(Base.unsafe_convert(Ptr{Cchar}, s))
173177

@@ -478,6 +482,9 @@ end
478482
return Base.or_int(s, _oftype(typeof(s), new_n))
479483
end
480484

485+
throw_strip_argument_error() =
486+
throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s"))
487+
481488
Base.lstrip(f, s::InlineString1) = lstrip(f, InlineString3(s))
482489
function Base.lstrip(f, s::InlineString)
483490
nc = 0
@@ -493,6 +500,9 @@ function Base.lstrip(f, s::InlineString)
493500
return nc == 0 ? s : _chopprefix(s, nc, len)
494501
end
495502

503+
Base.lstrip(::AbstractString, ::InlineString) = throw_strip_argument_error()
504+
Base.lstrip(::AbstractString, ::InlineString1) = throw_strip_argument_error()
505+
496506
if isdefined(Base, :chopsuffix)
497507

498508
Base.chopsuffix(s::InlineString1, suffix::AbstractString) = chopsuffix(String3(s), suffix)
@@ -534,6 +544,9 @@ function Base.rstrip(f, s::InlineString)
534544
return nc == 0 ? s : _chopsuffix(s, nc)
535545
end
536546

547+
Base.rstrip(::AbstractString, ::InlineString) = throw_strip_argument_error()
548+
Base.rstrip(::AbstractString, ::InlineString1) = throw_strip_argument_error()
549+
537550
Base.chomp(s::InlineString1) = chomp(String3(s))
538551
function Base.chomp(s::InlineString)
539552
i = lastindex(s)

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,16 @@ S = InlineString7
210210
@test rstrip(isnumeric, S("abc0123")) === S("abc")
211211
@test rstrip(S("ello"), ['e','o']) === S("ell")
212212
@test rstrip(InlineString1("x")) === InlineString3("x")
213+
@test_throws ArgumentError rstrip("test", S(" a b c "))
214+
@test_throws ArgumentError rstrip("test", InlineString1("x"))
213215

214216
@test lstrip(S(" a b c ")) isa S
215217
@test lstrip(S(" a b c ")) === S("a b c ")
216218
@test lstrip(isnumeric, S("0123abc")) === S("abc")
217219
@test lstrip(S("ello"), ['e','o']) === S("llo")
218220
@test lstrip(InlineString1("x")) === InlineString3("x")
221+
@test_throws ArgumentError lstrip("test", S(" a b c "))
222+
@test_throws ArgumentError lstrip("test", InlineString1("x"))
219223

220224
@test strip(InlineString1("x")) === InlineString3("x")
221225
S = InlineString3

0 commit comments

Comments
 (0)