Skip to content

Commit

Permalink
Merge pull request #11924 from ninjin/nin/pad
Browse files Browse the repository at this point in the history
Fix for l/rpad with > 1 length padding string
  • Loading branch information
tkelman committed Jun 29, 2015
2 parents 1544e4e + 05fb251 commit 89fbb19
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 4 additions & 2 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,8 @@ function lpad(s::AbstractString, n::Integer, p::AbstractString=" ")
end
q = div(m,l)
r = m - q*l
bytestring(p^q*p[1:chr2ind(p,r)]*s)
i = r != 0 ? chr2ind(p, r) : -1
bytestring(p^q*p[1:i]*s)
end

function rpad(s::AbstractString, n::Integer, p::AbstractString=" ")
Expand All @@ -1264,7 +1265,8 @@ function rpad(s::AbstractString, n::Integer, p::AbstractString=" ")
end
q = div(m,l)
r = m - q*l
bytestring(s*p^q*p[1:chr2ind(p,r)])
i = r != 0 ? chr2ind(p, r) : -1
bytestring(s*p^q*p[1:i])
end

lpad(s, n::Integer, p=" ") = lpad(string(s),n,string(p))
Expand Down
9 changes: 9 additions & 0 deletions test/strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ for T in (UInt8,UInt16,UInt32,UInt64)
@test_throws OverflowError parse(T,string(big(typemax(T))+1))
end

@test lpad("foo", 3) == "foo"
@test rpad("foo", 3) == "foo"
@test lpad("foo", 5) == " foo"
@test rpad("foo", 5) == "foo "
@test lpad("foo", 5, " ") == " foo"
@test rpad("foo", 5, " ") == "foo "
@test lpad("foo", 6, " ") == " foo"
@test rpad("foo", 6, " ") == "foo "

# string manipulation
@test strip("\t hi \n") == "hi"

Expand Down

0 comments on commit 89fbb19

Please sign in to comment.