Mercurial > hg > th-libs
comparison 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 |
comparison
equal
deleted
inserted
replaced
609:69f1cb7f9b38 | 610:a0e8d9c6300b |
---|---|
634 return TRUE; | 634 return TRUE; |
635 } | 635 } |
636 | 636 |
637 | 637 |
638 int th_regex_match(const th_regex_ctx *expr, const th_regex_char *haystack, | 638 int th_regex_match(const th_regex_ctx *expr, const th_regex_char *haystack, |
639 BOOL *pmatched, th_regex_match_node **pmatches, const ssize_t max, | 639 size_t *pnmatches, th_regex_match_node **pmatches, const size_t maxmatches, |
640 const int flags) | 640 const int flags) |
641 { | 641 { |
642 // th_regex_match_node *matches = NULL; | 642 size_t nmatches = 0; |
643 BOOL matched; | 643 |
644 (void) pmatches; | 644 if (pnmatches != NULL) |
645 (void) max; | 645 *pnmatches = 0; |
646 | 646 |
647 // Check given pattern and string | 647 // Check given pattern and string |
648 if (expr == NULL || haystack == NULL) | 648 if (expr == NULL || haystack == NULL) |
649 return THERR_NULLPTR; | 649 return THERR_NULLPTR; |
650 | 650 |
651 // Start matching | 651 // Start matching |
652 #if 0 | 652 // XXX NOTE .. lots to think about and to take into account: |
653 size_t soffs, coffs; | 653 // - anchored and unanchored expressions |
654 soffs = coffs = 0; | 654 // - how to check if the expression has consumed all possibilities? |
655 while (haystack[soffs] != 0) | 655 // .. |
656 { | 656 for (size_t soffs = 0; haystack[soffs] != 0; ) |
657 { | |
658 BOOL matched; | |
659 size_t coffs = soffs; | |
660 | |
657 if ((matched = th_regex_do_match_expr(expr, haystack, &coffs, flags))) | 661 if ((matched = th_regex_do_match_expr(expr, haystack, &coffs, flags))) |
658 { | 662 { |
663 nmatches++; | |
664 | |
665 if (pnmatches != NULL) | |
666 *pnmatches = nmatches; | |
667 | |
668 if (pmatches != NULL) | |
669 { | |
670 th_regex_match_node *match = th_malloc0(sizeof(th_regex_match_node)); | |
671 if (match == NULL) | |
672 return THERR_MALLOC; | |
673 | |
674 match->start = soffs; | |
675 match->len = coffs - soffs; | |
676 | |
677 th_llist_append_node((th_llist_t **) pmatches, (th_llist_t *) match); | |
678 } | |
679 | |
680 if (maxmatches > 0 && nmatches >= maxmatches) | |
681 break; | |
682 | |
683 if (soffs == coffs) | |
684 soffs++; | |
685 else | |
686 soffs = coffs; | |
659 } | 687 } |
660 else | 688 else |
661 { | 689 { |
690 soffs++; | |
662 } | 691 } |
663 } | 692 } |
664 #else | |
665 size_t offs = 0; | |
666 matched = th_regex_do_match_expr(expr, haystack, &offs, flags); | |
667 #endif | |
668 | |
669 if (pmatched != NULL) | |
670 *pmatched = matched; | |
671 | 693 |
672 return THERR_OK; | 694 return THERR_OK; |
673 } | 695 } |
674 | 696 |
697 | |
698 static void th_regex_free_match(th_regex_match_node *node) | |
699 { | |
700 (void) node; | |
701 // Nothing to do here at the moment | |
702 } | |
703 | |
704 | |
705 void th_regex_free_matches(th_regex_match_node *matches) | |
706 { | |
707 th_llist_free_func_node((th_llist_t *) matches, | |
708 (void (*)(th_llist_t *)) th_regex_free_match); | |
709 } | |
710 | |
675 #endif // TH_EXPERIMENTAL_REGEX | 711 #endif // TH_EXPERIMENTAL_REGEX |