Skip to content

Commit

Permalink
Merge pull request #27605 from JuliaLang/aa/functions-come-first
Browse files Browse the repository at this point in the history
Normalize the argument order in lstrip and rstrip
  • Loading branch information
ararslan authored Jun 19, 2018
2 parents 0dcbabf + 16c5df9 commit 8fc7a16
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,9 @@ Library improvements
* `IOBuffer` can take the `sizehint` keyword argument to suggest a capacity of
the buffer ([#25944]).

* `lstrip` and `rstrip` now accept a predicate function that defaults to `isspace`
([#27309]).

* `trunc`, `floor`, `ceil`, and `round` specify `digits`, `sigdigits` and `base` using
keyword arguments. ([#26156], [#26670])

Expand Down
30 changes: 18 additions & 12 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ function chomp(s::String)
end

"""
lstrip(str::AbstractString[, chars])
lstrip([pred=isspace,] str::AbstractString)
lstrip(str::AbstractString, chars)
Remove leading characters from `str`.
Remove leading 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, or a vector or set of characters.
# Examples
```jldoctest
Expand All @@ -154,25 +156,28 @@ julia> lstrip(a)
"March"
```
"""
function lstrip(s::AbstractString, f=isspace)
function lstrip(f, s::AbstractString)
e = lastindex(s)
for (i, c) in pairs(s)
!f(c) && return SubString(s, i, e)
end
SubString(s, e+1, e)
end
lstrip(s::AbstractString, chars::Chars) = lstrip(s, in(chars))
lstrip(s::AbstractString) = lstrip(isspace, s)
lstrip(s::AbstractString, chars::Chars) = lstrip(in(chars), s)

"""
rstrip(str::AbstractString[, chars])
rstrip([pred=isspace,] str::AbstractString)
rstrip(str::AbstractString, chars)
Remove trailing characters from `str`.
Remove 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, or a vector or set of characters.
# Examples
```jldoctest
Expand All @@ -183,13 +188,14 @@ julia> rstrip(a)
"March"
```
"""
function rstrip(s::AbstractString, f=isspace)
function rstrip(f, s::AbstractString)
for (i, c) in Iterators.reverse(pairs(s))
f(c) || return SubString(s, 1, i)
end
SubString(s, 1, 0)
end
rstrip(s::AbstractString, chars::Chars) = rstrip(s, in(chars))
rstrip(s::AbstractString) = rstrip(isspace, s)
rstrip(s::AbstractString, chars::Chars) = rstrip(in(chars), s)

"""
strip(str::AbstractString, [chars])
Expand Down
2 changes: 1 addition & 1 deletion stdlib/DelimitedFiles/src/DelimitedFiles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module DelimitedFiles

using Mmap

import Base: _default_delims, tryparse_internal, show
import Base: tryparse_internal, show

export readdlm, writedlm

Expand Down
5 changes: 5 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ end
@test typeof(fb) == SubString{T}
end
end

@test lstrip(isnumeric, "0123abc") == "abc"
@test rstrip(isnumeric, "abc0123") == "abc"
@test lstrip("ello", ['e','o']) == "llo"
@test rstrip("ello", ['e','o']) == "ell"
end

@testset "rsplit/split" begin
Expand Down

0 comments on commit 8fc7a16

Please sign in to comment.