zsh-workers
 help / color / mirror / code / Atom feed
* 0b numeric prefix
@ 2014-11-25 16:07 Peter Stephenson
  2014-11-25 16:47 ` Christian Neukirchen
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2014-11-25 16:07 UTC (permalink / raw)
  To: Zsh Hackers' List

This adds the fairly common (but certainly far from universal) 0b prefix
to denote binary on input.  I think numeric parsing is designed so this
can't conflict with anything existing that's valid.

I'm not sure whether to output this form in C_BASES mode.  On the one
hand, it's more likely to be understood on input in utilities other than
shells than 2#blah; on the other, it won't work with older versions of
zsh or other shells (not that C_BASES is designed with other shells in
mind).  Because of the latter I'm not intending to do this.  It would
also be a perversion of the name of the option.

I'll add some tests later.

pws


diff --git a/Doc/Zsh/arith.yo b/Doc/Zsh/arith.yo
index 96dc2dc..a620b73 100644
--- a/Doc/Zsh/arith.yo
+++ b/Doc/Zsh/arith.yo
@@ -39,7 +39,8 @@ zero status.
 cindex(arithmetic base)
 cindex(bases, in arithmetic)
 Integers can be in bases other than 10.
-A leading `tt(0x)' or `tt(0X)' denotes hexadecimal.
+A leading `tt(0x)' or `tt(0X)' denotes hexadecimal and a leading
+`tt(0b)' or `tt(0B) binary.
 Integers may also be of the form `var(base)tt(#)var(n)',
 where var(base) is a decimal number between two and thirty-six
 representing the arithmetic base and var(n)
diff --git a/Src/math.c b/Src/math.c
index 2665698..438a170 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -449,12 +449,14 @@ lexconstant(void)
 	nptr++;
 
     if (*nptr == '0') {
+	int lowchar;
 	nptr++;
-	if (*nptr == 'x' || *nptr == 'X') {
+	lowchar = tolower(*nptr);
+	if (lowchar == 'x' || lowchar == 'b') {
 	    /* Let zstrtol parse number with base */
 	    yyval.u.l = zstrtol_underscore(ptr, &ptr, 0, 1);
 	    /* Should we set lastbase here? */
-	    lastbase = 16;
+	    lastbase = (lowchar == 'b') ? 2 : 16;
 	    if (isset(FORCEFLOAT))
 	    {
 		yyval.type = MN_FLOAT;
diff --git a/Src/utils.c b/Src/utils.c
index c6e7aed..5f0c106 100644
--- a/Src/utils.c
+++ b/Src/utils.c
@@ -2082,6 +2082,8 @@ zstrtol_underscore(const char *s, char **t, int base, int underscore)
 	    base = 10;
 	else if (*++s == 'x' || *s == 'X')
 	    base = 16, s++;
+	else if (*s == 'b' || *s == 'B')
+	    base = 2, s++;
 	else
 	    base = 8;
     }

pws


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: 0b numeric prefix
  2014-11-25 16:07 0b numeric prefix Peter Stephenson
@ 2014-11-25 16:47 ` Christian Neukirchen
  2014-11-25 16:53   ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Christian Neukirchen @ 2014-11-25 16:47 UTC (permalink / raw)
  To: zsh-workers

Peter Stephenson <p.stephenson@samsung.com> writes:

> I'm not sure whether to output this form in C_BASES mode.  On the one
> hand, it's more likely to be understood on input in utilities other than
> shells than 2#blah; on the other, it won't work with older versions of
> zsh or other shells (not that C_BASES is designed with other shells in
> mind).  Because of the latter I'm not intending to do this.  It would
> also be a perversion of the name of the option.

C++14 has binary literals afaict.  gcc and clang implement it already.

-- 
Christian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: 0b numeric prefix
  2014-11-25 16:47 ` Christian Neukirchen
@ 2014-11-25 16:53   ` Peter Stephenson
  0 siblings, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2014-11-25 16:53 UTC (permalink / raw)
  To: zsh-workers

On Tue, 25 Nov 2014 17:47:08 +0100
Christian Neukirchen <chneukirchen@gmail.com> wrote:
> Peter Stephenson <p.stephenson@samsung.com> writes:
> 
> > I'm not sure whether to output this form in C_BASES mode.  On the one
> > hand, it's more likely to be understood on input in utilities other than
> > shells than 2#blah; on the other, it won't work with older versions of
> > zsh or other shells (not that C_BASES is designed with other shells in
> > mind).  Because of the latter I'm not intending to do this.  It would
> > also be a perversion of the name of the option.
> 
> C++14 has binary literals afaict.  gcc and clang implement it already.

Indeed, going back a few years now, apparently.  So might be worth an
option for output at some point.

pws


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2014-11-25 17:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-11-25 16:07 0b numeric prefix Peter Stephenson
2014-11-25 16:47 ` Christian Neukirchen
2014-11-25 16:53   ` Peter Stephenson

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).