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