Empty rules point to a null byte

This commit is contained in:
Devine Lu Linvega 2024-05-21 10:41:27 -07:00
parent ff0c53cf68
commit 170cfde437
1 changed files with 10 additions and 15 deletions

View File

@ -222,13 +222,11 @@ apply_rule(Rule *r, char *s)
c = *s;
if(!spacer(c)) return 0;
/* phase: write rule */
if(b != NULL) {
while((c = *b++))
if(c == '?' && (rid = *b) && (reg = regs[rid]))
write_reg(rid, reg), b++;
else
*dst_++ = c;
}
if(dst_ == origin) {
while(*s == ' ') s++;
if(*s == ')' && *(dst_ - 1) == ' ') dst_--;
@ -243,11 +241,8 @@ parse_frag(char **side, char *s)
char c, *cap;
while((c = *s) && c == ' ') s++;
if(c == ')' || (c == '<' && s[1] == '>') || (c == '>' && s[1] == '<')) {
*side = NULL;
*side = dict_, *dict_++ = 0;
return s;
} else if((c == '(' && s[1] == ')')) {
*side = NULL;
return s + 2;
} else {
cap = walk(s), *side = dict_;
if(c == '(')
@ -277,14 +272,14 @@ static void
remove_rule(Rule *r)
{
if(r < rules_ - 1) {
int distance = slen(r->a) + (r->b != NULL ? slen(r->b) : 0);
int distance = slen(r->a) + slen(r->b);
char *memsrc = (r + 1)->a;
copy(memsrc, r->a, dict_ - memsrc);
while(r < rules_ - 1) {
Rule *next = r + 1;
r->id = next->id, r->refs = next->refs;
r->a = next->a - distance;
r->b = next->b == NULL ? NULL : next->b - distance;
r->b = next->b - distance;
r++;
}
}
@ -315,7 +310,7 @@ rewrite(void)
if(c == '<' && s[1] == '>') {
r = rules_, r->id = rules_ - rules;
s = parse_frag(&r->b, parse_frag(&r->a, s + 2));
if(r->a != NULL) {
if(*r->a) {
if(!quiet) fprintf(stderr, "<> (%s) (%s)\n", r->a ? r->a : "", r->b ? r->b : "");
rules_++;
}
@ -330,7 +325,7 @@ rewrite(void)
parse_frag(&r->b, parse_frag(&r->a, s + 2));
s = cap;
while(*s == ' ') s++;
if(r->a == NULL || !apply_rule(r, s)) write_tail(s);
if(!(*r->a) || !apply_rule(r, s)) write_tail(s);
dict_ = d;
return 1;
}