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

Improve implementation of rstrip and lstrip #22496

Merged
merged 8 commits into from
Jun 26, 2017
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ This section lists changes that do not have deprecation warnings.
Library improvements
--------------------

* The functions `strip`, `lstrip` and `rstrip` now return `SubString` ([#22496]).

* The functions `base` and `digits` digits now accept a negative
base (like `ndigits` did) ([#21692]).

Expand Down
9 changes: 5 additions & 4 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,16 @@ julia> lstrip(a)
```
"""
function lstrip(s::AbstractString, chars::Chars=_default_delims)
e = endof(s)
i = start(s)
while !done(s,i)
c, j = next(s,i)
if !(c in chars)
return s[i:end]
return SubString(s, i, e)
end
i = j
end
s[end+1:end]
SubString(s, e+1, e)
end

"""
Expand All @@ -171,11 +172,11 @@ function rstrip(s::AbstractString, chars::Chars=_default_delims)
while !done(r,i)
c, j = next(r,i)
if !(c in chars)
return s[1:end-i+1]
return SubString(s, 1, endof(s)-i+1)
end
i = j
end
s[1:0]
SubString(s, 1, 0)
end

"""
Expand Down
2 changes: 1 addition & 1 deletion test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ for s in ("", " ", " abc", "abc ", " abc "), f in (lstrip, rstrip, strip)
ft = f(t)
@test s == t
@test fs == ft
@test typeof(ft) == typeof(t[1:end])
@test typeof(ft) == SubString{T}

b = convert(SubString{T}, t)
fb = f(b)
Expand Down