i have a proposal for charstod that should allow it
to recognize numbers without trailing non number
characters.

        int     xcharstod(int(*f)(void*), void *vp, double *g)

returning -1 on failure to parse a number, and 0
otherwise.  in addition, g is set to 0. on failure, not
NaN in keeping with strtod.

the two uses in /sys/src are:

sys/src//cmd/units.y:272:       yylval.val = charstod(gdigit, 0);
/sys/src//libbio/bgetd.c:31:    d = charstod(Bgetdf, &b);

it would seem easy enough to update them to match.
eof tracking in Bgetd could just be dleted.

also, this orphan function should likely be retired:

/sys/src/ape/lib/fmt/charstod.c:28: fmtcharstod(int(*f)(void*), void *vp)
/sys/src/cmd/unix/drawterm/libc/charstod.c:13: fmtcharstod(int(*f)(void*), void 
*vp)
/sys/include/ape/fmt.h:106: extern      double  fmtcharstod(int(*)(void*), 
void*);

- erik

-----

#include <u.h>
#include <libc.h>

/*
 * Reads a floating-point number by interpreting successive characters
 * returned by (*f)(vp).  The last call it makes to f terminates the
 * scan, so is not a character in the number.  It may therefore be
 * necessary to back up the input stream up one byte after calling charstod.
 */

#define ADVANCE *s++ = c; if(s>=e) return -1; c = (*f)(vp)

int
xcharstod(int(*f)(void*), void *vp, double *g)
{
        char str[400], *s, *e, *start;
        int c;

        *g = 0.;
        s = str;
        e = str + sizeof str - 1;
        c = (*f)(vp);
        while(c == ' ' || c == '\t')
                c = (*f)(vp);
        if(c == '-' || c == '+'){
                ADVANCE;
        }
        start = s;
        while(c >= '0' && c <= '9'){
                ADVANCE;
        }
        if(c == '.'){
                ADVANCE;
                while(c >= '0' && c <= '9'){
                        ADVANCE;
                }
        }
        if(s > start && (c == 'e' || c == 'E')){
                ADVANCE;
                if(c == '-' || c == '+'){
                        ADVANCE;
                }
                while(c >= '0' && c <= '9'){
                        ADVANCE;
                }
        }else if(s == start && (c == 'i' || c == 'I')){
                ADVANCE;
                if(c != 'n' && c != 'N')
                        return -1;
                ADVANCE;
                if(c != 'f' && c != 'F')
                        return -1;
                ADVANCE;
                if(c != 'i' && c != 'I')
                        goto accept;
                ADVANCE;
                if(c != 'n' && c != 'N')
                        return -1;
                ADVANCE;
                if(c != 'i' && c != 'I')
                        return -1;
                ADVANCE;
                if(c != 't' && c != 'T')
                        return -1;
                ADVANCE;
                if(c != 'y' && c != 'Y')
                        return -1;
                ADVANCE;  /* so caller can back up uniformly */
                USED(c);
        }else if(s == str && (c == 'n' || c == 'N')){
                ADVANCE;
                if(c != 'a' && c != 'A')
                        return -1;
                ADVANCE;
                if(c != 'n' && c != 'N')
                        return -1;
                ADVANCE;  /* so caller can back up uniformly */
                USED(c);
        }
accept:
        *s = 0;
        *g = strtod(str, &s);
        if(s > str)
                return 0;
        return -1;
}

Reply via email to