Skip to content

Commit

Permalink
Revert "Define lpad/rpad in terms of textwidth (#39044)"
Browse files Browse the repository at this point in the history
This reverts commit 708729b.
  • Loading branch information
DilumAluthge authored Jun 8, 2021
1 parent 1910a76 commit c50b8e2
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 16 deletions.
1 change: 0 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ Standard library changes
* `keys(::RegexMatch)` is now defined to return the capture's keys, by name if named, or by index if not ([#37299]).
* `keys(::Generator)` is now defined to return the iterator's keys ([#34678])
* `RegexMatch` now iterate to give their captures. ([#34355]).
* `lpad/rpad` are now defined in terms of `textwidth` ([#39044])
* `Test.@test` now accepts `broken` and `skip` boolean keyword arguments, which
mimic `Test.@test_broken` and `Test.@test_skip` behavior, but allows skipping
tests failing only under certain conditions. For example
Expand Down
16 changes: 6 additions & 10 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,16 +327,14 @@ strip(f, s::AbstractString) = lstrip(f, rstrip(f, s))
lpad(s, n::Integer, p::Union{AbstractChar,AbstractString}=' ') -> String
Stringify `s` and pad the resulting string on the left with `p` to make it `n`
characters (in [`textwidth`](@ref)) long. If `s` is already `n` characters long, an equal
characters (code points) long. If `s` is already `n` characters long, an equal
string is returned. Pad with spaces by default.
# Examples
```jldoctest
julia> lpad("March", 10)
" March"
```
!!! compat "Julia 1.7"
In Julia 1.7, this function was changed to use `textwidth` rather than a raw character (codepoint) count.
"""
lpad(s, n::Integer, p::Union{AbstractChar,AbstractString}=' ') = lpad(string(s)::AbstractString, n, string(p))

Expand All @@ -346,9 +344,9 @@ function lpad(
p::Union{AbstractChar,AbstractString}=' ',
) :: String
n = Int(n)::Int
m = signed(n) - Int(textwidth(s))::Int
m = signed(n) - Int(length(s))::Int
m 0 && return string(s)
l = textwidth(p)
l = length(p)
q, r = divrem(m, l)
r == 0 ? string(p^q, s) : string(p^q, first(p, r), s)
end
Expand All @@ -357,16 +355,14 @@ end
rpad(s, n::Integer, p::Union{AbstractChar,AbstractString}=' ') -> String
Stringify `s` and pad the resulting string on the right with `p` to make it `n`
characters (in [`textwidth`](@ref)) long. If `s` is already `n` characters long, an equal
characters (code points) long. If `s` is already `n` characters long, an equal
string is returned. Pad with spaces by default.
# Examples
```jldoctest
julia> rpad("March", 20)
"March "
```
!!! compat "Julia 1.7"
In Julia 1.7, this function was changed to use `textwidth` rather than a raw character (codepoint) count.
"""
rpad(s, n::Integer, p::Union{AbstractChar,AbstractString}=' ') = rpad(string(s)::AbstractString, n, string(p))

Expand All @@ -376,9 +372,9 @@ function rpad(
p::Union{AbstractChar,AbstractString}=' ',
) :: String
n = Int(n)::Int
m = signed(n) - Int(textwidth(s))::Int
m = signed(n) - Int(length(s))::Int
m 0 && return string(s)
l = textwidth(p)
l = length(p)
q, r = divrem(m, l)
r == 0 ? string(s, p^q) : string(s, p^q, first(p, r))
end
Expand Down
5 changes: 0 additions & 5 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@
# Issue #32160 (unsigned underflow in lpad/rpad)
@test lpad("xx", UInt(1), " ") == "xx"
@test rpad("xx", UInt(1), " ") == "xx"
# Issue #38256 (lpad/rpad defined in terms of textwidth)
@test lpad("⟨k|H₁|k̃⟩", 12) |> textwidth == 12
@test rpad("⟨k|H₁|k̃⟩", 12) |> textwidth == 12
@test lpad("⟨k|H₁|k⟩", 12) |> textwidth == 12
@test rpad("⟨k|H₁|k⟩", 12) |> textwidth == 12
end

# string manipulation
Expand Down

0 comments on commit c50b8e2

Please sign in to comment.