-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
lpad, rpad use textwidth/char count incoherently #25016
Comments
cc @jiahao, @stevengj, @nalimilan |
I'd just |
In that case why not just implement the character version of this since that's what the assumption that Another option is to make function fractional_padding(::typeof(textwidth), p::AbstractString, r::Integer)
w = 0
for (i, c) in enumerate(p)
w += textwidth(c)
w ≤ r || return first(p, i-1)
end
return p
end
fractional_padding(::typeof(length), p::AbstractString, r::Integer) = first(p, r)
function lpad(length::Function, s::AbstractString, n::Integer, p::AbstractString=" ")
m = n - length(s)
m ≤ 0 && return s
l = length(p)
q, r = divrem(m, l)
string(p^q, fractional_padding(length, p, r), s)
end
lpad(s::AbstractString, n::Integer, p::AbstractString=" ") = lpad(textwidth, s, n, p) |
That's not the same thing: the string can still contain several characters. I feel like in general you want to pad with ASCII characters, so while we should keep the possibility of extending the function later, I don't think it's worth wasting your precious time right now on it. |
Yes, but asserting that is simply saying that we don't handle padding unless the characters in the padding string are all single-column width. At that point, why not just implement the behavior that treats all characters as having unit width – i.e. padding by character count rather that textwidth. Then the textwidth version can be implemented externally. |
Didn’t we discuss this in a previous issue? |
Yes, but this was spurred by the fact that I just noticed that our implementation is still incorrect and arguably it's not possible to implement fully correct exact padding based on |
#25021 made me realize something important: we want the behavior of base to be independent of what particular version of the Unicode standard one uses. Moving the Unicode package out of Base is a good step in that direction, but now any use of the internal Base.Unicode module is suspect – in particular, this one since it currently depends on |
Should textwidth throw an error if you try to pad an odd amount with a double width character? |
No, we shouldn't be using |
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.
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.
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.
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.
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.
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.
Here's a slightly cleaned up version of
lpad
:The quotient and remainder are computed in terms of
textwidth
, while the remainder is then used to select a number of characters, not a width of text. We should either revert the change I made in 8be4acc, or compute the number of characters for fractional padding by taking as many leading characters as fit into the allowed text width.The text was updated successfully, but these errors were encountered: