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

Re: regression in $(([##16]...))?



On Thu, Oct 16, 2008 at 10:31:29AM +0100, Peter Stephenson wrote:
> On Thu, 16 Oct 2008 10:23:08 +0100
> Stephane Chazelas <Stephane_Chazelas@xxxxxxxx> wrote:
> > ~$ echo $(([##16]12))
> > zsh: invalid base (must be 2 to 36 inclusive): -16
> > (1)~$ echo $(([##2]12))
> > zsh: invalid base (must be 2 to 36 inclusive): -2
> > 
> > (4.3.6-dev-0+1004)
> > 
> > I remember a check for the allowed bases was introduced at some
> > point, that may be what broke it.
> 
> That feature's so obscure I didn't know it was there and it's never been
> tested.

Obscure but quite handy, I'm surprised there's no equivalent in
other shells, you have to resort to bc/dc in those.

> Index: Src/math.c
> ===================================================================
> RCS file: /cvsroot/zsh/zsh/Src/math.c,v
> retrieving revision 1.35
> diff -u -r1.35 math.c
> --- Src/math.c	26 Sep 2008 09:11:30 -0000	1.35
> +++ Src/math.c	16 Oct 2008 09:30:04 -0000
> @@ -670,7 +670,8 @@
>  		}
>  		if(*ptr != ']')
>  			goto bofs;
> -		if (outputradix < 2 || outputradix > 36) {
> +		n = (outputradix < 0) ? -outputradix : outputradix;
> +		if (n < 2 || n > 36) {
>  		    zerr("invalid base (must be 2 to 36 inclusive): %d",
>  			 outputradix);
>  		    return EOI;
[...]

I've been too slow I was about to suggest to rewrite maybe a bit
more legibly as:

Index: Src/math.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/math.c,v
retrieving revision 1.35
diff -p -u -r1.35 math.c
--- Src/math.c	26 Sep 2008 09:11:30 -0000	1.35
+++ Src/math.c	16 Oct 2008 10:01:04 -0000
@@ -643,40 +643,44 @@ zzlex(void)
 	    return EOI;
 	case '[':
 	    {
-		int n;
-
 		if (idigit(*ptr)) {
-		    n = zstrtol(ptr, &ptr, 10);
+		    int base = zstrtol(ptr, &ptr, 10);
 		    if (*ptr != ']' || !idigit(*++ptr)) {
 			zerr("bad base syntax");
 			return EOI;
 		    }
-		    yyval.u.l = zstrtol(ptr, &ptr, lastbase = n);
+		    yyval.u.l = zstrtol(ptr, &ptr, lastbase = base);
 		    return NUM;
-		}
-		if (*ptr == '#') {
-		    n = 1;
-		    if (*++ptr == '#') {
-			n = -1;
-			ptr++;
-		    }
-		    if (!idigit(*ptr))
-			goto bofs;
-		    outputradix = n * zstrtol(ptr, &ptr, 10);
 		} else {
-		    bofs:
+		    if (*ptr == '#') {
+			int double_hash = 0;
+
+			if (*++ptr == '#') {
+			    double_hash = 1;
+			    ptr++;
+			}
+
+			if (idigit(*ptr)) {
+			    outputradix = zstrtol(ptr, &ptr, 10);
+
+			    if (outputradix < 2 || outputradix > 36) {
+				zerr("invalid base (must be 2 to 36 inclusive): %d",
+				     outputradix);
+				return EOI;
+			    }
+
+			    if (double_hash)
+				outputradix = -outputradix;
+
+			    ptr++;
+
+			    if(*ptr == ']')
+				break;
+			}
+		    }
 		    zerr("bad output format specification");
 		    return EOI;
 		}
-		if(*ptr != ']')
-			goto bofs;
-		if (outputradix < 2 || outputradix > 36) {
-		    zerr("invalid base (must be 2 to 36 inclusive): %d",
-			 outputradix);
-		    return EOI;
-		}
-		ptr++;
-		break;
 	    }
 	case ' ':
 	case '\t':

Cheers,
Stéphane



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