zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: Zsh Hackers' List <zsh-workers@zsh.org>
Subject: Re: $[ 09.5 ]  -- bad math expression
Date: Thu, 14 Nov 2013 11:30:45 +0000	[thread overview]
Message-ID: <20131114113045.6bf509fc@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <121202135940.ZM19771@torch.brasslantern.com>

On Sun, 02 Dec 2012 13:59:40 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> In the patch below I pulled the memchr from the original code that was
> changed by 23165.  There may be a better way to do that test.
> 
> Index: Src/math.c
> ===================================================================
> RCS file: /cvsroot/zsh/zsh/Src/math.c,v
> retrieving revision 1.43
> diff -u -r1.43 math.c
> --- Src/math.c  11 Sep 2012 16:02:42 -0000      1.43
> +++ Src/math.c  2 Dec 2012 21:46:50 -0000
> @@ -447,7 +447,8 @@
>      if (*nptr == '-')
>  	nptr++;
>  
> -    if (*nptr == '0')
> +    if (*nptr == '0' &&
> +	(memchr(nptr, '.', strlen(nptr)) == NULL))

I've just very verybelatedly tracked down some problems I've been
seeing combining integers and floats...

  % print $(( 0x3000 + 0.5 ))
  zsh: bad math expression: operator expected at `x3000 + 0....'

to this memchr(), which is overkill --- it scans the entire rest of the
expression.

The only difficult case, in fact, is the test for OCTALZEROES (both the
option setting and checking the number), since for hexadecimal
we have an explicit signal that the value is a hex integer and it
certainly can't be a float if we find that.  It was almost there: the
problem was when we discovered it wasn't an octal we didn't continue
with the normal code for parsing the number before the decimal point if
octalzeroes *wasn't* set (noted by Bart in the original message).

So I think this is the simplest fix.  The while loop will perform one
extra set of tests if OCTALZEROES *is* set, but anything to remove that
is just as complicated as leaving it this way.

diff --git a/Src/math.c b/Src/math.c
index b21a3ad..42355f8 100644
--- a/Src/math.c
+++ b/Src/math.c
@@ -448,9 +448,7 @@ lexconstant(void)
     if (*nptr == '-')
 	nptr++;
 
-    if (*nptr == '0' &&
-	(memchr(nptr, '.', strlen(nptr)) == NULL))
-    {
+    if (*nptr == '0') {
 	nptr++;
 	if (*nptr == 'x' || *nptr == 'X') {
 	    /* Let zstrtol parse number with base */
@@ -491,11 +489,8 @@ lexconstant(void)
 	    nptr = ptr2;
 	}
     }
-    else
-    {
-	while (idigit(*nptr) || *nptr == '_')
-	    nptr++;
-    }
+    while (idigit(*nptr) || *nptr == '_')
+	nptr++;
 
     if (*nptr == '.' || *nptr == 'e' || *nptr == 'E') {
 	char *ptr2;
diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst
index c19135c..7b005c2 100644
--- a/Test/C01arith.ztst
+++ b/Test/C01arith.ztst
@@ -258,3 +258,11 @@
 >0.5
 >3
 >3.
+
+  print $(( 0x30 + 0.5 ))
+  print $(( 077 + 0.5 ))
+  (setopt octalzeroes; print $(( 077 + 0.5 )) )
+0:Mixed float and non-decimal integer constants
+>48.5
+>77.5
+>63.5

pws


      parent reply	other threads:[~2013-11-14 11:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <alpine.DEB.2.00.1212022323450.27859@oreo.lan>
     [not found] ` <20121202210516.66e31219@pws-pc.ntlworld.com>
2012-12-04 15:52   ` Those patches " Bart Schaefer
2012-12-04 16:07     ` Peter Stephenson
     [not found] ` <121202135940.ZM19771@torch.brasslantern.com>
2013-11-14 11:30   ` Peter Stephenson [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=20131114113045.6bf509fc@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-workers@zsh.org \
    /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).