diff --git a/Makefile b/Makefile index 3eb018f..e0f92d0 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,9 @@ PREFIX = /usr/local LIBPREFIX = $(PREFIX)/lib # флегови за C компајлер и линкер -CPPFLAGS = -D_POSIX_C_SOURCE=200200L -DDESTDIR=\"$(DESTDIR)\" \ --DVERSION=\"$(VERSION)\" -CFLAGS = -g -std=c99 -pedantic -Wall -O0 -# CFLAGS = -std=c99 -pedantic -Wall -O3 +CPPFLAGS = -D_POSIX_C_SOURCE=2 -DDESTDIR=\"$(DESTDIR)\" -DVERSION=\"$(VERSION)\" +# CFLAGS = -g -std=c99 -pedantic -Wall -O0 +CFLAGS = -std=c99 -pedantic -Wall -O3 LDFLAGS = -lm -lc CC = cc diff --git a/read.c b/read.c index 1098673..13ba1b0 100644 --- a/read.c +++ b/read.c @@ -4,7 +4,6 @@ #include #include #include -#include #include "util.h" #include "read.h" @@ -138,12 +137,45 @@ wint_t unscanwc(wint_t c, FILE *stream) } } -int lengthDigitArray(char *s) +int validFracNum(char *s) { - int i; - for (i = 0; isdigit(s[i]); ++i) - ; - return i; + char *endptr; + strtoll(s, &endptr, 10); + if (*endptr == '\0' && endptr != s) + { + return 1; + } + else if (*endptr == '/' && endptr != s) + { + char *denom = endptr + 1; + strtoll(denom, &endptr, 10); + if (*endptr == '\0' && endptr != denom) + { + return 1; + } + else + { + return 0; + } + } + else + { + return 0; + } +} + +int validRealNum(char *s) +{ + char *endptr; + strtold(s, &endptr); + if (*endptr == '\0') + { + return 1; + } + else + { + return 0; + } } object getToken(FILE *stream) @@ -190,25 +222,17 @@ object getToken(FILE *stream) char *s = malloc(n * sizeof(char)); wcstombs(s, buffer, n); - regex_t regNumberFrac, regNumberReal; - regcomp(®NumberFrac, "^[-+]?[[:digit:]]+(/[[:digit:]]+)?$", - REG_EXTENDED); - regcomp(®NumberReal, "^[-+]?[[:digit:]]*,[[:digit:]]+$", - REG_EXTENDED); - const int nmatches = 1; - regmatch_t a[nmatches]; - - if (!regexec(®NumberFrac, s, nmatches, a, 0)) + char *endptr; + if (validFracNum(s)) { TYPE(result) = numberObject; NUM_TYPE(result) = fractionNum; - NUM_NUMER(result) = atoll(s); - char *tmp; - NUM_DENOM(result) = (tmp = strchr(s, '/')) == NULL ? - 1 : atoll(tmp + 1); + NUM_NUMER(result) = strtoll(s, &endptr, 10); + NUM_DENOM(result) = *endptr == '/' ? + strtoll(endptr + 1, &endptr, 10) : 1; result = shortenFractionNum(result); } - else if (!regexec(®NumberReal, s, nmatches, a, 0)) + else if (validRealNum(s)) { TYPE(result) = numberObject; NUM_TYPE(result) = realNum; @@ -220,8 +244,6 @@ object getToken(FILE *stream) SYM(result) = malloc((strlen(s) + 1) * sizeof(char)); strcpy(SYM(result), s); } - regfree(®NumberFrac); - regfree(®NumberReal); return result; }