Skip to content

Commit 4095600

Browse files
IanButterworthKristofferC
authored and
KristofferC
committed
Throw clearer ArgumentError for strip with two string args (#51491)
(cherry picked from commit 66fe51f)
1 parent 2376f69 commit 4095600

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

base/strings/util.jl

+4
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ function lstrip(f, s::AbstractString)
350350
end
351351
lstrip(s::AbstractString) = lstrip(isspace, s)
352352
lstrip(s::AbstractString, chars::Chars) = lstrip(in(chars), s)
353+
lstrip(::AbstractString, ::AbstractString) = throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s"))
353354

354355
"""
355356
rstrip([pred=isspace,] str::AbstractString) -> SubString
@@ -383,6 +384,8 @@ function rstrip(f, s::AbstractString)
383384
end
384385
rstrip(s::AbstractString) = rstrip(isspace, s)
385386
rstrip(s::AbstractString, chars::Chars) = rstrip(in(chars), s)
387+
rstrip(::AbstractString, ::AbstractString) = throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s"))
388+
386389

387390
"""
388391
strip([pred=isspace,] str::AbstractString) -> SubString
@@ -410,6 +413,7 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n'])
410413
"""
411414
strip(s::AbstractString) = lstrip(rstrip(s))
412415
strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars)
416+
strip(::AbstractString, ::AbstractString) = throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s"))
413417
strip(f, s::AbstractString) = lstrip(f, rstrip(f, s))
414418

415419
## string padding functions ##

test/strings/util.jl

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ end
8989
@test rstrip(isnumeric, "abc0123") == "abc"
9090
@test lstrip("ello", ['e','o']) == "llo"
9191
@test rstrip("ello", ['e','o']) == "ell"
92+
93+
@test_throws ArgumentError strip("", "")
94+
@test_throws ArgumentError lstrip("", "")
95+
@test_throws ArgumentError rstrip("", "")
9296
end
9397

9498
@testset "partition" begin

0 commit comments

Comments
 (0)