Skip to content

Commit

Permalink
Define lpad/rpad in terms of textwidth (JuliaLang#39044)
Browse files Browse the repository at this point in the history
Fix JuliaLang#38256, per triage consensus there
  • Loading branch information
mimame committed Jun 7, 2021
1 parent 0a34309 commit 708729b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,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

0 comments on commit 708729b

Please sign in to comment.