Skip to content

Commit

Permalink
allow predicate functions to lstrip etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed May 29, 2018
1 parent 1d3de4c commit 4324c80
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
52 changes: 31 additions & 21 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,15 @@ end
const _default_delims = [' ','\t','\n','\v','\f','\r']

"""
lstrip(s::AbstractString[, chars::Chars])
lstrip(str::AbstractString[, chars])
Return `s` with any leading whitespace and delimiters removed.
The default delimiters to remove are `' '`, `\\t`, `\\n`, `\\v`,
`\\f`, and `\\r`.
If `chars` (a character, or vector or set of characters) is provided,
instead remove characters contained in it.
Remove leading characters from `str`.
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.
# Examples
```jldoctest
Expand All @@ -154,22 +156,25 @@ julia> lstrip(a)
"March"
```
"""
function lstrip(s::AbstractString, chars::Chars=_default_delims)
function lstrip(s::AbstractString, f=isspace)
e = lastindex(s)
for (i, c) in pairs(s)
!(c in chars) && return SubString(s, i, e)
!f(c) && return SubString(s, i, e)
end
SubString(s, e+1, e)
end
lstrip(s::AbstractString, chars::Chars) = lstrip(s, in(chars))

"""
rstrip(s::AbstractString[, chars::Chars])
rstrip(str::AbstractString[, chars])
Remove trailing characters from `str`.
Return `s` with any trailing whitespace and delimiters removed.
The default delimiters to remove are `' '`, `\\t`, `\\n`, `\\v`,
`\\f`, and `\\r`.
If `chars` (a character, or vector or set of characters) is provided,
instead remove characters contained in it.
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.
# Examples
```jldoctest
Expand All @@ -180,19 +185,24 @@ julia> rstrip(a)
"March"
```
"""
function rstrip(s::AbstractString, chars::Chars=_default_delims)
function rstrip(s::AbstractString, f=isspace)
for (i, c) in Iterators.reverse(pairs(s))
c in chars || return SubString(s, 1, i)
f(c) in chars || return SubString(s, 1, i)
end
SubString(s, 1, 0)
end
rstrip(s, chars::Chars) = rstrip(s, in(chars))

"""
strip(s::AbstractString, [chars::Chars])
strip(str::AbstractString, [chars])
Remove leading and trailing characters from `str`.
The default behaviour is to remove leading whitespace and delimiters: see
[`isspace`](@ref) for precise details.
Return `s` with any leading and trailing whitespace removed.
If `chars` (a character, or vector or set of characters) is provided,
instead remove characters contained in it.
The optional `chars` argument specifies which characters to remove: it can be a single character,
vector or set of characters, or a predicate function.
# Examples
```jldoctest
Expand All @@ -201,7 +211,7 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n'])
```
"""
strip(s::AbstractString) = lstrip(rstrip(s))
strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars)
strip(s::AbstractString, chars) = lstrip(rstrip(s, chars), chars)

## string padding functions ##

Expand Down
1 change: 1 addition & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ end
@test strip(" ") == ""
@test strip(" ") == ""
@test strip("\t hi \n") == "hi"
@test strip(" \u2009 hi \u2009 ") == "hi"
@test strip("foobarfoo", ['f','o']) == "bar"
@test strip("foobarfoo", ('f','o')) == "bar"

Expand Down

0 comments on commit 4324c80

Please sign in to comment.