Fix regex

This commit is contained in:
Roman Venediktov 2024-07-01 14:53:24 +02:00
parent d1b1e04a40
commit d19851fcdf
2 changed files with 14 additions and 19 deletions

View file

@ -22,7 +22,7 @@ remake_runtime:
copy_to_build: all remake_runtime copy_to_build: all remake_runtime
mkdir -p $(BUILDDIR) mkdir -p $(BUILDDIR)
cp runtime/Std.i runtime/runtime.a stdlib/* src/lamac $(BUILDDIR) cp -r runtime/Std.i runtime/runtime.a stdlib/* src/lamac $(BUILDDIR)
install: all install: all

View file

@ -480,40 +480,35 @@ extern void *Lsubstring (aint* args /*void *subj, aint p, aint l*/) {
} }
extern regex_t *Lregexp (char *regexp) { extern regex_t *Lregexp (char *regexp) {
regex_t *b = (regex_t *)malloc(sizeof(regex_t)); regex_t *regexp_compiled = (regex_t *)malloc(sizeof(regex_t));
/* printf ("regexp: %s,\t%x\n", regexp, b); */ memset(regexp_compiled, 0, sizeof(regex_t));
memset(b, 0, sizeof(regex_t)); int res = regcomp(regexp_compiled, regexp, REG_EXTENDED);
// aint n = (aint)re_compile_pattern(regexp, strlen(regexp), b); // printf("Lregexp: got compiled regexp %p, for string %s\n", regexp_compiled, regexp);
aint n = (aint)regcomp(b, regexp, REG_EXTENDED);
#ifdef DEBUG_PRINT if (res != 0) {
printf("Lregexp: got compiled regexp %p, for string %s\n", b, regexp); char buf[100];
#endif regerror(res, regexp_compiled, buf, 100);
failure("%", buf);
}
if (n != 0) { failure("%", strerror(n)); }; return regexp_compiled;
return b;
} }
extern aint LregexpMatch (regex_t *b, char *s, aint pos) { extern aint LregexpMatch (regex_t *b, char *s, aint pos) {
aint res;
regmatch_t match; regmatch_t match;
ASSERT_BOXED("regexpMatch:1", b); ASSERT_BOXED("regexpMatch:1", b);
ASSERT_STRING("regexpMatch:2", s); ASSERT_STRING("regexpMatch:2", s);
ASSERT_UNBOXED("regexpMatch:3", pos); ASSERT_UNBOXED("regexpMatch:3", pos);
// res = re_match(b, s, LEN(TO_DATA(s)->data_header), UNBOX(pos), 0); int res = regexec(b, s + UNBOX(pos), (size_t) 1, &match, 0);
res = regexec(b, s + UNBOX(pos), (size_t) 1, &match, 0);
// printf("regexpMatch %p: %s, res=%d so=%d eo=%d\n", b, s + UNBOX(pos), res, match.rm_so, match.rm_eo);
if (res == 0 && match.rm_so == 0) { if (res == 0 && match.rm_so == 0) {
#ifdef DEBUG_PRINT
printf("Matched!\n");
printf ("regexpMatch %p: %s, res=%d\n", b, s+UNBOX(pos), match.rm_eo);
#endif
return BOX(match.rm_eo); return BOX(match.rm_eo);
} else { } else {
return BOX(-1); return BOX(-1);