Fixing up more edge cases for retrieving captures

This commit is contained in:
Bruce Hill 2022-10-30 17:01:17 -04:00
parent b306f72050
commit 611cf8441a

33
match.c
View File

@ -925,7 +925,7 @@ match_t *get_numbered_capture(match_t *m, int n)
{
if (n <= 0) return m;
if (m->pat->type == BP_TAGGED || m->pat->type == BP_CAPTURE) {
if (n == 1) return m;
if (n == 1 && m->pat->type == BP_CAPTURE && m->pat->args.capture.namelen == 0) return m;
if (m->children) {
for (int i = 0; m->children[i]; i++) {
match_t *cap = _get_numbered_capture(m->children[i], &n);
@ -939,22 +939,45 @@ match_t *get_numbered_capture(match_t *m, int n)
}
//
// Get a capture with a specific name.
// Helper function for get_named_capture()
//
match_t *get_named_capture(match_t *m, const char *name, ssize_t _namelen)
match_t *_get_named_capture(match_t *m, const char *name, size_t namelen)
{
size_t namelen = _namelen < 0 ? strlen(name) : (size_t)_namelen;
if (m->pat->type == BP_CAPTURE && m->pat->args.capture.name
&& m->pat->args.capture.namelen == namelen
&& strncmp(m->pat->args.capture.name, name, m->pat->args.capture.namelen) == 0)
return m;
if (m->pat->type == BP_TAGGED || m->pat->type == BP_CAPTURE)
return NULL;
if (m->children) {
for (int i = 0; m->children[i]; i++) {
match_t *cap = get_named_capture(m->children[i], name, namelen);
match_t *cap = _get_named_capture(m->children[i], name, namelen);
if (cap) return cap;
}
}
return NULL;
}
//
// Get a capture with a specific name.
//
match_t *get_named_capture(match_t *m, const char *name, ssize_t _namelen)
{
size_t namelen = _namelen < 0 ? strlen(name) : (size_t)_namelen;
if (m->pat->type == BP_TAGGED) {// || (m->pat->type == BP_CAPTURE && m->pat->args.capture.namelen > 0)) {
if (m->children) {
for (int i = 0; m->children[i]; i++) {
match_t *cap = _get_named_capture(m->children[i], name, namelen);
if (cap) return cap;
}
}
return NULL;
} else {
return _get_named_capture(m, name, namelen);
}
return NULL;
}
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0