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 Add a check_number function th...



details:   https://anonhg.NetBSD.org/src/rev/8add2b8fb314
branches:  trunk
changeset: 938067:8add2b8fb314
user:      christos <christos%NetBSD.org@localhost>
date:      Mon Aug 31 23:37:55 2020 +0000

description:
Add a check_number function that does what is repeated in many places in
the code, but better.

diffstat:

 external/historical/nawk/dist/lib.c   |  36 +++++++++++++++++++---------------
 external/historical/nawk/dist/proto.h |   1 +
 external/historical/nawk/dist/run.c   |  15 ++-----------
 3 files changed, 24 insertions(+), 28 deletions(-)

diffs (119 lines):

diff -r d86162612587 -r 8add2b8fb314 external/historical/nawk/dist/lib.c
--- a/external/historical/nawk/dist/lib.c       Mon Aug 31 23:36:58 2020 +0000
+++ b/external/historical/nawk/dist/lib.c       Mon Aug 31 23:37:55 2020 +0000
@@ -191,10 +191,7 @@
                                        xfree(fldtab[0]->sval);
                                fldtab[0]->sval = buf;  /* buf == record */
                                fldtab[0]->tval = REC | STR | DONTFREE;
-                               if (is_number(fldtab[0]->sval)) {
-                                       fldtab[0]->fval = atof(fldtab[0]->sval);
-                                       fldtab[0]->tval |= NUM;
-                               }
+                               check_number(fldtab[0]);
                        }
                        setfval(nrloc, nrloc->fval+1);
                        setfval(fnrloc, fnrloc->fval+1);
@@ -306,10 +303,7 @@
        p = qstring(p, '\0');
        q = setsymtab(s, p, 0.0, STR, symtab);
        setsval(q, p);
-       if (is_number(q->sval)) {
-               q->fval = atof(q->sval);
-               q->tval |= NUM;
-       }
+       check_number(q);
           dprintf( ("command line set %s to |%s|\n", s, p) );
 }
 
@@ -407,10 +401,7 @@
        donefld = true;
        for (j = 1; j <= lastfld; j++) {
                p = fldtab[j];
-               if(is_number(p->sval)) {
-                       p->fval = atof(p->sval);
-                       p->tval |= NUM;
-               }
+               check_number(p);
        }
        setfval(nfloc, (Awkfloat) lastfld);
        donerec = true; /* restore */
@@ -763,7 +754,8 @@
 /* wrong: violates 4.10.1.4 of ansi C standard */
 
 #include <math.h>
-int is_number(const char *s)
+
+static int get_number(const char *s, double *res)
 {
        double r;
        char *ep;
@@ -773,8 +765,20 @@
                return 0;
        while (*ep == ' ' || *ep == '\t' || *ep == '\n')
                ep++;
-       if (*ep == '\0')
-               return 1;
-       else
+       if (*ep != '\0')
                return 0;
+       if (res)                
+               *res = r;
+       return 1;
 }
+
+void check_number(Cell *x)
+{
+       if (get_number(x->sval, &x->fval))
+               x->tval |= NUM;
+}
+
+int is_number(const char *s)
+{
+       return get_number(s, NULL);
+}
diff -r d86162612587 -r 8add2b8fb314 external/historical/nawk/dist/proto.h
--- a/external/historical/nawk/dist/proto.h     Mon Aug 31 23:36:58 2020 +0000
+++ b/external/historical/nawk/dist/proto.h     Mon Aug 31 23:37:55 2020 +0000
@@ -149,6 +149,7 @@
 extern double  errcheck(double, const char *);
 extern int     isclvar(const char *);
 extern int     is_number(const char *);
+extern void    check_number(Cell *);
 
 extern int     adjbuf(char **, int *, int, int, char **, const char *);
 extern void    run(Node *);
diff -r d86162612587 -r 8add2b8fb314 external/historical/nawk/dist/run.c
--- a/external/historical/nawk/dist/run.c       Mon Aug 31 23:36:58 2020 +0000
+++ b/external/historical/nawk/dist/run.c       Mon Aug 31 23:37:55 2020 +0000
@@ -432,17 +432,11 @@
                } else if (a[0] != NULL) {      /* getline var <file */
                        x = execute(a[0]);
                        setsval(x, buf);
-                       if (is_number(x->sval)) {
-                               x->fval = atof(x->sval);
-                               x->tval |= NUM;
-                       }
+                       check_number(x);
                        tempfree(x);
                } else {                        /* getline <file */
                        setsval(fldtab[0], buf);
-                       if (is_number(fldtab[0]->sval)) {
-                               fldtab[0]->fval = atof(fldtab[0]->sval);
-                               fldtab[0]->tval |= NUM;
-                       }
+                       check_number(fldtab[0]);
                }
        } else {                        /* bare getline; use current input */
                if (a[0] == NULL)       /* getline */
@@ -451,10 +445,7 @@
                        n = getrec(&buf, &bufsize, false);
                        x = execute(a[0]);
                        setsval(x, buf);
-                       if (is_number(x->sval)) {
-                               x->fval = atof(x->sval);
-                               x->tval |= NUM;
-                       }
+                       check_number(x);
                        tempfree(x);
                }
        }



Home | Main Index | Thread Index | Old Index