ref: b5edd86da2f935e6b8e77d60762fad31a263432a
parent: 1f95241f529adc736e20be2919696a8dab11a2e7
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Jan 11 04:11:12 EST 2018
[as] Trim characters from fields It was creating some stupid problems and the code wasn't very optimal. This new version is simpler (if you can read good C code) and avoid an additional run over the field.
--- a/as/parser.c
+++ b/as/parser.c
@@ -53,23 +53,29 @@
static char *
field(char **oldp, size_t *siz)
{
- char *s, *begin;
+ char *s, *t, *begin;
+ size_t n;
if ((begin = *oldp) == NULL)
return NULL;
- if (*begin == '/') {
- *begin = '\0';
- *oldp = NULL;
- } else if (s = memchr(begin, '\t', *siz)) {
- *s++ = '\0';
- *siz -= s - begin;
- *oldp = s;
- } else {
- *oldp = NULL;
+ for (s = begin; *s == ' '; ++s)
+ ;
+ if (*s == '\0' || *s == '/' || *s == ';') {
+ *t = '\0';
+ return *oldp = NULL;
}
- return (*begin != '\0') ? begin : NULL;
+ for (t = s; *t && *t != '\t'; ++t)
+ ;
+ if (*t == '\t')
+ *t++ = '\0';
+ *siz -= begin - t;
+ *oldp = t;
+
+ while (t >= s && *t == ' ')
+ *t-- = '\0';
+ return (*s != '\0') ? s : NULL;
}
static int
@@ -86,6 +92,11 @@
case '.':
case '$':
continue;
+ case ':':
+ if (*name != '\0')
+ return 0;
+ *--name = '\0';
+ continue;
default:
return 0;
}
@@ -98,14 +109,8 @@
{
int r = 0;
- if (lp->label = field(&s, &len)) {
- size_t n = strlen(lp->label);
- if (lp->label[n-1] == ':')
- lp->label[n-1] = '\0';
- if (!validlabel(lp->label))
- error("incorrect label name '%s'", lp->label);
+ if (lp->label = field(&s, &len))
r++;
- }
if (lp->op = field(&s, &len))
r++;
if (lp->args = field(&s, &len))
@@ -113,6 +118,8 @@
if (s && *s && *s != '/')
error("trailing characters at the end of the line");
+ if (lp->label && !validlabel(lp->label))
+ error("incorrect label name '%s'", lp->label);
return r;
}