changeset 651:18fe45e61b2b

Moar re-work.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 25 Jan 2020 13:13:37 +0200
parents 24cbab6e88c6
children 38a9302962f7
files tests.c th_regex.c th_regex.h
diffstat 3 files changed, 47 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Sat Jan 25 13:05:11 2020 +0200
+++ b/tests.c	Sat Jan 25 13:13:37 2020 +0200
@@ -3,6 +3,7 @@
 #include "th_util.h"
 #include "th_string.h"
 #include "th_crypto.h"
+#include "th_ioctx.h"
 #include "th_config.h"
 #include "th_regex.h"
 
@@ -49,6 +50,8 @@
 
 int optFlags = TST_ALL;
 
+th_ioctx testio;
+
 
 // Define option arguments
 static const th_optarg arg_opts[] =
@@ -610,7 +613,7 @@
     }
 
     if (th_verbosity > 0)
-        th_regex_dump(stdout, 1, expr);
+        th_regex_dump(&testio, 1, expr);
 
     for (const test_regex_def1 *def = list; def->str != NULL; def++)
     {
@@ -660,7 +663,7 @@
         }
 
         if (th_verbosity > 0)
-            th_regex_dump(stdout, 1, expr);
+            th_regex_dump(&testio, 1, expr);
 
         printf("----------------------------------------\n");
 
@@ -711,6 +714,8 @@
     for (i1 = 0; i1 < SET_MAX_TESTS; i1++)
         sets_enabled[i1] = 1;
 
+    th_io_init_stdio(&testio, stdout);
+
     //
     // Parse command line arguments
     //
@@ -944,8 +949,10 @@
 #ifdef TH_EXPERIMENTAL_REGEX
     if (test_set_start("Regular expressions"))
     {
+
 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
-        th_dbg_re_flags = th_verbosity > 0;
+        if (th_verbosity > 0)
+            th_dbg_fh = &testio;
 #endif
 
         {
@@ -954,7 +961,7 @@
             int res = th_regex_compile(&expr, str);
             printf("REGEX: \"%s\"\n", str);
             if (res == THERR_OK)
-                th_regex_dump(stdout, 1, expr);
+                th_regex_dump(&testio, 1, expr);
             else
                 printf("ERROR: %s\n", th_error_str(res));
             th_regex_free(expr);
@@ -1004,9 +1011,10 @@
         {
             static const test_regex_def1 tlist[] =
             {
-//                { "zoobar"                       , 1, 0 },
-                { "zoo lol bar"                  , 1, 0 },
+                { "zoobar"                       , 1, 0 },
+                { "hehzoo lol baromg"            , 1, 0 },
                 { "hoho zoo lol lol bar bar"     , 1, 0 },
+                { "hoho zoobar bar"              , 1, 0 },
                 { NULL                           , 0, 0 }
             };
 
--- a/th_regex.c	Sat Jan 25 13:05:11 2020 +0200
+++ b/th_regex.c	Sat Jan 25 13:13:37 2020 +0200
@@ -10,17 +10,17 @@
 
 
 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
-BOOL th_dbg_re_flags = FALSE;
+th_ioctx *th_dbg_fh = NULL;
 
-#    define DBG_RE_MATCH(...) do { \
-        if (th_dbg_re_flags) \
+#    define DBG_RE_PRINT(...) do { \
+        if (th_dbg_fh != NULL) \
         { \
-            th_regex_dump_indent(stdout, level); \
-            fprintf(stdout, __VA_ARGS__); \
+            th_regex_dump_indent(th_dbg_fh, level); \
+            thfprintf(th_dbg_fh, __VA_ARGS__); \
         } \
     } while (0)
 #else
-#    define DBG_RE_MATCH(...)
+#    define DBG_RE_PRINT(...)
 #endif
 
 
@@ -723,63 +723,63 @@
 }
 
 
-static void th_regex_dump_indent(FILE *fh, const int level)
+static void th_regex_dump_indent(th_ioctx *fh, const int level)
 {
     for (int indent = 0; indent < level; indent++)
-        fprintf(fh, "    ");
+        thfputs("    ", fh);
 }
 
 
-static void th_regex_dump_node(FILE *fh, const th_regex_node_t *node)
+static void th_regex_dump_node(th_ioctx *fh, const th_regex_node_t *node)
 {
-    fprintf(fh,
+    thfprintf(fh,
         "%s %s ",
         re_match_modes[node->mode],
         re_match_types[node->type]);
 
     if (node->mode == TH_RE_MATCH_COUNT)
     {
-        fprintf(fh, "min=%" PRId_SSIZE_T ", max=%" PRId_SSIZE_T " : ",
+        thfprintf(fh, "min=%" PRId_SSIZE_T ", max=%" PRId_SSIZE_T " : ",
             node->repeatMin, node->repeatMax);
     }
 
     switch (node->type)
     {
         case TH_RE_TYPE_CHAR:
-            fprintf(fh, "'%c'", node->match.chr);
+            thfprintf(fh, "'%c'", node->match.chr);
             break;
 
         case TH_RE_TYPE_STR:
-            fprintf(fh, "\"%s\"", node->match.str);
+            thfprintf(fh, "\"%s\"", node->match.str);
             break;
 
         case TH_RE_TYPE_ANY_CHAR:
-            fprintf(fh, ".");
+            thfprintf(fh, ".");
             break;
 
         case TH_RE_TYPE_LIST:
         case TH_RE_TYPE_LIST_REVERSE:
-            fprintf(fh, "[ ");
+            thfputs("[ ", fh);
             for (size_t n = 0; n < node->match.list.nitems; n++)
             {
                 const th_regex_list_item_t *li = &node->match.list.items[n];
                 if (li->type)
                 {
-                    fprintf(fh, "'%c-%c' ", li->start, li->end);
+                    thfprintf(fh, "'%c-%c' ", li->start, li->end);
                 }
                 else
                 {
                     for (size_t i = 0; i < li->nchars; i++)
-                        fprintf(fh, "'%c' ", li->chars[i]);
+                        thfprintf(fh, "'%c' ", li->chars[i]);
                 }
             }
-            fprintf(fh, "]");
+            thfputs("]", fh);
             break;
     }
 }
 
 
-void th_regex_dump(FILE *fh, const int level, const th_regex_t *expr)
+void th_regex_dump(th_ioctx *fh, const int level, const th_regex_t *expr)
 {
     if (expr != NULL)
     {
@@ -789,12 +789,12 @@
 
             th_regex_dump_indent(fh, level);
 
-            fprintf(fh,
+            thfprintf(fh,
                 "[%" PRIu_SIZE_T "/%" PRIu_SIZE_T "] ",
                 nnode + 1, expr->nnodes);
 
             th_regex_dump_node(fh, node);
-            fprintf(fh, "\n");
+            thfputs("\n", fh);
 
             if (node->type == TH_RE_TYPE_SUBEXPR)
                 th_regex_dump(fh, level + 1, node->match.expr);
@@ -943,12 +943,12 @@
 
     if (count > 0 || node->repeatMin == 0)
     {
-        DBG_RE_MATCH("count=%" PRId_SSIZE_T " \"%s\"\n",
+        DBG_RE_PRINT("count=%" PRId_SSIZE_T " \"%s\"\n",
             count, haystack + toffs);
 
         match = th_regex_match_expr(haystack, &toffs, expr, *nnode + 1, flags, level + 1);
 
-        DBG_RE_MATCH("rest expr match=%s \"%s\"\n",
+        DBG_RE_PRINT("rest expr match=%s \"%s\"\n",
             match ? "YES" : "NO", haystack + toffs);
     }
 
@@ -964,7 +964,7 @@
         (node->repeatMin >= 0 && count >= node->repeatMin)
         );
 
-    DBG_RE_MATCH("RESULT: match=%s, res=%s\n",
+    DBG_RE_PRINT("RESULT: match=%s, res=%s\n",
         match ? "YES" : "NO", res ? "YES" : "NO");
 
     return res;
@@ -988,16 +988,17 @@
         const th_regex_node_t *node = &expr->nodes[nnode];
 
 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
-        if (th_dbg_re_flags)
+        if (th_dbg_fh != NULL)
         {
-            th_regex_dump_indent(stdout, level);
-            fprintf(stdout,
+            th_regex_dump_indent(th_dbg_fh, level);
+
+            thfprintf(th_dbg_fh,
                 "[%" PRIu_SIZE_T "/%" PRIu_SIZE_T "] ",
                 nnode + 1, expr->nnodes);
 
-            th_regex_dump_node(stdout, node);
+            th_regex_dump_node(th_dbg_fh, node);
 
-            fprintf(stdout, " <-> \"%s\"\n",
+            thfprintf(th_dbg_fh, " <-> \"%s\"\n",
                 haystack + soffs);
         }
 #endif
--- a/th_regex.h	Sat Jan 25 13:05:11 2020 +0200
+++ b/th_regex.h	Sat Jan 25 13:13:37 2020 +0200
@@ -12,6 +12,7 @@
 
 #include "th_types.h"
 #include "th_datastruct.h"
+#include "th_ioctx.h"
 
 
 #ifdef __cplusplus
@@ -50,7 +51,7 @@
 
 
 #ifdef TH_EXPERIMENTAL_REGEX_DEBUG
-extern BOOL th_dbg_re_flags;
+extern th_ioctx *th_dbg_fh;
 #endif
 
 
@@ -59,7 +60,7 @@
 //
 int      th_regex_compile(th_regex_t **pexpr, const th_regex_char_t *pattern);
 void     th_regex_free(th_regex_t *expr);
-void     th_regex_dump(FILE *fh, const int level, const th_regex_t *expr);
+void     th_regex_dump(th_ioctx *fh, const int level, const th_regex_t *expr);
 
 int      th_regex_match(const th_regex_t *expr, const th_regex_char_t *haystack,
          size_t *pnmatches, th_regex_match_t **pmatches, const size_t maxmatches,