commit 71c431c23ebdafb7c8efadf4131a0c002169ee55
parent 9e388846ad8e90f8dd06e264fcf8a7e1ec20344f
Author: sin <sin@2f30.org>
Date: Mon, 21 Dec 2015 22:30:53 +0000
consume last char from prev type
Diffstat:
M | ben.c | | | 23 | ++++++++++++----------- |
1 file changed, 12 insertions(+), 11 deletions(-)
diff --git a/ben.c b/ben.c
@@ -29,16 +29,16 @@ parsestr(char *s, char *e, struct ben **b)
len = len * 10 + *p - '0';
if (p == e)
error("bad string");
- if (*p != ':')
+ if (*p++ != ':')
error("expected ':' in string");
- if (p + len >= e)
+ if (p + len > e)
error("bad string");
p += len;
*b = emalloc(sizeof(**b));
(*b)->type = 's';
- (*b)->s = &p[-len + 1];
+ (*b)->s = &p[-len];
(*b)->start = s;
- (*b)->end = p + 1;
+ (*b)->end = p;
(*b)->len = len;
return p;
}
@@ -77,11 +77,13 @@ parseint(char *s, char *e, struct ben **b)
error("unexpected char in integer");
v = v * 10 + *p - '0';
}
+ if (*p++ != 'e')
+ error("expected terminator");
*b = emalloc(sizeof(**b));
(*b)->type = 'i';
(*b)->start = s;
- (*b)->end = p + 1;
+ (*b)->end = p;
(*b)->i = isneg ? -v : v;
return p;
}
@@ -98,8 +100,9 @@ parsedl(char *s, char *e, struct ben **b)
memset(&head, 0, sizeof(head));
if (*p != 'd' && *p != 'l')
error("expected dictionary or list");
+ p++;
while (1) {
- if (++p == e)
+ if (p == e)
error("bad dictionary or list");
if (*p == 'e')
break;
@@ -108,14 +111,12 @@ parsedl(char *s, char *e, struct ben **b)
bp = bp->next;
bp->type = type;
bp->k = NULL;
- if (type == 'd') {
+ if (type == 'd')
p = parsestr(p, e, &bp->k);
- p++;
- }
p = parse(p, e, &bp->v);
bp->next = NULL;
}
- if (*p != 'e')
+ if (*p++ != 'e')
error("expected terminator");
*b = head.next;
@@ -128,7 +129,7 @@ parsedl(char *s, char *e, struct ben **b)
}
(*b)->len = len;
(*b)->start = s;
- (*b)->end = p + 1;
+ (*b)->end = p;
return p;
}