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

Fix #38256 (lpad/rpad defined in terms of textwidth) #39044

Merged
merged 5 commits into from
Jun 7, 2021
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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ 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: 10 additions & 6 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,14 +327,16 @@ 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 (code points) long. If `s` is already `n` characters long, an equal
characters (in [`textwidth`](@ref)) 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 @@ -344,9 +346,9 @@ function lpad(
p::Union{AbstractChar,AbstractString}=' ',
) :: String
n = Int(n)::Int
m = signed(n) - Int(length(s))::Int
m = signed(n) - Int(textwidth(s))::Int
m ≤ 0 && return string(s)
l = length(p)
l = textwidth(p)
q, r = divrem(m, l)
r == 0 ? string(p^q, s) : string(p^q, first(p, r), s)
end
Expand All @@ -355,14 +357,16 @@ 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 (code points) long. If `s` is already `n` characters long, an equal
characters (in [`textwidth`](@ref)) 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 @@ -372,9 +376,9 @@ function rpad(
p::Union{AbstractChar,AbstractString}=' ',
) :: String
n = Int(n)::Int
m = signed(n) - Int(length(s))::Int
m = signed(n) - Int(textwidth(s))::Int
m ≤ 0 && return string(s)
l = length(p)
l = textwidth(p)
q, r = divrem(m, l)
r == 0 ? string(s, p^q) : string(s, p^q, first(p, r))
end
Expand Down
5 changes: 5 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@
# 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