zsh-workers
 help / color / mirror / code / Atom feed
From: Stephane Chazelas <Stephane_Chazelas@yahoo.fr>
To: Peter Stephenson <pws@csr.com>
Cc: Zsh hackers list <zsh-workers@sunsite.dk>
Subject: Re: regression in $(([##16]...))?
Date: Thu, 16 Oct 2008 11:04:54 +0100	[thread overview]
Message-ID: <20081016100454.GC6376@sc.homeunix.net> (raw)
In-Reply-To: <20081016103129.77a25e33@news01>

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@yahoo.fr> 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


      reply	other threads:[~2008-10-16 10:05 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-16  9:23 Stephane Chazelas
2008-10-16  9:31 ` Peter Stephenson
2008-10-16 10:04   ` Stephane Chazelas [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20081016100454.GC6376@sc.homeunix.net \
    --to=stephane_chazelas@yahoo.fr \
    --cc=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).