Fix another edge case in expr_match

This commit is contained in:
dmiller 2023-12-01 23:38:20 +00:00
parent ee1d570b4e
commit f3f9ca7a8b
2 changed files with 45 additions and 8 deletions

View file

@ -308,27 +308,28 @@ bool expr_match(const char *val, size_t vlen, const char *expr, size_t explen, b
}
p = expr;
const char *p_end = p + explen;
do {
const char *nest = NULL; // where the [] nested expr starts
const char *subval = val; // portion of val after previous nest and before the next one
size_t sublen; // length of subval not subject to nested matching
q = strchr(p, '|');
q = strchr_p(p, p_end, '|');
// if we're already in a nested expr, we skip this and just match as usual.
if (do_nested) {
nest = strchr(p, '[');
subval = val;
nest = strchr_p(p, p_end, '[');
// As long as we keep finding nested portions, e.g. M[>500]ST11W[1-5]
while (nest) {
q1 = strchr(nest, ']');
q1 = strchr_p(nest, p_end, ']');
assert(q1);
if (q && q < q1) {
// "AB[C|D]E|XYZ"
q = strchr(q1, '|');
q = strchr_p(q1, p_end, '|');
}
// "AB[C-D]E" or or "AB[C-D]E|F"
sublen = nest - p;
//fprintf(stderr, "subcmp(%-.*s, %-.*s)\n", sublen, p, sublen, subval);
if (strncmp(p, subval, sublen) != 0) {
goto next_expr;
}
@ -339,10 +340,10 @@ bool expr_match(const char *val, size_t vlen, const char *expr, size_t explen, b
nlen++;
}
p = q1 + 1;
// fprintf(stderr, "nest: %-.*s cmp %-.*s\n", nlen, subval, q1 - nest, nest);
//fprintf(stderr, "nest: %-.*s cmp %-.*s\n", nlen, subval, q1 - nest, nest);
if (nlen > 0 && expr_match(subval, nlen, nest, q1 - nest, false)) {
subval += nlen;
nest = strchr(p, '[');
nest = strchr_p(p, p_end, '[');
}
else {
goto next_expr;
@ -370,7 +371,7 @@ bool expr_match(const char *val, size_t vlen, const char *expr, size_t explen, b
}
goto next_expr;
} else {
q1 = strchr(p, '-');
q1 = strchr_p(p, p_end, '-');
if (q1 != NULL) {
size_t sublen1 = q1 - p;
if ((vlen > sublen1)
@ -386,6 +387,7 @@ bool expr_match(const char *val, size_t vlen, const char *expr, size_t explen, b
}
}
}
//fprintf(stderr, "cmp(%-.*s, %-.*s)\n", sublen, p, vlen, subval);
if (vlen == sublen && !strncmp(p, subval, vlen)) {
return true;
}

View file

@ -44,6 +44,9 @@ const struct expr_test tests[] = {
{"M123|B", "M123", true},
{"B|M123", "B", true},
{"B|M123", "M123", true},
{"123|1BC|ABC", "123", true},
{"123|1BC|ABC", "1BC", true},
{"123|1BC|ABC", "ABC", true},
{"<A", "9", true},
{"", "1", false},
{"", "anything", false},
@ -116,7 +119,39 @@ const struct expr_test tests[] = {
{"[>A7]S", "FS", false},
{"[>A7]S", "A6S", false},
{"G", "0", false},
{">0", "G", false},
{"<FFFF", "G", false},
{"0-FFFF", "G", false},
{"M[500|5B4]S", "M500S", true},
{"M[500|5B4]S", "M5B4S", true},
{"M[500|5B4]S", "M500", false},
{"M[500|5B4]S", "M5B4", false},
{"M[500|5B4]S", "M500ST", false},
{"M[500|5B4]S", "M5B4ST", false},
{"M[500|5B4]ST11NW[7|9]", "M500ST11NW7", true},
{"M[500|5B4]ST11NW[7|9]", "M5B4ST11NW7", true},
{"M[500|5B4]ST11NW[7|9]", "M500ST11NW9", true},
{"M[500|5B4]ST11NW[7|9]", "M5B4ST11NW9", true},
{"M[500|5B4]ST11NW[7|9]", "M5ST11NW7", false},
{"M[500|5B4]ST11NW[7|9]", "M5B4ST11NW79", false},
{"M[500|5B4]ST11NW[7|9]", "M500ST11NWA", false},
{"M[500|5B4]ST11NW[7|9]", "M5B40ST11NW9", false},
{"M[500-5B4]ST11NW[7|9]", "M500ST11NW7", true},
{"M[500-5B4]ST11NW[7|9]", "M5B4ST11NW7", true},
{"M[500-5B4]ST11NW[7|9]", "M500ST11NW9", true},
{"M[500-5B4]ST11NW[7|9]", "M5B4ST11NW9", true},
{"M[500-5B4]ST11NW[7|9]", "M5ST11NW7", false},
{"M[500-5B4]ST11NW[7|9]", "M5B4ST11NW79", false},
{"M[500-5B4]ST11NW[7|9]", "M500ST11NWA", false},
{"M[500-5B4]ST11NW[7|9]", "M5B40ST11NW9", false},
{"M[500|5B4]ST11NW[7-9]", "M500ST11NW7", true},
{"M[500|5B4]ST11NW[7-9]", "M5B4ST11NW7", true},
{"M[500|5B4]ST11NW[7-9]", "M500ST11NW9", true},
{"M[500|5B4]ST11NW[7-9]", "M5B4ST11NW9", true},
{"M[500|5B4]ST11NW[7-9]", "M5ST11NW7", false},
{"M[500|5B4]ST11NW[7-9]", "M5B4ST11NW79", false},
{"M[500|5B4]ST11NW[7-9]", "M500ST11NWA", false},
{"M[500|5B4]ST11NW[7-9]", "M5B40ST11NW9", false},
{"", "", true}
};