[hackers] [sbase] expand: Use strsep() for parsing the tablist || sin
commit 34c9083598aa920645e1db3e6d5bc48a35f141b7
Author: sin <sin_AT_2f30.org>
Date: Sun Jan 25 18:13:32 2015 +0000
expand: Use strsep() for parsing the tablist
diff --git a/expand.c b/expand.c
index 3862afa..3135e33 100644
--- a/expand.c
+++ b/expand.c
_AT_@ -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
_AT_@ -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);
Received on Tue Mar 24 2015 - 23:53:16 CET
This archive was generated by hypermail 2.3.0
: Wed Mar 25 2015 - 00:03:10 CET