Skip to content

Commit

Permalink
add deprecation for [lr]strip(str,chars)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbyrne committed May 24, 2018
1 parent 8b70da1 commit b4c6102
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 41 deletions.
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,10 @@ end

@eval @deprecate $(Symbol("@schedule")) $(Symbol("@async"))

@deprecate lstrip(str::AbstractString, chars::Chars) lstrip(in(chars), str)
@deprecate rstrip(str::AbstractString, chars::Chars) rstrip(in(chars), str)
@deprecate strip(str::AbstractString, chars::Chars) strip(in(chars), str)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
59 changes: 20 additions & 39 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,20 +116,12 @@ end
const _default_delims = [' ','\t','\n','\v','\f','\r']

"""
lstrip(s::AbstractString)
lstrip(f, s::AbstractString)
lstrip(s::AbstractString, chars::Chars)
lstrip(f, str::AbstractString)
lstrip(str::AbstractString)
Remove leading characters from `s`.
Remove any leading characters from `str` for which the predicate `f` is true.
The default behaviour is to remove leading whitespace and delimiters: any characters for
which [`isspace`](@ref) is `true`.
If a predicate `f` is provided, then remove any leading characters `c` such that `f(c)` is
`true`.
If `chars` (a character, or vector or set of characters) is provided, instead remove
leading characters contained in `chars`.
The default predicate is [`isspace`](@ref), that is remove leading whitespace and delimiters.
# Examples
```jldoctest
Expand All @@ -139,6 +131,10 @@ julia> a = lpad("March", 20)
julia> lstrip(a)
"March"
```
# See also
- [`rstrip`](@ref)
- [`strip`](@ref)
"""
function lstrip(f, s::AbstractString)
e = lastindex(s)
Expand All @@ -148,24 +144,15 @@ function lstrip(f, s::AbstractString)
SubString(s, e+1, e)
end
lstrip(s::AbstractString) = lstrip(isspace, s)
lstrip(s::AbstractString, chars::Chars) = lstrip(in(chars), s)


"""
rstrip(s::AbstractString)
rstrip(f, s::AbstractString)
rstrip(s::AbstractString, chars::Chars)
Remove trailing characters from `s`.
rstrip(f, str::AbstractString)
rstrip(str::AbstractString)
The default behaviour is to remove trailing whitespace and delimiters: any characters for
which [`isspace`](@ref) is `true`.
Remove any trailing characters from `str` for which the predicate `f` is true.
If a predicate `f` is provided, then remove any trailing characters `c` such that `f(c)`
is `true`.
If `chars` (a character, or vector or set of characters) is provided, instead remove
trailing characters contained in `chars`.
The default predicate is [`isspace`](@ref), that is remove trailing whitespace and delimiters.
# Examples
```jldoctest
Expand All @@ -175,6 +162,10 @@ julia> a = rpad("March", 20)
julia> rstrip(a)
"March"
```
# See also
- [`lstrip`](@ref)
- [`strip`](@ref)
"""
function rstrip(f, s::AbstractString)
for (i, c) in Iterators.reverse(pairs(s))
Expand All @@ -183,23 +174,14 @@ function rstrip(f, s::AbstractString)
SubString(s, 1, 0)
end
rstrip(s::AbstractString) = rstrip(isspace, s)
rstrip(s::AbstractString, chars::Chars) = rstrip(in(chars), s)

"""
strip(s::AbstractString)
strip(f, s::AbstractString)
strip(s::AbstractString, chars::Chars)
Remove leading and trailing characters from `s`.
The default behaviour is to remove leading or trailing whitespace and delimiters: any
characters for which [`isspace`](@ref) is `true`.
strip(f, str::AbstractString)
strip(str::AbstractString)
If a predicate `f` is provided, then remove any leading or trailing characters `c` such
that `f(c)` is `true`.
Remove any leading or trailing characters from `str` for which the predicate `f` is true.
If `chars` (a character, or vector or set of characters) is provided, instead remove
leading or trailing characters contained in `chars`.
The default predicate is [`isspace`](@ref), that is remove trailing whitespace and delimiters.
# Examples
```jldoctest
Expand All @@ -213,7 +195,6 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n'])
"""
strip(f, s::AbstractString) = lstrip(f, rstrip(f, s))
strip(s::AbstractString) = strip(isspace, s)
strip(s::AbstractString, chars::Chars) = strip(in(chars), s)

## string padding functions ##

Expand Down
4 changes: 2 additions & 2 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ end
@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"
@test strip(in(['f','o']), "foobarfoo") == "bar"
@test strip(in(('f','o')), "foobarfoo") == "bar"

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

0 comments on commit b4c6102

Please sign in to comment.