-
-
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
@printf should use textwidth for "%ns" and "%0.ns" #41068
Labels
display and printing
Aesthetics and correctness of printed representations of objects.
unicode
Related to unicode characters and encodings
Comments
stevengj
added
unicode
Related to unicode characters and encodings
display and printing
Aesthetics and correctness of printed representations of objects.
labels
Jun 3, 2021
Related: #38256 |
Python 3 also gets this wrong: >>> print('%0.2s' % 'föó')
fo
>>> f'{"föó": <{5}}'
'föó'
>>> f'{"foo": <{5}}'
'foo ' |
I think even with
|
@arunsanganal, it works if you use julia> @printf("%5s", "föo")
föo
julia> @printf("%5s", "foo")
foo
julia> Printf.@printf("%.2s", "föó")
fö with diff --git a/stdlib/Printf/src/Printf.jl b/stdlib/Printf/src/Printf.jl
index 5f42df50b8..282d0ebdfd 100644
--- a/stdlib/Printf/src/Printf.jl
+++ b/stdlib/Printf/src/Printf.jl
@@ -239,7 +239,7 @@ end
@inline function fmt(buf, pos, arg, spec::Spec{T}) where {T <: Strings}
leftalign, hash, width, prec = spec.leftalign, spec.hash, spec.width, spec.precision
str = string(arg)
- slen = length(str) + (hash ? arg isa AbstractString ? 2 : 1 : 0)
+ slen = textwidth(str) + (hash ? arg isa AbstractString ? 2 : 1 : 0)
op = p = prec == -1 ? slen : min(slen, prec)
if !leftalign && width > p
for _ = 1:(width - p)
@@ -259,9 +259,9 @@ end
end
end
for c in str
- p == 0 && break
+ p -= textwidth(c)
+ p < 0 && break
pos = writechar(buf, pos, c)
- p -= 1
end
if hash && arg isa AbstractString && p > 0
buf[pos] = UInt8('"')
@@ -753,8 +753,9 @@ plength(f::Spec{Pointer}, x) = max(f.width, 2 * sizeof(x) + 2)
function plength(f::Spec{T}, x) where {T <: Strings}
str = string(x)
- p = f.precision == -1 ? (length(str) + (f.hash ? (x isa Symbol ? 1 : 2) : 0)) : f.precision
- return max(f.width, p) + (sizeof(str) - length(str))
+ sw = textwidth(str)
+ p = f.precision == -1 ? (sw + (f.hash ? (x isa Symbol ? 1 : 2) : 0)) : f.precision
+ return max(f.width, p) + (sizeof(str) - sw)
end
function plength(f::Spec{T}, x) where {T <: Ints} |
Ah! i had missed the edit on line 259. It does work fine now. 😄 |
I'll have a PR shortly. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
display and printing
Aesthetics and correctness of printed representations of objects.
unicode
Related to unicode characters and encodings
This seems wrong:
It seems like
@printf
(in this code) should really usetextwidth
to compute string widths for formatting purposes, similar to this discussion on discourse.(To improve backward compatibility, I suppose we could use
textwidth
only for%S
, which is currently a rarely used synonym for%s
, or use another format code entirely.)The text was updated successfully, but these errors were encountered: