Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

0b numeric prefix



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



Messages sorted by: Reverse Date, Date, Thread, Author