Mercurial > hg > th-libs
diff th_regex.c @ 610:a0e8d9c6300b
A bit more work on the regex stuff.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 16 Jan 2020 03:33:11 +0200 |
parents | 69f1cb7f9b38 |
children | d895b0fd6ad6 |
line wrap: on
line diff
--- a/th_regex.c Thu Jan 16 01:46:19 2020 +0200 +++ b/th_regex.c Thu Jan 16 03:33:11 2020 +0200 @@ -636,40 +636,76 @@ int th_regex_match(const th_regex_ctx *expr, const th_regex_char *haystack, - BOOL *pmatched, th_regex_match_node **pmatches, const ssize_t max, + size_t *pnmatches, th_regex_match_node **pmatches, const size_t maxmatches, const int flags) { -// th_regex_match_node *matches = NULL; - BOOL matched; - (void) pmatches; - (void) max; + size_t nmatches = 0; + + if (pnmatches != NULL) + *pnmatches = 0; // Check given pattern and string if (expr == NULL || haystack == NULL) return THERR_NULLPTR; // Start matching -#if 0 - size_t soffs, coffs; - soffs = coffs = 0; - while (haystack[soffs] != 0) + // XXX NOTE .. lots to think about and to take into account: + // - anchored and unanchored expressions + // - how to check if the expression has consumed all possibilities? + // .. + for (size_t soffs = 0; haystack[soffs] != 0; ) { + BOOL matched; + size_t coffs = soffs; + if ((matched = th_regex_do_match_expr(expr, haystack, &coffs, flags))) { + nmatches++; + + if (pnmatches != NULL) + *pnmatches = nmatches; + + if (pmatches != NULL) + { + th_regex_match_node *match = th_malloc0(sizeof(th_regex_match_node)); + if (match == NULL) + return THERR_MALLOC; + + match->start = soffs; + match->len = coffs - soffs; + + th_llist_append_node((th_llist_t **) pmatches, (th_llist_t *) match); + } + + if (maxmatches > 0 && nmatches >= maxmatches) + break; + + if (soffs == coffs) + soffs++; + else + soffs = coffs; } else { + soffs++; } } -#else - size_t offs = 0; - matched = th_regex_do_match_expr(expr, haystack, &offs, flags); -#endif - - if (pmatched != NULL) - *pmatched = matched; return THERR_OK; } + +static void th_regex_free_match(th_regex_match_node *node) +{ + (void) node; + // Nothing to do here at the moment +} + + +void th_regex_free_matches(th_regex_match_node *matches) +{ + th_llist_free_func_node((th_llist_t *) matches, + (void (*)(th_llist_t *)) th_regex_free_match); +} + #endif // TH_EXPERIMENTAL_REGEX