From 611cf8441a7bd6bf1740914103198967ddab26bb Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Sun, 30 Oct 2022 17:01:17 -0400 Subject: Fixing up more edge cases for retrieving captures --- match.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/match.c b/match.c index 8d02109..9e0a0ec 100644 --- a/match.c +++ b/match.c @@ -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 -- cgit v1.2.3