From a399b382fa72cd6ca97693164a0662a135d1aa1a Mon Sep 17 00:00:00 2001 From: John Kerl Date: Mon, 5 Oct 2015 22:34:46 -0400 Subject: [PATCH] regex iterate --- c/mapping/mlr_val.c | 40 ++++++++++++++++++++++++++++++++++++++-- c/todo.txt | 2 +- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/c/mapping/mlr_val.c b/c/mapping/mlr_val.c index db3696526..a9e622c98 100644 --- a/c/mapping/mlr_val.c +++ b/c/mapping/mlr_val.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "lib/mlr_globals.h" #include "lib/mlrutil.h" #include "mapping/mlr_val.h" @@ -847,8 +848,43 @@ mv_t lt_op_func(mv_t* pval1, mv_t* pval2) { return (lt_dispositions[pval1->type] mv_t le_op_func(mv_t* pval1, mv_t* pval2) { return (le_dispositions[pval1->type][pval2->type])(pval1, pval2); } mv_t matches_op_func(mv_t* pval1, mv_t* pval2) { - return (mv_t) {.type = MT_BOOL, .u.boolv = TRUE}; // xxx stub + char* s1 = pval1->u.strv; + char* s2 = pval2->u.strv; + + // xxx temp -- need to regcomp once & keep it + regex_t reg; + char* sstr = s1; + char* sregex = s2; + int cflags = REG_EXTENDED; // also support REG_ICASE somehow + regmatch_t pmatch[1]; + int eflags = 0; + int rc; + + rc = regcomp(®, sregex, cflags); + if (rc != 0) { + size_t nbytes = regerror(rc, ®, NULL, 0); + char* errbuf = malloc(nbytes); + (void)regerror(rc, ®, errbuf, nbytes); + fprintf(stderr, "regcomp failure: %s\n", errbuf); + exit(1); + } + + rc = regexec(®, sstr, 1, pmatch, eflags); + regfree(®); + if (rc == 0) { + return (mv_t) {.type = MT_BOOL, .u.boolv = TRUE}; + } else if (rc == REG_NOMATCH) { + return (mv_t) {.type = MT_BOOL, .u.boolv = FALSE}; + } else { + size_t nbytes = regerror(rc, ®, NULL, 0); + char* errbuf = malloc(nbytes); + (void)regerror(rc, ®, errbuf, nbytes); + printf("regexec failure: %s\n", errbuf); + exit(1); + } } mv_t does_not_match_op_func(mv_t* pval1, mv_t* pval2) { - return (mv_t) {.type = MT_BOOL, .u.boolv = FALSE}; // xxx stub + mv_t rv = matches_op_func(pval1, pval2); + rv.u.boolv = !rv.u.boolv; + return rv; } diff --git a/c/todo.txt b/c/todo.txt index 06808a455..be3d316e3 100644 --- a/c/todo.txt +++ b/c/todo.txt @@ -19,7 +19,7 @@ MAJOR: running-mean feature ....... really for all of stats1/stats2 ---------------------------------------------------------------- MAJOR: regex -* mlr filter '$y =~ /abc/' +* mlr filter '$y =~ "abc"' * mlr put '$y = gsub(/a.*b/, "cde")' * having-fields * IOXS...