void reinit(Regex *r, char *regx) { refree(r); if(regx[0] != '\0') { r->regex = estrdup(regx); r->regc = regcomp(regx); } }
/* * Implementation */ static re_cod * LispRecomp(LispBuiltin *builtin, char *pattern, int cflags) { int code; re_cod *regex = LispMalloc(sizeof(re_cod)); if ((code = recomp(regex, pattern, cflags)) != 0) { char buffer[256]; reerror(code, regex, buffer, sizeof(buffer)); refree(regex); LispFree(regex); LispDestroy("%s: recomp(\"%s\"): %s", STRFUN(builtin), pattern, buffer); } return (regex); }
Regexp * re_update(NODE *t) { NODE *t1; if ((t->re_flags & CASE) == IGNORECASE) { if ((t->re_flags & CONST) != 0) { assert(t->type == Node_regex); return t->re_reg; } t1 = force_string(tree_eval(t->re_exp)); if (t->re_text != NULL) { if (cmp_nodes(t->re_text, t1) == 0) { free_temp(t1); return t->re_reg; } unref(t->re_text); } t->re_text = dupnode(t1); free_temp(t1); } if (t->re_reg != NULL) refree(t->re_reg); if (t->re_cnt > 0) t->re_cnt++; if (t->re_cnt > 10) t->re_cnt = 0; if (t->re_text == NULL || (t->re_flags & CASE) != IGNORECASE) { t1 = force_string(tree_eval(t->re_exp)); unref(t->re_text); t->re_text = dupnode(t1); free_temp(t1); } t->re_reg = make_regexp(t->re_text->stptr, t->re_text->stlen, IGNORECASE, t->re_cnt); t->re_flags &= ~CASE; t->re_flags |= IGNORECASE; return t->re_reg; }
int main(int argc, char *argv[]) { re_cod cod; re_mat mat[10]; int line, ecode, i, len, group, failed; long eo, so; char buf[8192]; char str[8192]; FILE *fp = fopen("tests.txt", "r"); if (fp == NULL) { fprintf(stderr, "failed to open tests.txt\n"); exit(1); } ecode = line = group = failed = 0; cod.cod = NULL; while (fgets(buf, sizeof(buf), fp)) { ++line; if (buf[0] == '#' || buf[0] == '\n') continue; else if (buf[0] == '/') { char *ptr = strrchr(buf, '/'); if (ptr == buf) { fprintf(stderr, "syntax error at line %d\n", line); break; } else { int flags = 0; refree(&cod); for (*ptr++ = '\0'; *ptr; ptr++) { if (*ptr == 'i') flags |= RE_ICASE; else if (*ptr == 'n') flags |= RE_NEWLINE; } ecode = recomp(&cod, buf + 1, flags); failed = ecode; } } else if (buf[0] == '>') { if (cod.cod == NULL) { fprintf(stderr, "no previous pattern at line %d\n", line); break; } len = strlen(buf) - 1; buf[len] = '\0'; strcpy(str, buf + 1); for (i = 0, --len; i < len - 1; i++) { if (str[i] == '\\') { memmove(str + i, str + i + 1, len); --len; switch (str[i]) { case 'a': str[i] = '\a'; break; case 'b': str[i] = '\b'; break; case 'f': str[i] = '\f'; break; case 'n': str[i] = '\n'; break; case 'r': str[i] = '\r'; break; case 't': str[i] = '\t'; break; case 'v': str[i] = '\v'; break; default: break; } } } group = 0; ecode = reexec(&cod, str, 10, &mat[0], 0); if (ecode && ecode != RE_NOMATCH) { reerror(failed, &cod, buf, sizeof(buf)); fprintf(stderr, "%s, at line %d\n", buf, line); break; } } else if (buf[0] == ':') { if (failed) { len = strlen(buf) - 1; buf[len] = '\0'; if (failed == RE_EESCAPE && strcmp(buf, ":EESCAPE") == 0) continue; if (failed == RE_ESUBREG && strcmp(buf, ":ESUBREG") == 0) continue; if (failed == RE_EBRACK && strcmp(buf, ":EBRACK") == 0) continue; if (failed == RE_EPAREN && strcmp(buf, ":EPAREN") == 0) continue; if (failed == RE_EBRACE && strcmp(buf, ":EBRACE") == 0) continue; if (failed == RE_EBADBR && strcmp(buf, ":EBADBR") == 0) continue; if (failed == RE_ERANGE && strcmp(buf, ":ERANGE") == 0) continue; if (failed == RE_ESPACE && strcmp(buf, ":ESPACE") == 0) continue; if (failed == RE_BADRPT && strcmp(buf, ":BADRPT") == 0) continue; if (failed == RE_EMPTY && strcmp(buf, ":EMPTY") == 0) continue; reerror(failed, &cod, buf, sizeof(buf)); fprintf(stderr, "Error value %d doesn't match: %s, at line %d\n", failed, buf, line); break; } else if (!ecode) { fprintf(stderr, "found match when shoudn't, at line %d\n", line); break; } } else { if (failed) { reerror(failed, &cod, buf, sizeof(buf)); fprintf(stderr, "%s, at line %d\n", buf, line); break; } if (sscanf(buf, "%ld,%ld:", &so, &eo) != 2) { fprintf(stderr, "expecting match offsets at line %d\n", line); break; } else if (ecode) { fprintf(stderr, "didn't match, at line %d\n", line); break; } else if (group >= 10) { fprintf(stderr, "syntax error at line %d (too many groups)\n", line); break; } else if (so != mat[group].rm_so || eo != mat[group].rm_eo) { fprintf(stderr, "match failed at line %d, got %ld,%ld: ", line, mat[group].rm_so, mat[group].rm_eo); if (mat[group].rm_so < mat[group].rm_eo) fwrite(str + mat[group].rm_so, mat[group].rm_eo - mat[group].rm_so, 1, stderr); fputc('\n', stderr); break; } ++group; } } fclose(fp); return (ecode); }
LispObj * Lisp_Reexec(LispBuiltin *builtin) /* re-exec regex string &key count start end notbol noteol */ { size_t nmatch; re_mat match[10]; long start, end, length; int code, cflags, eflags; char *string; LispObj *result; re_cod *regexp; LispObj *regex, *ostring, *count, *ostart, *oend, *notbol, *noteol; noteol = ARGUMENT(6); notbol = ARGUMENT(5); oend = ARGUMENT(4); ostart = ARGUMENT(3); count = ARGUMENT(2); ostring = ARGUMENT(1); regex = ARGUMENT(0); if (STRINGP(regex)) regexp = LispRecomp(builtin, THESTR(regex), cflags = 0); else { CHECK_REGEX(regex); regexp = regex->data.regex.regex; cflags = regex->data.regex.options; } CHECK_STRING(ostring); if (count == UNSPEC) nmatch = 1; else { CHECK_INDEX(count); nmatch = FIXNUM_VALUE(count); if (nmatch > 10) LispDestroy("%s: COUNT cannot be larger than 10", STRFUN(builtin)); } if (nmatch && (cflags & RE_NOSUB)) nmatch = 1; eflags = RE_STARTEND; if (notbol != UNSPEC && notbol != NIL) eflags |= RE_NOTBOL; if (noteol != UNSPEC && noteol != NIL) eflags |= RE_NOTEOL; string = THESTR(ostring); LispCheckSequenceStartEnd(builtin, ostring, ostart, oend, &start, &end, &length); match[0].rm_so = start; match[0].rm_eo = end; code = reexec(regexp, string, nmatch, &match[0], eflags); if (code == 0) { if (nmatch && match[0].rm_eo >= match[0].rm_so) { result = CONS(CONS(FIXNUM(match[0].rm_so), FIXNUM(match[0].rm_eo)), NIL); if (nmatch > 1 && match[1].rm_eo >= match[1].rm_so) { int i; GC_ENTER(); LispObj *cons = result; GC_PROTECT(result); for (i = 1; i < nmatch && match[i].rm_eo >= match[i].rm_so; i++) { RPLACD(cons, CONS(CONS(FIXNUM(match[i].rm_so), FIXNUM(match[i].rm_eo)), NIL)); cons = CDR(cons); } GC_LEAVE(); } } else result = NIL; } else result = Knomatch; /* Maybe shoud cache compiled regex, but better the caller do it */ if (!XREGEXP(regex)) { refree(regexp); LispFree(regexp); } return (result); }