# HG changeset patch # User Matti Hamalainen # Date 1579583778 -7200 # Node ID d191ded8a7900c341a797456d23ca59cb70a852b # Parent c09b068ecb3277053f002ded936956e68f900c17 Improve the experimental regex matching debugging macros. diff -r c09b068ecb32 -r d191ded8a790 Makefile.gen --- a/Makefile.gen Tue Jan 21 05:00:53 2020 +0200 +++ b/Makefile.gen Tue Jan 21 07:16:18 2020 +0200 @@ -9,6 +9,7 @@ CFLAGS += -DTH_PRINTF_DEBUG=1 CFLAGS += -DTH_USE_OPT_ARG=1 #CFLAGS += -DTH_EXPERIMENTAL_REGEX=1 +#CFLAGS += -DTH_EXPERIMENTAL_REGEX_DEBUG=1 THLIBS = ./ diff -r c09b068ecb32 -r d191ded8a790 tests.c --- a/tests.c Tue Jan 21 05:00:53 2020 +0200 +++ b/tests.c Tue Jan 21 07:16:18 2020 +0200 @@ -881,6 +881,10 @@ th_regex_ctx *reg = NULL; int res; +#ifdef TH_EXPERIMENTAL_REGEX_DEBUG + th_dbg_re_flags = TH_DBG_RE_MATCH; +#endif + #if 0 res = th_regex_compile(®, "z*k+abba fabboa? k{4} [gz]{1,2} foo(bar|zoo)?"); if (res != THERR_OK) @@ -933,7 +937,7 @@ { // { "zoobar" , 1, 0 }, { "zoo lol bar" , 1, 0 }, -// { "hoho zoo lol lol bar bar" , 1, 0 }, + { "hoho zoo lol lol bar bar" , 1, 0 }, { NULL , 0, 0 } }; diff -r c09b068ecb32 -r d191ded8a790 th_regex.c --- a/th_regex.c Tue Jan 21 05:00:53 2020 +0200 +++ b/th_regex.c Tue Jan 21 07:16:18 2020 +0200 @@ -9,25 +9,17 @@ #include "th_regex.h" -//#define DBG_RE_COMPILE 1 -//#define DBG_RE_FREE 1 -#define DBG_RE_MATCH 1 - -#if defined(DBG_RE_COMPILE) -# define DBG_RE_PRINT_COMPILE(...) do { fprintf(stderr, __VA_ARGS__); } while (0) +#ifdef TH_EXPERIMENTAL_REGEX_DEBUG +int th_dbg_re_flags = 0; +# define DBG_RE_COMPILE(...) do { if (th_dbg_re_flags & TH_DBG_RE_COMPILE) fprintf(stderr, __VA_ARGS__); } while (0) +# define DBG_RE_FREE(...) do { if (th_dbg_re_flags & TH_DBG_RE_FREE) fprintf(stderr, __VA_ARGS__); } while (0) +# define DBG_RE_MATCH(...) do { if (th_dbg_re_flags & TH_DBG_RE_MATCH) fprintf(stderr, __VA_ARGS__); } while (0) #else -# define DBG_RE_PRINT_COMPILE(...) do { } while (0) +# define DBG_RE_COMPILE(...) +# define DBG_RE_FREE(...) +# define DBG_RE_MATCH(...) #endif -#if defined(DBG_RE_FREE) -# define DBG_RE_PRINT_FREE(...) do { fprintf(stderr, __VA_ARGS__); } while (0) -#else -# define DBG_RE_PRINT_FREE(...) do { } while (0) -#endif -#if defined(DBG_RE_MATCH) -# define DBG_RE_PRINT_MATCH(...) do { fprintf(stderr, __VA_ARGS__); } while (0) -#else -# define DBG_RE_PRINT_MATCH(...) do { } while (0) -#endif + #ifdef TH_EXPERIMENTAL_REGEX @@ -51,7 +43,7 @@ }; -#if defined(DBG_RE_COMPILE) || defined(DBG_RE_FREE) || defined(DBG_RE_MATCH) +#ifdef TH_EXPERIMENTAL_REGEX_DEBUG static const char *re_match_modes[] = { "ONCE", @@ -178,7 +170,7 @@ memcpy(&data->nodes[data->nnodes], node, sizeof(th_regex_node)); data->nnodes++; - DBG_RE_PRINT_COMPILE( + DBG_RE_COMPILE( "node [%" PRIu_SIZE_T " / %" PRIu_SIZE_T "]: " "mode=%d, type=%d, rmin=%" PRId_SSIZE_T ", rmax=%" PRId_SSIZE_T "\n", data->nnodes, data->nodessize, @@ -255,7 +247,7 @@ for (; ctx.pattern[ctx.offs] != 0; ctx.offs++) { th_regex_char cch = ctx.pattern[ctx.offs]; - DBG_RE_PRINT_COMPILE("[%" PRIu_SIZE_T "] '%c'\n", ctx.offs, cch); + DBG_RE_COMPILE("[%" PRIu_SIZE_T "] '%c'\n", ctx.offs, cch); switch (cch) { case '?': @@ -461,7 +453,7 @@ { th_regex_node *node = &expr->nodes[n]; - DBG_RE_PRINT_FREE( + DBG_RE_FREE( "node [%" PRIu_SIZE_T " / %" PRIu_SIZE_T "]: " "mode=%d, type=%d, rmin=%" PRId_SSIZE_T ", rmax=%" PRId_SSIZE_T "\n", n, expr->nnodes, @@ -471,23 +463,23 @@ switch (node->type) { case TH_RE_TYPE_SUBEXPR: - DBG_RE_PRINT_FREE(" SUBEXPR: %p vs %p\n", expr, node->match.expr); + DBG_RE_FREE(" SUBEXPR: %p vs %p\n", (void *) expr, (void *) node->match.expr); th_regex_free(node->match.expr); break; case TH_RE_TYPE_LIST: case TH_RE_TYPE_LIST_REVERSE: - DBG_RE_PRINT_FREE(" list='%s'\n", node->match.list.chars); + DBG_RE_FREE(" list='%s'\n", node->match.list.chars); th_free(node->match.list.chars); break; -#ifdef DBG_RE_FREE +#ifdef TH_EXPERIMENTAL_REGEX_DEBUG case TH_RE_TYPE_ANY_CHAR: - DBG_RE_PRINT_FREE(" any char\n"); + DBG_RE_FREE(" any char\n"); break; case TH_RE_TYPE_CHAR: - DBG_RE_PRINT_FREE(" char='%c'\n", node->match.chr); + DBG_RE_FREE(" char='%c'\n", node->match.chr); break; #endif } @@ -508,18 +500,18 @@ th_regex_char cch; BOOL ret = FALSE; - DBG_RE_PRINT_MATCH(" node_START [%s]: '%s': ", re_match_types[node->type], haystack + *offs); + DBG_RE_MATCH(" node_START [%s]: '%s': ", re_match_types[node->type], haystack + *offs); switch (node->type) { case TH_RE_TYPE_SUBEXPR: - DBG_RE_PRINT_MATCH("subexpr ..\n"); + DBG_RE_MATCH("subexpr ..\n"); ret = th_regex_do_match_expr(node->match.expr, haystack, offs, flags); break; case TH_RE_TYPE_LIST: case TH_RE_TYPE_LIST_REVERSE: - DBG_RE_PRINT_MATCH("[%s]\n", node->match.list.chars); + DBG_RE_MATCH("[%s]\n", node->match.list.chars); ret = FALSE; if ((cch = haystack[*offs]) == 0) goto out; @@ -540,7 +532,7 @@ break; case TH_RE_TYPE_ANY_CHAR: - DBG_RE_PRINT_MATCH("\n"); + DBG_RE_MATCH("\n"); if ((cch = haystack[*offs]) == 0) { ret = FALSE; @@ -552,7 +544,7 @@ break; case TH_RE_TYPE_CHAR: - DBG_RE_PRINT_MATCH("'%c'\n", node->match.chr); + DBG_RE_MATCH("'%c'\n", node->match.chr); if ((cch = haystack[*offs]) == 0) { ret = FALSE; @@ -565,7 +557,7 @@ } out: - DBG_RE_PRINT_MATCH(" node_DONE [%s]: match %s\n", re_match_types[node->type], ret ? "YES" : "NO"); + DBG_RE_MATCH(" node_DONE [%s]: match %s\n", re_match_types[node->type], ret ? "YES" : "NO"); return ret; } @@ -578,14 +570,14 @@ const th_regex_node *node = &expr->nodes[n]; size_t soffs; - DBG_RE_PRINT_MATCH(" expr [%" PRIu_SIZE_T "/%" PRIu_SIZE_T "]: %s ", + DBG_RE_MATCH(" expr [%" PRIu_SIZE_T "/%" PRIu_SIZE_T "]: %s ", n, expr->nnodes, re_match_modes[node->mode]); switch (node->mode) { case TH_RE_MATCH_ONCE: { - DBG_RE_PRINT_MATCH("\n"); + DBG_RE_MATCH("\n"); soffs = *offs; if (!th_regex_do_match_node(haystack, &soffs, node, flags)) return FALSE; @@ -599,7 +591,7 @@ BOOL done = FALSE; ssize_t count = 0; - DBG_RE_PRINT_MATCH("min=%" PRId_SSIZE_T ", max=%" PRId_SSIZE_T "\n", node->repeatMin, node->repeatMax); + DBG_RE_MATCH("min=%" PRId_SSIZE_T ", max=%" PRId_SSIZE_T "\n", node->repeatMin, node->repeatMax); do { @@ -631,7 +623,7 @@ } } while (!done); - DBG_RE_PRINT_MATCH("RESULT: %" PRId_SSIZE_T " = %s\n", count, done ? "YES" : "NO"); + DBG_RE_MATCH("RESULT: %" PRId_SSIZE_T " = %s\n", count, done ? "YES" : "NO"); if (!done) return FALSE; @@ -641,13 +633,13 @@ break; case TH_RE_MATCH_ANCHOR_START: - DBG_RE_PRINT_MATCH("offs=%" PRIu_SIZE_T "\n", *offs); + DBG_RE_MATCH("offs=%" PRIu_SIZE_T "\n", *offs); if (*offs != 0) return FALSE; break; case TH_RE_MATCH_ANCHOR_END: - DBG_RE_PRINT_MATCH("is end=%d\n", haystack[*offs]); + DBG_RE_MATCH("is end=%d\n", haystack[*offs]); if (haystack[*offs] != 0) return FALSE; break; @@ -681,7 +673,7 @@ BOOL matched; size_t coffs = soffs; - DBG_RE_PRINT_MATCH("\nTRY_MATCH @ startoffs=%" PRIu_SIZE_T ": '%s'\n", + DBG_RE_MATCH("\nTRY_MATCH @ startoffs=%" PRIu_SIZE_T ": '%s'\n", soffs, haystack + soffs); if ((matched = th_regex_do_match_expr(expr, haystack, &coffs, flags))) diff -r c09b068ecb32 -r d191ded8a790 th_regex.h --- a/th_regex.h Tue Jan 21 05:00:53 2020 +0200 +++ b/th_regex.h Tue Jan 21 07:16:18 2020 +0200 @@ -68,6 +68,18 @@ } th_regex_match_node; +#ifdef TH_EXPERIMENTAL_REGEX_DEBUG +enum +{ + TH_DBG_RE_COMPILE = 0x0001, + TH_DBG_RE_FREE = 0x0002, + TH_DBG_RE_MATCH = 0x0004, +}; + +extern int th_dbg_re_flags; +#endif + + // // Functions //