Skip to content

Commit 9b88049

Browse files
Pontus StenetorpKristofferC
Pontus Stenetorp
authored andcommitted
Fix for l/rpad with > 1 length padding string
Also, tests for JuliaLang#11885.
1 parent fd2ca2d commit 9b88049

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

base/string.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,7 +1252,8 @@ function lpad(s::AbstractString, n::Integer, p::AbstractString=" ")
12521252
end
12531253
q = div(m,l)
12541254
r = m - q*l
1255-
bytestring(p^q*p[1:chr2ind(p,r)]*s)
1255+
i = r != 0 ? chr2ind(p, r) : -1
1256+
bytestring(p^q*p[1:i]*s)
12561257
end
12571258

12581259
function rpad(s::AbstractString, n::Integer, p::AbstractString=" ")
@@ -1264,7 +1265,8 @@ function rpad(s::AbstractString, n::Integer, p::AbstractString=" ")
12641265
end
12651266
q = div(m,l)
12661267
r = m - q*l
1267-
bytestring(s*p^q*p[1:chr2ind(p,r)])
1268+
i = r != 0 ? chr2ind(p, r) : -1
1269+
bytestring(s*p^q*p[1:i])
12681270
end
12691271

12701272
lpad(s, n::Integer, p=" ") = lpad(string(s),n,string(p))

test/strings.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,15 @@ for T in (UInt8,UInt16,UInt32,UInt64)
253253
@test_throws OverflowError parse(T,string(big(typemax(T))+1))
254254
end
255255

256+
@test lpad("foo", 3) == "foo"
257+
@test rpad("foo", 3) == "foo"
258+
@test lpad("foo", 5) == " foo"
259+
@test rpad("foo", 5) == "foo "
260+
@test lpad("foo", 5, " ") == " foo"
261+
@test rpad("foo", 5, " ") == "foo "
262+
@test lpad("foo", 6, " ") == " foo"
263+
@test rpad("foo", 6, " ") == "foo "
264+
256265
# string manipulation
257266
@test strip("\t hi \n") == "hi"
258267

0 commit comments

Comments
 (0)