diff tests.c @ 645:b897995101b7

More fiddling and twiddling. Add parsing to string nodes instead of separate character nodes.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 25 Jan 2020 05:11:40 +0200
parents a2bf1ea05b05
children 1e7e3f96632e
line wrap: on
line diff
--- a/tests.c	Fri Jan 24 15:10:19 2020 +0200
+++ b/tests.c	Sat Jan 25 05:11:40 2020 +0200
@@ -571,26 +571,44 @@
 } test_regex_def;
 
 
+void test_regex_print_matches(const th_regex_char_t *str, const th_regex_match_t *matches)
+{
+    for (const th_regex_match_t *mt = matches;
+        mt != NULL; mt = (th_regex_match_t *) mt->node.next)
+    {
+        char *tmp = th_strndup(str + mt->start, mt->len);
+
+        printf("    match [%3" PRIu_SIZE_T " ++ %3" PRIu_SIZE_T "]: '%s'\n",
+            mt->start, mt->len, tmp);
+
+        th_free(tmp);
+    }
+}
+
+
 void test_regex_list(const test_regex_def *list, const th_regex_char_t *pattern)
 {
-    th_regex_t *reg = NULL;
+    th_regex_t *expr = NULL;
     int res;
 
     printf("========================================\n");
-    if ((res = th_regex_compile(&reg, pattern)) != THERR_OK)
+    printf("Compiling pattern \"%s\"\n", pattern);
+    if ((res = th_regex_compile(&expr, pattern)) != THERR_OK)
     {
         THERR("Regex compilation failed: %s\n",
             th_error_str(res));
         goto out;
     }
 
+    th_regex_dump(stdout, 1, expr);
+
     for (const test_regex_def *def = list; def->str != NULL; def++)
     {
         th_regex_match_t *matches = NULL;
         size_t nmatches;
 
         printf("----------------------------------------\n");
-        if ((res = th_regex_match(reg, def->str,
+        if ((res = th_regex_match(expr, def->str,
             &nmatches, &matches, -1, def->flags)) != THERR_OK)
         {
             THERR("Regex match returned error: %s\n",
@@ -598,26 +616,17 @@
             goto out;
         }
 
-        printf("\npattern '%s'\n", pattern);
-        printf("  '%s': matched %" PRIu_SIZE_T " time(s), testresult=%s\n",
+        printf("'%s': matched %" PRIu_SIZE_T " time(s), testresult=%s\n",
             def->str,
             nmatches,
             def->nmatches == nmatches ? "YES" : "NO");
 
-        for (th_regex_match_t *m = matches;
-            m != NULL; m = (th_regex_match_t *) m->node.next)
-        {
-            char *tmp = th_strndup(def->str + m->start, m->len);
-            printf("      match [%" PRIu_SIZE_T " ++ %" PRIu_SIZE_T "]: '%s'\n",
-                m->start, m->len, tmp);
-            th_free(tmp);
-        }
-
+        test_regex_print_matches(def->str, matches);
         th_regex_free_matches(matches);
     }
 
 out:
-    th_regex_free(reg);
+    th_regex_free(expr);
 }
 
 #endif
@@ -880,18 +889,23 @@
     if (test_set_start("Regular expressions"))
     {
 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
-        th_dbg_re_flags = th_verbosity > 0 ? TH_DBG_RE_MATCH : 0;
+        th_dbg_re_flags = th_verbosity > 0;
 #endif
 
+        {
+            const char *str = "z*k+abba fabboa? [a-zA-Z_-] \\{\\} k{4} ([0-9]+ yay){1,2} foo(bar|zoo)?";
+            th_regex_t *expr = NULL;
+            int res = th_regex_compile(&expr, str);
+            printf("REGEX: \"%s\"\n", str);
+            if (res == THERR_OK)
+                th_regex_dump(stdout, 1, expr);
+            else
+                printf("ERROR: %s\n", th_error_str(res));
+            th_regex_free(expr);
+        }
+
 #if 0
         {
-            th_regex_t *reg = NULL;
-            int res = th_regex_compile(&reg, "z*k+abba fabboa? k{4} [gz]{1,2} foo(bar|zoo)?");
-            if (res != THERR_OK)
-                printf("result: %s\n", th_error_str(res));
-            th_regex_free(reg);
-        }
-        {
             static const test_regex_def tlist[] =
             {
                 { "abcfoabccg"                   , 1, 0 },