From bea145baecab9bda52fa2838e92e2e3cf54d43a9 Mon Sep 17 00:00:00 2001 From: kappa Date: Mon, 4 Mar 2019 01:14:41 +0100 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=BA=D0=BB=D0=BE=D1=9A=D0=B5=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=BE=D1=82=D1=80=D0=B5=D0=B1=D0=B0=20=D0=B7?= =?UTF-8?q?=D0=B0=20=D1=80=D0=B5=D0=B3=D0=B5=D0=BA=D1=81=20=D0=B1=D0=B8?= =?UTF-8?q?=D0=B1=D0=BB=D0=B8=D0=BE=D1=82=D0=B5=D0=BA=D0=BE=D0=BC,=20?= =?UTF-8?q?=D1=83=D0=BC=D0=B0=D1=9A=D0=B5=D0=BD=20=D0=B7=D0=B0=D1=85=D1=82?= =?UTF-8?q?=D0=B5=D0=B2=20=D0=B7=D0=B0=20=D0=9F=D0=BE=D1=81=D0=B8=D0=BA?= =?UTF-8?q?=D1=81=20=D1=81=D1=82=D0=B0=D0=BD=D0=B4=D0=B0=D1=80=D0=B4=20?= =?UTF-8?q?=D0=BE=D0=BA=D1=80=D1=83=D0=B6=D0=B5=D1=9A=D0=B0=20=D1=83=20?= =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D1=83=20=D0=BA=D0=BE=D0=BC?= =?UTF-8?q?=D0=BF=D0=B0=D1=98=D0=BB=D0=B8=D1=80=D0=B0=D1=9A=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 7 +++--- read.c | 66 +++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 47 insertions(+), 26 deletions(-) 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; }