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 for l/rpad with > 1 length padding string #11924

Merged
merged 1 commit into from Jun 29, 2015
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
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