diff --git a/NEWS.md b/NEWS.md index 6bcc2fc706f35..0a591ca81dc15 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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]). diff --git a/base/strings/util.jl b/base/strings/util.jl index c4c4459545128..2ac087aab9075 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -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 """ @@ -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 """ diff --git a/test/strings/util.jl b/test/strings/util.jl index 92298b767b07b..d0d232b5ba977 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -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)