[hackers] [sbase] Fixing validfmt and optimizing the algorithm. (Thanks David Galos) || Christoph Lohmann
changeset: 107:a9f4ddff0da7
tag: tip
user: Christoph Lohmann <20h_AT_r-36.net>
date: Wed Apr 18 13:41:05 2012 +0200
files: seq.c
description:
Fixing validfmt and optimizing the algorithm. (Thanks David Galos)
diff -r 172527f61bd3 -r a9f4ddff0da7 seq.c
--- a/seq.c Mon Apr 16 21:48:50 2012 +0200
+++ b/seq.c Wed Apr 18 13:41:05 2012 +0200
_AT_@ -5,7 +5,6 @@
#include <unistd.h>
#include <stdbool.h>
#include <libgen.h>
-#include <regex.h>
#include "util.h"
_AT_@ -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
_AT_@ -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;
_AT_@ -56,15 +53,36 @@
int
validfmt(char *fmt)
{
- regex_t reg;
- int ret;
+ int occur;
- regcomp(®, "\\([^%]|%%\\)*%[0-9]*\\.[0-9]*[fFgG]"
- "\\([^%]|%%\\)*", REG_NOSUB);
- ret = regexec(®, fmt, 0, NULL, 0);
- regfree(®);
+ occur = 0;
- return (ret == 0);
+NonFormat:
+ while(*fmt) {
+ if (*fmt++ == '%')
+ goto Format;
+ }
+ return (occur == 1);
+Format:
+ if (*fmt == '%') {
+ fmt++;
+ goto NonFormat;
+ }
+ fmt += strspn(fmt, "-+#0 '");
+ fmt += strspn(fmt, "0123456789");
+ if (*fmt == '.') {
+ fmt ++;
+ fmt += strspn(fmt, "0123456789");
+ }
+ if (*fmt == 'L')
+ fmt++;
+ if (*fmt == '\0')
+ return 0;
+ if (strchr("fFgGeEaA", *fmt)) {
+ occur++;
+ goto NonFormat;
+ }
+ return 0;
}
int
_AT_@ -72,7 +90,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";
_AT_@ -134,17 +152,12 @@
start = atof(starts);
step = atof(steps);
end = atof(ends);
+ dir = (step > 0)? 1.0 : -1.0;
if (step == 0)
return EXIT_FAILURE;
-
- if (start > end) {
- if (step > 0)
- return EXIT_FAILURE;
- } else if (start < end) {
- if (step < 0)
- return EXIT_FAILURE;
- }
+ if (start * dir > end * dir)
+ return EXIT_FAILURE;
right = MAX(digitsright(starts),
MAX(digitsright(ends),
_AT_@ -163,18 +176,8 @@
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;
- }
+ if (out * dir <= end * dir) {
+ printf("%s", sep);
} else {
break;
}
Received on Wed Apr 18 2012 - 13:41:15 CEST
This archive was generated by hypermail 2.3.0
: Wed Apr 18 2012 - 13:48:13 CEST