Skip to content

Commit 29151a0

Browse files
lpad, rpad: use character cound instead of textwidth [fix #25016] (#25069)
The general principle here is that we should avoid having behavior in Base that depends on which version of Unicode you're using. It's fine for an external package to provide versions of lpad and rpad which use textwidth and thereby depend on Unicode details, but the functions in Base should not depend on these.
1 parent c55e58f commit 29151a0

File tree

3 files changed

+76
-30
lines changed

3 files changed

+76
-30
lines changed

base/deprecated.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3033,6 +3033,10 @@ end
30333033
# Associative -> AbstractDict (#25012)
30343034
@deprecate_binding Associative AbstractDict
30353035

3036+
# issue #25016
3037+
@deprecate lpad(s, n::Integer, p) lpad(string(s), n, string(p))
3038+
@deprecate rpad(s, n::Integer, p) rpad(string(s), n, string(p))
3039+
30363040
# END 0.7 deprecations
30373041

30383042
# BEGIN 1.0 deprecations

base/strings/util.jl

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -204,49 +204,59 @@ strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars)
204204

205205
## string padding functions ##
206206

207-
function lpad(s::AbstractString, n::Integer, p::AbstractString=" ")
208-
m = n - Unicode.textwidth(s)
209-
m 0 && return s
210-
l = Unicode.textwidth(p)
211-
q, r = divrem(m, l)
212-
string(p^q, first(p, r), s)
213-
end
214-
215-
function rpad(s::AbstractString, n::Integer, p::AbstractString=" ")
216-
m = n - Unicode.textwidth(s)
217-
m 0 && return s
218-
l = Unicode.textwidth(p)
219-
q, r = divrem(m, l)
220-
string(s, p^q, first(p, r))
221-
end
222-
223207
"""
224-
lpad(s, n::Integer, p::AbstractString=" ")
208+
lpad(s, n::Integer, p::Union{Char,AbstractString}=' ') -> String
225209
226-
Make a string at least `n` columns wide when printed by padding `s` on the left
227-
with copies of `p`.
210+
Stringify `s` and pad the resulting string on the left with `p` to make it `n`
211+
characters (code points) long. If `s` is already `n` characters long, an equal
212+
string is returned. Pad with spaces by default.
228213
229214
# Examples
230215
```jldoctest
231-
julia> lpad("March",10)
216+
julia> lpad("March", 10)
232217
" March"
233218
```
234219
"""
235-
lpad(s, n::Integer, p=" ") = lpad(string(s),n,string(p))
220+
lpad(s, n::Integer, p::Union{Char,AbstractString}=' ') = lpad(string(s), n, string(p))
221+
222+
function lpad(
223+
s::Union{Char,AbstractString},
224+
n::Integer,
225+
p::Union{Char,AbstractString}=' ',
226+
) :: String
227+
m = n - length(s)
228+
m 0 && return string(s)
229+
l = length(p)
230+
q, r = divrem(m, l)
231+
r == 0 ? string(p^q, s) : string(p^q, first(p, r), s)
232+
end
236233

237234
"""
238-
rpad(s, n::Integer, p::AbstractString=" ")
235+
rpad(s, n::Integer, p::Union{Char,AbstractString}=' ') -> String
239236
240-
Make a string at least `n` columns wide when printed by padding `s` on the right
241-
with copies of `p`.
237+
Stringify `s` and pad the resulting string on the right with `p` to make it `n`
238+
characters (code points) long. If `s` is already `n` characters long, an equal
239+
string is returned. Pad with spaces by default.
242240
243241
# Examples
244242
```jldoctest
245-
julia> rpad("March",20)
243+
julia> rpad("March", 20)
246244
"March "
247245
```
248246
"""
249-
rpad(s, n::Integer, p=" ") = rpad(string(s),n,string(p))
247+
rpad(s, n::Integer, p::Union{Char,AbstractString}=' ') = rpad(string(s), n, string(p))
248+
249+
function rpad(
250+
s::Union{Char,AbstractString},
251+
n::Integer,
252+
p::Union{Char,AbstractString}=' ',
253+
) :: String
254+
m = n - length(s)
255+
m 0 && return string(s)
256+
l = length(p)
257+
q, r = divrem(m, l)
258+
r == 0 ? string(s, p^q) : string(s, p^q, first(p, r))
259+
end
250260

251261
# splitter can be a Char, Vector{Char}, AbstractString, Regex, ...
252262
# any splitter that provides search(s::AbstractString, splitter)

test/strings/util.jl

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,46 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
@testset "padding (lpad and rpad)" begin
4+
@test lpad("foo", 2) == "foo"
5+
@test rpad("foo", 2) == "foo"
46
@test lpad("foo", 3) == "foo"
57
@test rpad("foo", 3) == "foo"
8+
@test lpad("foo", 4) == " foo"
9+
@test rpad("foo", 4) == "foo "
610
@test lpad("foo", 5) == " foo"
711
@test rpad("foo", 5) == "foo "
8-
@test lpad("foo", 5, " ") == " foo"
9-
@test rpad("foo", 5, " ") == "foo "
10-
@test lpad("foo", 6, " ") == " foo"
11-
@test rpad("foo", 6, " ") == "foo "
12+
@test lpad("foo", 2, "123") == "foo"
13+
@test rpad("foo", 2, "123") == "foo"
14+
@test lpad("foo", 3, "123") == "foo"
15+
@test rpad("foo", 3, "123") == "foo"
16+
@test lpad("foo", 4, "123") == "1foo"
17+
@test rpad("foo", 4, "123") == "foo1"
18+
@test lpad("foo", 5, "123") == "12foo"
19+
@test rpad("foo", 5, "123") == "foo12"
20+
@test lpad("foo", 6, "123") == "123foo"
21+
@test rpad("foo", 6, "123") == "foo123"
22+
@test lpad("foo", 7, "123") == "1231foo"
23+
@test rpad("foo", 7, "123") == "foo1231"
24+
@test lpad("foo", 8, "123") == "12312foo"
25+
@test rpad("foo", 8, "123") == "foo12312"
26+
@test lpad("foo", 9, "123") == "123123foo"
27+
@test rpad("foo", 9, "123") == "foo123123"
28+
@test lpad("αβ", 2, "¹₂³") == "αβ"
29+
@test rpad("αβ", 2, "¹₂³") == "αβ"
30+
@test lpad("αβ", 3, "¹₂³") == "¹αβ"
31+
@test rpad("αβ", 3, "¹₂³") == "αβ¹"
32+
@test lpad("αβ", 4, "¹₂³") == "¹₂αβ"
33+
@test rpad("αβ", 4, "¹₂³") == "αβ¹₂"
34+
@test lpad("αβ", 5, "¹₂³") == "¹₂³αβ"
35+
@test rpad("αβ", 5, "¹₂³") == "αβ¹₂³"
36+
@test lpad("αβ", 6, "¹₂³") == "¹₂³¹αβ"
37+
@test rpad("αβ", 6, "¹₂³") == "αβ¹₂³¹"
38+
@test lpad("αβ", 7, "¹₂³") == "¹₂³¹₂αβ"
39+
@test rpad("αβ", 7, "¹₂³") == "αβ¹₂³¹₂"
40+
@test lpad("αβ", 8, "¹₂³") == "¹₂³¹₂³αβ"
41+
@test rpad("αβ", 8, "¹₂³") == "αβ¹₂³¹₂³"
42+
@test lpad("αβ", 9, "¹₂³") == "¹₂³¹₂³¹αβ"
43+
@test rpad("αβ", 9, "¹₂³") == "αβ¹₂³¹₂³¹"
1244
end
1345

1446
# string manipulation

0 commit comments

Comments
 (0)