From 4095600686178e49d8bae3c812f6a3696092017d Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Thu, 28 Sep 2023 15:57:19 -0400 Subject: [PATCH] Throw clearer ArgumentError for strip with two string args (#51491) (cherry picked from commit 66fe51f0ad9531fa782c4dece0669076456f396c) --- base/strings/util.jl | 4 ++++ test/strings/util.jl | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/base/strings/util.jl b/base/strings/util.jl index dabb84ae65639..273b3dc37a913 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -350,6 +350,7 @@ function lstrip(f, s::AbstractString) end lstrip(s::AbstractString) = lstrip(isspace, s) lstrip(s::AbstractString, chars::Chars) = lstrip(in(chars), s) +lstrip(::AbstractString, ::AbstractString) = throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s")) """ rstrip([pred=isspace,] str::AbstractString) -> SubString @@ -383,6 +384,8 @@ function rstrip(f, s::AbstractString) end rstrip(s::AbstractString) = rstrip(isspace, s) rstrip(s::AbstractString, chars::Chars) = rstrip(in(chars), s) +rstrip(::AbstractString, ::AbstractString) = throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s")) + """ strip([pred=isspace,] str::AbstractString) -> SubString @@ -410,6 +413,7 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n']) """ strip(s::AbstractString) = lstrip(rstrip(s)) strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars) +strip(::AbstractString, ::AbstractString) = throw(ArgumentError("Both arguments are strings. The second argument should be a `Char` or collection of `Char`s")) strip(f, s::AbstractString) = lstrip(f, rstrip(f, s)) ## string padding functions ## diff --git a/test/strings/util.jl b/test/strings/util.jl index 5218310c5c1c7..f09236775acc8 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -89,6 +89,10 @@ end @test rstrip(isnumeric, "abc0123") == "abc" @test lstrip("ello", ['e','o']) == "llo" @test rstrip("ello", ['e','o']) == "ell" + + @test_throws ArgumentError strip("", "") + @test_throws ArgumentError lstrip("", "") + @test_throws ArgumentError rstrip("", "") end @testset "partition" begin