Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/historical/nawk/dist check explicitly for inf and n...



details:   https://anonhg.NetBSD.org/src/rev/b7b0fae628c7
branches:  trunk
changeset: 938068:b7b0fae628c7
user:      christos <christos%NetBSD.org@localhost>
date:      Tue Sep 01 00:21:01 2020 +0000

description:
check explicitly for inf and nan. We can't check if it is a number,
because awk parses 1a as 1...

diffstat:

 external/historical/nawk/dist/tran.c |  24 +++++++++++++++++++++---
 1 files changed, 21 insertions(+), 3 deletions(-)

diffs (43 lines):

diff -r 8add2b8fb314 -r b7b0fae628c7 external/historical/nawk/dist/tran.c
--- a/external/historical/nawk/dist/tran.c      Mon Aug 31 23:37:55 2020 +0000
+++ b/external/historical/nawk/dist/tran.c      Tue Sep 01 00:21:01 2020 +0000
@@ -395,6 +395,25 @@
        return(vp->sval);
 }
 
+static int checkstr(const char *s, const char *v)
+{
+       while (*s && tolower((unsigned char)*s) == *v)
+               s++, v++;
+       return !(*s || *v);
+}
+
+static int checkinfnan(const char *s)
+{
+       switch (tolower((unsigned char)*s)) {
+       case 'i':
+               return checkstr(s, "inf") || checkstr(s, "infinity");
+       case 'n':
+               return checkstr(s, "nan");
+       default:
+               return 1;
+       }
+}
+
 Awkfloat getfval(Cell *vp)     /* get float val of a Cell */
 {
        if ((vp->tval & (NUM | STR)) == 0)
@@ -404,11 +423,10 @@
        else if (isrec(vp) && !donerec)
                recbld();
        if (!isnum(vp)) {       /* not a number */
+               if (checkinfnan(vp->sval))
+                       vp->fval = atof(vp->sval);      /* best guess */
                if (is_number(vp->sval) && !(vp->tval&CON)) {
-                       vp->fval = atof(vp->sval);      /* best guess */
                        vp->tval |= NUM;        /* make NUM only sparingly */
-               } else {
-                       vp->fval = 0;
                }
        }
           dprintf( ("getfval %p: %s = %g, t=%o\n",



Home | Main Index | Thread Index | Old Index