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

Throw clearer ArgumentError for strip with two string args #51491

Merged
merged 1 commit into from
Sep 28, 2023
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
4 changes: 4 additions & 0 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,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
Expand Down Expand Up @@ -402,6 +403,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
Expand Down Expand Up @@ -429,6 +432,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 ##
Expand Down
4 changes: 4 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down