diff -r 172527f61bd3 seq.c --- a/seq.c Mon Apr 16 21:48:50 2012 +0200 +++ b/seq.c Mon Apr 16 18:27:13 2012 -0500 @@ -5,7 +5,6 @@ #include #include #include -#include #include "util.h" @@ -30,12 +29,12 @@ char *exp; int shift; - if (d[0] == '-' || d[0] == '+') + if (d[0] == '+') d++; exp = strpbrk(d, "eE"); shift = exp? atoi(exp+1) : 0; - return MAX(0, strspn(d, "0123456789")+shift); + return MAX(0, strspn(d, "-0123456789")+shift); } int @@ -44,8 +43,6 @@ char *exp; int shift, after; - if (d[0] == '-' || d[0] == '+') - d++; exp = strpbrk(d, "eE"); shift = exp ? atoi(exp+1) : 0; after = (d = strchr(d, '.'))? strspn(d+1, "0123456789") : 0; @@ -56,15 +53,29 @@ int validfmt(char *fmt) { - regex_t reg; - int ret; - - regcomp(®, "\\([^%]|%%\\)*%[0-9]*\\.[0-9]*[fFgG]" - "\\([^%]|%%\\)*", REG_NOSUB); - ret = regexec(®, fmt, 0, NULL, 0); - regfree(®); - - return (ret == 0); + int end = 0; +NonFormat: + while(*fmt) + if(*fmt++=='%') + goto Format; + return 1; +Format: + if(*fmt=='%'){ + fmt++; + goto NonFormat; + } + if(end) + return 0; + if(strchr(" +-", *fmt)) + fmt++; + fmt+=strspn(fmt, "0123456789"); + if(*fmt == '.') + fmt++; + fmt+=strspn(fmt, "0123456789"); + if(!strchr("fFgGeE", *fmt)) + return 0; + end = 1; + goto NonFormat; } int @@ -72,7 +83,7 @@ { char c, *fmt, ftmp[4096], *sep, *starts, *steps, *ends; bool wflag, fflag; - double start, step, end, out; + double start, step, end, out, dir; int left, right; sep = "\n"; @@ -101,7 +112,6 @@ break; } } - if (wflag && fflag) eprintf("-f and -w cannot be combined.\n"); @@ -134,21 +144,14 @@ start = atof(starts); step = atof(steps); end = atof(ends); + dir = (step>0)?1.0:-1.0; if (step == 0) return EXIT_FAILURE; + if (start*dir > end*dir) + return EXIT_FAILURE; - if (start > end) { - if (step > 0) - return EXIT_FAILURE; - } else if (start < end) { - if (step < 0) - return EXIT_FAILURE; - } - - right = MAX(digitsright(starts), - MAX(digitsright(ends), - digitsright(steps))); + right = MAX(digitsright(starts), digitsright(steps)); if (wflag) { left = MAX(digitsleft(starts), digitsleft(ends)); @@ -161,23 +164,11 @@ for (out = start;;) { printf(fmt, out); - out += step; - if (start > end) { - if (out >= end) { - printf("%s", sep); - } else { - break; - } - } else if (start < end) { - if (out <= end) { - printf("%s", sep); - } else { - break; - } - } else { + if(out*dir <= end*dir) + printf("%s", sep); + else break; - } } printf("\n");