changeset 648:91c43398c6fc

Twiddle.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 25 Jan 2020 08:01:17 +0200
parents 1e7e3f96632e
children 2c9260f5cf44
files th_regex.c
diffstat 1 files changed, 49 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/th_regex.c	Sat Jan 25 06:47:41 2020 +0200
+++ b/th_regex.c	Sat Jan 25 08:01:17 2020 +0200
@@ -566,6 +566,15 @@
                 }
                 break;
 
+/*
+            case '|':
+                if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
+                    goto out;
+
+                // Alt pattern .. how to handle these?
+                break;
+*/
+
             case '(':
                 if ((res = th_regex_parse_ctx_node_commit_strchr(&ctx, FALSE)) != THERR_OK)
                     goto out;
@@ -882,15 +891,12 @@
 
         case TH_RE_TYPE_STR:
             res = TRUE;
-            for (th_regex_char_t *str = node->match.str; *str != 0; )
+            for (th_regex_char_t *str = node->match.str;
+                res && *str != 0;
+                str++, (*poffs)++)
             {
                 if (haystack[*poffs] != *str)
-                {
                     res = FALSE;
-                    break;
-                }
-                str++;
-                (*poffs)++;
             }
             break;
     }
@@ -916,15 +922,18 @@
         const th_regex_node_t *node = &expr->nodes[nnode];
 
 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
-        th_regex_dump_indent(stdout, level);
-        fprintf(stdout,
-            "[%" PRIu_SIZE_T "/%" PRIu_SIZE_T "] ",
-            nnode + 1, expr->nnodes);
+        if (th_dbg_re_flags)
+        {
+            th_regex_dump_indent(stdout, level);
+            fprintf(stdout,
+                "[%" PRIu_SIZE_T "/%" PRIu_SIZE_T "] ",
+                nnode + 1, expr->nnodes);
 
-        th_regex_dump_node(stdout, node);
+            th_regex_dump_node(stdout, node);
 
-        fprintf(stdout, " <-> \"%s\"\n",
-            haystack + soffs);
+            fprintf(stdout, " <-> \"%s\"\n",
+                haystack + soffs);
+        }
 #endif
 
         switch (node->mode)
@@ -935,55 +944,58 @@
 
             case TH_RE_MATCH_COUNT:
                 {
-                    ssize_t count = 0;
+                    ssize_t count = 0, ncount = 0;
 
                     do
                     {
                         BOOL match;
-                        size_t toffs = soffs, tnode;
+                        size_t toffs = soffs, noffs;
 
                         DBG_RE_MATCH("    ROUND #%" PRIu_SIZE_T ": '%s'\n",
                             count, haystack + toffs);
 
+                        res = FALSE;
+                        match = TRUE;
                         do
                         {
-                            match = TRUE;
-                            size_t noffs = toffs;
-                            for (tnode = nnode; match && tnode < expr->nnodes && haystack[toffs] != 0; )
+                            noffs = toffs;
+                            match = th_regex_match_one(haystack, &toffs, node, flags, level + 1);
+                            if (match)
                             {
-                                match = th_regex_match_one(haystack, &toffs, &expr->nodes[tnode], flags, level + 1);
-                                if (match)
-                                    tnode++;
+                                ncount++;
+
+                                if (node->repeatMin >= 0 && ncount >= node->repeatMin)
+                                    break;
                             }
+                        } while (match && haystack[toffs] != 0 && toffs > noffs);
 
-                            DBG_RE_MATCH("        '%s': %d (tnode=%" PRIu_SIZE_T ")\n",
-                                haystack + noffs, match, tnode);
+                        DBG_RE_MATCH("    ROUND #%" PRIu_SIZE_T " END: match=%s \"%s\"\n",
+                            ncount, match ? "YES" : "NO", haystack + toffs);
 
-                            if (node->repeatMin >= 0 && match)
-                                break;
-                        } while (!match && haystack[toffs] != 0);
+                        if (ncount > 0)
+                        {
+                            match = th_regex_match_expr(haystack, &toffs, expr, nnode + 1, flags, level + 2);
+                        }
 
-                        DBG_RE_MATCH("    ROUND #%" PRIu_SIZE_T " END: match=%s, tnode=%" PRIu_SIZE_T "\n",
-                            count, match ? "YES" : "NO", tnode);
+                        DBG_RE_MATCH("    ROUND #%" PRIu_SIZE_T " END: match=%s \"%s\"\n",
+                            count, match ? "YES" : "NO", haystack + toffs);
 
                         if (match)
                         {
                             // Node matched
                             count++;
                             soffs = toffs;
-                            nnode = tnode;
-                            res = (node->repeatMax > 0 && count >= node->repeatMax);
-                        }
-                        else
-                        {
-                            // Node did not match, check if we got the minimum if set
-                            res = (node->repeatMin >= 0 && count >= node->repeatMin);
+                            nnode = expr->nnodes;
+
+                            res =
+                                (node->repeatMax > 0 && ncount >= node->repeatMax) ||
+                                (node->repeatMin >= 0 && ncount >= node->repeatMin);
                         }
 
                     } while (!res);
 
-                    DBG_RE_MATCH("    RESULT: count=%" PRId_SSIZE_T ", done=%s\n",
-                        count, res ? "YES" : "NO");
+                    DBG_RE_MATCH("    RESULT: count=%" PRId_SSIZE_T ", ncount=%" PRId_SSIZE_T ", done=%s\n",
+                        count, ncount, res ? "YES" : "NO");
                 }
                 break;