commit 34c9083598aa920645e1db3e6d5bc48a35f141b7
parent bc9c752df5a2579bf5734135e8446af9bb6b0dca
Author: sin <sin@2f30.org>
Date: Sun, 25 Jan 2015 18:13:32 +0000
expand: Use strsep() for parsing the tablist
Diffstat:
M | expand.c | | | 52 | +++++++++++++++++++--------------------------------- |
1 file changed, 19 insertions(+), 33 deletions(-)
diff --git a/expand.c b/expand.c
@@ -7,44 +7,30 @@
#include "util.h"
static int iflag = 0;
-static ssize_t *tablist = NULL;
+static size_t *tablist = NULL;
static size_t tablistlen = 0;
static size_t
-parselist(const char *s, size_t slen)
+parselist(const char *s)
{
- size_t i, m, len;
- char *sep;
+ size_t i;
+ char *p, *tmp;
- if (s[0] == ',' || s[0] == ' ')
- eprintf("expand: tablist can't begin with a ',' or ' '.\n");
- if (s[slen - 1] == ',' || s[slen - 1] == ' ')
- eprintf("expand: tablist can't end with a ',' or ' '.\n");
-
- len = 1;
- for (i = 0; i < slen; i++) {
- if (s[i] == ',' || s[i] == ' ') {
- if (i > 0 && (s[i - 1] == ',' || s[i - 1] == ' '))
- eprintf("expand: empty field in tablist.\n");
- len++;
- }
+ tmp = estrdup(s);
+ for (i = 0; (p = strsep(&tmp, " ,")); i++) {
+ if (*p == '\0')
+ eprintf("empty field in tablist\n");
+ tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
+ tablist[i] = estrtol(p, 10);
+ if (!tablist[i] || tablist[i] < 0)
+ eprintf("tab field must be positive\n");
+ if (i > 0 && tablist[i - 1] >= tablist[i])
+ eprintf("tablist must be ascending\n");
}
- tablist = emalloc((len + 1) * sizeof(ssize_t));
-
- m = 0;
- for (i = 0; i < slen; i += sep - (s + i) + 1) {
- tablist[m++] = strtol(s + i, &sep, 10);
- if (tablist[m - 1] <= 0)
- eprintf("expand: tab size can't be negative or zero.\n");
- if (*sep && *sep != ',' && *sep != ' ')
- eprintf("expand: invalid number in tablist.\n");
- if (m > 1 && tablist[m - 1] < tablist[m - 2])
- eprintf("expand: tablist must be ascending.\n");
- }
-
+ tablist = erealloc(tablist, (i + 1) * sizeof(*tablist));
/* tab length = 1 for the overflowing case later in the matcher */
- tablist[len] = 1;
- return len;
+ tablist[i] = 1;
+ return i;
}
static int
@@ -114,13 +100,13 @@ main(int argc, char *argv[])
case 't':
tl = EARGF(usage());
if (!*tl)
- eprintf("expand: tablist cannot be empty.\n");
+ eprintf("tablist cannot be empty\n");
break;
default:
usage();
} ARGEND;
- tablistlen = parselist(tl, strlen(tl));
+ tablistlen = parselist(tl);
if (argc == 0)
expand("<stdin>", stdin);