static void tre_build_aparams(regaparams_t* aparams, VALUE params) { // Sets to default tre_regaparams_default(aparams); // Then override _TRE_RUBY_APARAM_OVERRIDE(cost_ins); _TRE_RUBY_APARAM_OVERRIDE(cost_del); _TRE_RUBY_APARAM_OVERRIDE(cost_subst); _TRE_RUBY_APARAM_OVERRIDE(max_cost); _TRE_RUBY_APARAM_OVERRIDE(max_ins); _TRE_RUBY_APARAM_OVERRIDE(max_del); _TRE_RUBY_APARAM_OVERRIDE(max_subst); _TRE_RUBY_APARAM_OVERRIDE(max_err); }
static int tre_match(const tre_tnfa_t *tnfa, const void *string, size_t len, tre_str_type_t type, size_t nmatch, regmatch_t pmatch[], int eflags) { reg_errcode_t status; int *tags = NULL, eo; if (tnfa->num_tags > 0 && nmatch > 0) { #ifdef TRE_USE_ALLOCA tags = alloca(sizeof(*tags) * tnfa->num_tags); #else /* !TRE_USE_ALLOCA */ tags = xmalloc(sizeof(*tags) * tnfa->num_tags); #endif /* !TRE_USE_ALLOCA */ if (tags == NULL) return REG_ESPACE; } /* Dispatch to the appropriate matcher. */ if (tnfa->have_backrefs || eflags & REG_BACKTRACKING_MATCHER) { /* The regex has back references, use the backtracking matcher. */ if (type == STR_USER) { const tre_str_source *source = string; if (source->rewind == NULL || source->compare == NULL) /* The backtracking matcher requires rewind and compare capabilities from the input stream. */ return REG_BADPAT; } status = tre_tnfa_run_backtrack(tnfa, string, (int)len, type, tags, eflags, &eo); } #ifdef TRE_APPROX else if (tnfa->have_approx || eflags & REG_APPROX_MATCHER) { /* The regex uses approximate matching, use the approximate matcher. */ regamatch_t match; regaparams_t params; tre_regaparams_default(¶ms); params.max_err = 0; params.max_cost = 0; status = tre_tnfa_run_approx(tnfa, string, (int)len, type, tags, &match, params, eflags, &eo); } #endif /* TRE_APPROX */ else { /* Exact matching, no back references, use the parallel matcher. */ status = tre_tnfa_run_parallel(tnfa, string, (int)len, type, tags, eflags, &eo); } if (status == REG_OK) /* A match was found, so fill the submatch registers. */ tre_fill_pmatch(nmatch, pmatch, tnfa->cflags, tnfa, tags, eo); #ifndef TRE_USE_ALLOCA if (tags) xfree(tags); #endif /* !TRE_USE_ALLOCA */ return status; }
/* M A I N *******************************************************************/ int main(int argc, char *argv[]) { int opt_index; opt_index = process_options(argc, argv); if (opt_index >= argc) { fprintf(stderr, "%s : %s\n", PRG_NAME, "[err] specify a record string to process!"); exit(1); } if (argv[1]) strncpy (record, argv[opt_index], MAX_CHAR); if ( strlen(record) == 0 ) { fprintf(stdout, "Please enter a valid string to search pattern for!\n"); fprintf(stdout, "Usage: %s %s %s %s\n", PRG_NAME, "[COSTS]", "[PATTERN]", "[STRING]"); exit(1); } /* Testing patterns */ // char record[] = "ATTTACTATGTAAAGATAGAAGGAATAAGGTGAAG"; // char regexp[] = "GATTT"; // char regexp[] = "GTAAAGA"; // char regexp[] = "(G|T)A*GAT"; int comp_flags = REG_EXTENDED | REG_ICASE ; static regex_t preg; /* Compiled pattern to search for. */ static regaparams_t match_params; /* regexp matching parameters */ int errcode; regmatch_t pmatch = { 0, 0 }; /* matched pattern structure */ regamatch_t match; /* overall match structure */ memset(&match, 0, sizeof(match)); /* initialize the overall match struct */ match.pmatch = &pmatch; /* assign default pattern structure */ match.nmatch = 1; /* initialization of pmatch array */ /* setup the default match parameters */ tre_regaparams_default(&match_params); /* Set the maximum number of errors allowed for a record to match. */ match_params.max_cost = max_cost; match_params.cost_ins = cost_ins; match_params.cost_del = cost_del; match_params.cost_subst = cost_subs; fprintf(stdout, "Default regex params set by TRE:\n"); fprintf(stdout, "\t%-12s : %4d\n", "cost_ins", match_params.cost_ins); fprintf(stdout, "\t%-12s : %4d\n", "cost_del", match_params.cost_del); fprintf(stdout, "\t%-12s : %4d\n", "cost_substr", match_params.cost_subst); fprintf(stdout, "\t%-12s : %4d\n", "max_cost", match_params.max_cost); fprintf(stdout, "\t%-12s : %4d\n", "max_ins", match_params.max_ins); fprintf(stdout, "\t%-12s : %4d\n", "max_del", match_params.max_del); fprintf(stdout, "\t%-12s : %4d\n", "max_subst", match_params.max_subst); fprintf(stdout, "\t%-12s : %4d\n", "max_err", match_params.max_err); fprintf(stdout, "\n\n"); /* Step 1: compile the regex */ errcode = tre_regcomp(&preg, regexp, comp_flags); if (errcode) { char errbuf[256]; tre_regerror(errcode, &preg, errbuf, sizeof(errbuf)); fprintf(stderr, "%s: %s: %s\n", PRG_NAME, "Error in search pattern", errbuf); exit(1); } // if (tre_regexec(&delim, "", 0, NULL, 0) == REG_OK) // { // fprintf(stderr, "%s: %s\n", PRG_NAME, // "Record delimiter pattern must not match an empty string"); // exit(1); // } /* Step 2: search for the pattern in the haystack */ errcode = tre_regaexec(&preg, record, &match, match_params, 0); if (errcode == REG_OK) { fprintf(stdout, "Found match!\n"); fprintf(stdout, "\t%10s : %s\n", "record", record); fprintf(stdout, "\t%10s : %s\n", "pattern", regexp); fprintf(stdout, "\t%10s : %4d\n", "cost", match.cost); fprintf(stdout, "\t%10s : %4d\n", "num_ins", match.num_ins); fprintf(stdout, "\t%10s : %4d\n", "num_del", match.num_del); fprintf(stdout, "\t%10s : %4d\n", "num_subst", match.num_subst); fprintf(stdout, "\t%10s : %2d - %2d\n", "char pos", pmatch.rm_so, pmatch.rm_eo); } else { fprintf(stdout, "Found no matches!\n"); fprintf(stdout, "%6s : %s\n", "regexp", regexp); fprintf(stdout, "%6s : %s\n", "record", record); } return 0; }