* Re: printf, left-justification ignored in 5.0.8
[not found] <20150607081142.GF15174@isis.sigpipe.cz>
@ 2015-06-07 20:39 ` Oliver Kiddle
2015-06-07 21:15 ` Stephane Chazelas
0 siblings, 1 reply; 4+ messages in thread
From: Oliver Kiddle @ 2015-06-07 20:39 UTC (permalink / raw)
To: Zsh workers
Roman Neuhauser wrote:
>
> 5.0.8:
>
> % printf "x:%-20s:y\n" fubar
> x: fubar:y
That'll be my fault for not checking thoroughly enough. The - flag is
fine for numeric values but for strings it seems there is separate code
to allow for UTF-8 which looks in the flags array. The patch should
fix this, adds a test and makes the maximum specification length test
include the ' flag.
Oliver
diff --git a/Src/builtin.c b/Src/builtin.c
index 9358e8b..8bfc419 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -4461,7 +4461,7 @@ bin_print(char *name, char **args, Options ops, int func)
lleft -= chars;
ptr += chars;
}
- if (width > 0 && flags[2]) width = -width;
+ if (width > 0 && flags[3]) width = -width;
if (width > 0 && lchars < width)
count += fprintf(fout, "%*c", width - lchars, ' ');
count += fwrite(b, 1, lbytes, fout);
diff --git a/Test/B03print.ztst b/Test/B03print.ztst
index 48574c2..9360416 100644
--- a/Test/B03print.ztst
+++ b/Test/B03print.ztst
@@ -169,11 +169,15 @@
0:%n count zeroed on format reuse
>1
-# this may fill spec string with '%0+- #*.*lld\0' - 13 characters
- printf '%1$0+- #-08.5dx\n' 123
+# this may fill spec string with '%0'+- #*.*lld\0' - 14 characters
+ printf '%1$0'"'+- #-08.5dx\n" 123
0:maximal length format specification
>+00123 x
+ printf "x:%-20s:y\n" fubar
+0:left-justification of string
+>x:fubar :y
+
printf '%*smorning\n' -5 good
0:negative width specified
>good morning
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: printf, left-justification ignored in 5.0.8
2015-06-07 20:39 ` printf, left-justification ignored in 5.0.8 Oliver Kiddle
@ 2015-06-07 21:15 ` Stephane Chazelas
2015-06-12 11:30 ` Vincent Lefevre
0 siblings, 1 reply; 4+ messages in thread
From: Stephane Chazelas @ 2015-06-07 21:15 UTC (permalink / raw)
To: zsh-workers
2015-06-07 22:39:24 +0200, Oliver Kiddle:
> Roman Neuhauser wrote:
> >
> > 5.0.8:
> >
> > % printf "x:%-20s:y\n" fubar
> > x: fubar:y
>
> That'll be my fault for not checking thoroughly enough. The - flag is
> fine for numeric values but for strings it seems there is separate code
> to allow for UTF-8 which looks in the flags array. The patch should
> fix this, adds a test and makes the maximum specification length test
> include the ' flag.
[...]
BTW, there was a discussion lately on the Austin group mailing
list confirming that the %20s should count bytes, not
characters.
That is in a UTF-8 locale
printf '|%3s|\n' e é €
should print:
| e|
| é|
|€|
not:
| e|
| é|
| €|
I find the zsh behaviour more useful though, especially
considering that in zsh we can get the POSIX behaviour with:
$ LC_ALL=C printf '|%3s|\n' e é €
| e|
| é|
|€|
But note that to get proper alignment, rather than number of
characters, the width of the glyphs should be considered
instead anyway:
$ printf '|%3b|\n' e '\ue9' 'e\u301' '\uff45'
| e|
| é|
| é|
| e|
ksh93 has %3Ls for that:
$ printf '|%3Ls|\n' e $'\ue9' $'e\u301' $'\uff45'
| e|
| é|
| é|
| e|
(possibly uses wcswidth()).
--
Stephane
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: printf, left-justification ignored in 5.0.8
2015-06-07 21:15 ` Stephane Chazelas
@ 2015-06-12 11:30 ` Vincent Lefevre
2015-06-12 12:31 ` Stephane Chazelas
0 siblings, 1 reply; 4+ messages in thread
From: Vincent Lefevre @ 2015-06-12 11:30 UTC (permalink / raw)
To: zsh-workers
On 2015-06-07 22:15:29 +0100, Stephane Chazelas wrote:
> BTW, there was a discussion lately on the Austin group mailing
> list confirming that the %20s should count bytes, not
> characters.
>
> That is in a UTF-8 locale
>
> printf '|%3s|\n' e é €
>
> should print:
>
> | e|
> | é|
> |€|
>
> not:
>
> | e|
> | é|
> | €|
>
> I find the zsh behaviour more useful though,
Well, it depends on the context. As I've said in
http://www.zsh.org/mla/workers/2012/msg00151.html
Yes, the number is the size in bytes, not in characters. I think
that the intent is to deal with internal structures (e.g. with
file formats where some fields have a fixed or limited size, and
the same syntax can be used in C to avoid buffer overflows).
I don't know if this is the real reason.
> especially considering that in zsh we can get the POSIX behaviour
> with:
>
> $ LC_ALL=C printf '|%3s|\n' e é €
> | e|
> | é|
> |€|
However the change of locale may affect other format specifiers,
like %f, and error messages.
> But note that to get proper alignment, rather than number of
> characters, the width of the glyphs should be considered
> instead anyway:
>
> $ printf '|%3b|\n' e '\ue9' 'e\u301' '\uff45'
> | e|
> | é|
> | é|
> | e|
>
> ksh93 has %3Ls for that:
>
> $ printf '|%3Ls|\n' e $'\ue9' $'e\u301' $'\uff45'
> | e|
> | é|
> | é|
> | e|
>
> (possibly uses wcswidth()).
This could be useful in zsh, and its support would be needed for
"emulate ksh".
--
Vincent Lefèvre <vincent@vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: printf, left-justification ignored in 5.0.8
2015-06-12 11:30 ` Vincent Lefevre
@ 2015-06-12 12:31 ` Stephane Chazelas
0 siblings, 0 replies; 4+ messages in thread
From: Stephane Chazelas @ 2015-06-12 12:31 UTC (permalink / raw)
To: zsh-workers
2015-06-12 13:30:50 +0200, Vincent Lefevre:
> On 2015-06-07 22:15:29 +0100, Stephane Chazelas wrote:
> > BTW, there was a discussion lately on the Austin group mailing
> > list confirming that the %20s should count bytes, not
> > characters.
> >
> > That is in a UTF-8 locale
> >
> > printf '|%3s|\n' e é €
> >
> > should print:
> >
> > | e|
> > | é|
> > |€|
> >
> > not:
> >
> > | e|
> > | é|
> > | €|
> >
> > I find the zsh behaviour more useful though,
>
> Well, it depends on the context. As I've said in
>
> http://www.zsh.org/mla/workers/2012/msg00151.html
>
> Yes, the number is the size in bytes, not in characters. I think
> that the intent is to deal with internal structures (e.g. with
> file formats where some fields have a fixed or limited size, and
> the same syntax can be used in C to avoid buffer overflows).
>
> I don't know if this is the real reason.
I beleive the real reason was to align on C printf (not wprintf
for instance) (makes little sense to me)
> > especially considering that in zsh we can get the POSIX behaviour
> > with:
> >
> > $ LC_ALL=C printf '|%3s|\n' e é €
> > | e|
> > | é|
> > |€|
>
> However the change of locale may affect other format specifiers,
> like %f, and error messages.
Well, that's a general problem with LC_* handling. Here however
it can be worked around by using several invocations of printf.
[...]
> > ksh93 has %3Ls for that:
> >
> > $ printf '|%3Ls|\n' e $'\ue9' $'e\u301' $'\uff45'
> > | e|
> > | é|
> > | é|
> > | e|
> >
> > (possibly uses wcswidth()).
>
> This could be useful in zsh, and its support would be needed for
> "emulate ksh".
[...]
While I agree that could be useful feature, note that zsh is not
(and will never be I hope) a superset of ksh93. The emulation
mode changes some behaviour that othewise differ from ksh. It
covers mostly ksh88, and zsh has a few features from ksh93 but
that's about it.
--
Stephane
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2015-06-12 12:52 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20150607081142.GF15174@isis.sigpipe.cz>
2015-06-07 20:39 ` printf, left-justification ignored in 5.0.8 Oliver Kiddle
2015-06-07 21:15 ` Stephane Chazelas
2015-06-12 11:30 ` Vincent Lefevre
2015-06-12 12:31 ` Stephane Chazelas
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).