* (Fwd) printf for converting numbers to letters, bug?
@ 2004-03-26 16:42 Bart Schaefer
2004-03-26 17:00 ` Clint Adams
0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2004-03-26 16:42 UTC (permalink / raw)
To: zsh-workers
Looks like "%c" is interpreting the argument as a string and printing
the first character thereof. It should work more like %d, shouldn't it?
--- Forwarded mail from Matthias Kopfermann <matthias@kopfermann.org>
Date: Fri, 26 Mar 2004 16:29:13 +0100
From: Matthias Kopfermann <matthias@kopfermann.org>
To: schaefer@brasslantern.com
Subject: printf for converting numbers to letters, bug?
hi bart (hero:) )!
I wanted to do printf "%c\n" {65..101}
to get some ascii-letters and did not succeed.
Is this wanted behaviour?
Matthias
---End of forwarded mail from Matthias Kopfermann <matthias@kopfermann.org>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: (Fwd) printf for converting numbers to letters, bug?
2004-03-26 16:42 (Fwd) printf for converting numbers to letters, bug? Bart Schaefer
@ 2004-03-26 17:00 ` Clint Adams
2004-03-27 23:53 ` Bart Schaefer
0 siblings, 1 reply; 5+ messages in thread
From: Clint Adams @ 2004-03-26 17:00 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-workers
> Looks like "%c" is interpreting the argument as a string and printing
> the first character thereof. It should work more like %d, shouldn't it?
Not according to POSIX; the argument to %b, %c, or %s is to be
interpreted as a string.
If we had octal brace expansion, he could do something like
printf "%b\n" \\0{101..145}
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf for converting numbers to letters, bug?
2004-03-26 17:00 ` Clint Adams
@ 2004-03-27 23:53 ` Bart Schaefer
2004-03-28 4:49 ` Clint Adams
0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2004-03-27 23:53 UTC (permalink / raw)
To: zsh-workers
On Mar 26, 12:00pm, Clint Adams wrote:
} Subject: Re: (Fwd) printf for converting numbers to letters, bug?
}
} > Looks like "%c" is interpreting the argument as a string and printing
} > the first character thereof. It should work more like %d, shouldn't it?
}
} Not according to POSIX; the argument to %b, %c, or %s is to be
} interpreted as a string.
Just to clarify my earlier comment:
Zsh is (as of 4.2.0) behaving such that, given x='101',
printf "%c" $x
and
print -r $x[1]
produce the same output. That behavior is nearly useless. (It is how
/usr/bin/printf on Linux works, though, so I guess it can be interpreted
as a compatibility issue.)
} If we had octal brace expansion, he could do something like
} printf "%b\n" \\0{101..145}
If %b interprets the string \0101 as an octal number and prints the
corresponding character, why shouldn't %c interpret the string 101 as a
decimal number and print the corresponding character?
I've just been looking at
http://www.opengroup.org/onlinepubs/007904975/utilities/printf.html
I notice that although it mentions %b, there's NO definition of %b on any
of the pages it cross-references:
http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap05.html
http://www.opengroup.org/onlinepubs/007904975/functions/printf.html
Where is %b defined?
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf for converting numbers to letters, bug?
2004-03-27 23:53 ` Bart Schaefer
@ 2004-03-28 4:49 ` Clint Adams
2004-03-29 11:57 ` Oliver Kiddle
0 siblings, 1 reply; 5+ messages in thread
From: Clint Adams @ 2004-03-28 4:49 UTC (permalink / raw)
To: Bart Schaefer; +Cc: zsh-workers
> Zsh is (as of 4.2.0) behaving such that, given x='101',
>
> printf "%c" $x
> and
> print -r $x[1]
>
> produce the same output. That behavior is nearly useless. (It is how
> /usr/bin/printf on Linux works, though, so I guess it can be interpreted
> as a compatibility issue.)
Solaris too.
> If %b interprets the string \0101 as an octal number and prints the
> corresponding character, why shouldn't %c interpret the string 101 as a
> decimal number and print the corresponding character?
Compatibility. I think an option to do that would be okay, though
perhaps a bit confusing. I think it would be nice if bin_printf could
peek back in time to see if $x had PM_INTEGER set before it got
expanded, but that seems like more trouble than it's worth.
> http://www.opengroup.org/onlinepubs/007904975/utilities/printf.html
>
> I notice that although it mentions %b, there's NO definition of %b on any
> of the pages it cross-references:
>
> http://www.opengroup.org/onlinepubs/007904975/basedefs/xbd_chap05.html
> http://www.opengroup.org/onlinepubs/007904975/functions/printf.html
>
> Where is %b defined?
In #7 of EXTENDED DESCRIPTION of the first URL you cited.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: printf for converting numbers to letters, bug?
2004-03-28 4:49 ` Clint Adams
@ 2004-03-29 11:57 ` Oliver Kiddle
0 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2004-03-29 11:57 UTC (permalink / raw)
To: zsh-workers
Clint Adams wrote:
> > Zsh is (as of 4.2.0) behaving such that, given x='101',
> >
> > printf "%c" $x
> > and
> > print -r $x[1]
> >
> > produce the same output. That behavior is nearly useless. (It is how
> > /usr/bin/printf on Linux works, though, so I guess it can be interpreted
> > as a compatibility issue.)
It's how every other printf works and I remember thinking about it when
I implemented zsh's printf. Unlike C, we don't have separate character
and string types. So it is fairly useless but consistent with C printf.
> > If %b interprets the string \0101 as an octal number and prints the
> > corresponding character, why shouldn't %c interpret the string 101 as a
> > decimal number and print the corresponding character?
%b is defined to be like %s except the escape sequences of echo (which
are slightly different to those of the printf format) are understood.
We can't make %c interpret 101 without breaking compatibility.
> Compatibility. I think an option to do that would be okay, though
> perhaps a bit confusing. I think it would be nice if bin_printf could
> peek back in time to see if $x had PM_INTEGER set before it got
> expanded, but that seems like more trouble than it's worth.
Peeking back would be nasty. If you really think the feature is worth
having, we could add a new formatting directive (such as %C). What
would you want it to do for values greater than 127 though?
I can't see anything wrong with the %b trick though. Can be used with
\0, \x or \u. Only problem from the perspective of the original post is
that brace expansion uses decimal. I suppose we could allow things like
{8#62..101}
Oliver
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-03-29 11:58 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-26 16:42 (Fwd) printf for converting numbers to letters, bug? Bart Schaefer
2004-03-26 17:00 ` Clint Adams
2004-03-27 23:53 ` Bart Schaefer
2004-03-28 4:49 ` Clint Adams
2004-03-29 11:57 ` Oliver Kiddle
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).