Skip to content

Commit

Permalink
Accept a function to strip like rstrip and lstrip
Browse files Browse the repository at this point in the history
`strip` is documented to accept a function argument but it does not.
`rstrip` and `lstrip` do, so it seems this was simply an oversight.

See #31195.
  • Loading branch information
ararslan committed Mar 1, 2019
1 parent cab5ba5 commit e0e0d78
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Expand Up @@ -44,6 +44,7 @@ Standard library changes
* `startswith` and `endswith` now accept a `Regex` for the second argument ([#29790]).
* `retry` supports arbitrary callable objects ([#30382]).
* A no-argument construct to `Ptr{T}` has been added which constructs a null pointer ([#30919])
* `strip` now accepts a function argument in the same manner as `lstrip` and `rstrip` ([#31211])

#### LinearAlgebra

Expand Down
16 changes: 11 additions & 5 deletions base/strings/util.jl
Expand Up @@ -195,15 +195,20 @@ rstrip(s::AbstractString) = rstrip(isspace, s)
rstrip(s::AbstractString, chars::Chars) = rstrip(in(chars), s)

"""
strip(str::AbstractString, [chars])
strip([pred=isspace,] str::AbstractString)
strip(str::AbstractString, chars)
Remove leading and trailing characters from `str`.
Remove leading and trailing characters from `str`, either those specified by `chars` or
those for which the function `pred` returns `true`.
The default behaviour is to remove leading whitespace and delimiters: see
[`isspace`](@ref) for precise details.
The optional `chars` argument specifies which characters to remove: it can be a single character,
vector or set of characters, or a predicate function.
The optional `chars` argument specifies which characters to remove: it can be a single
character, vector or set of characters.
!!! compat "Julia 1.2"
The method which accepts a predicate function requires Julia 1.2 or later.
# Examples
```jldoctest
Expand All @@ -212,7 +217,8 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n'])
```
"""
strip(s::AbstractString) = lstrip(rstrip(s))
strip(s::AbstractString, chars) = lstrip(rstrip(s, chars), chars)
strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars)
strip(f, s::AbstractString) = lstrip(f, rstrip(f, s))

## string padding functions ##

Expand Down
1 change: 1 addition & 0 deletions test/strings/util.jl
Expand Up @@ -53,6 +53,7 @@ end
@test strip(" \u2009 hi \u2009 ") == "hi"
@test strip("foobarfoo", ['f','o']) == "bar"
@test strip("foobarfoo", ('f','o')) == "bar"
@test strip(ispunct, "¡Hola!") == "Hola"

for s in ("", " ", " abc", "abc ", " abc "),
f in (lstrip, rstrip, strip)
Expand Down

0 comments on commit e0e0d78

Please sign in to comment.