From e9f6a87d2a33a1f819659e937d480e155ca7e5dc Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 18 Dec 2022 09:34:51 +0100 Subject: [PATCH 1/2] fix end anchor match length --- re.c | 4 ++++ tests/test_end_anchor.c | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 tests/test_end_anchor.c diff --git a/re.c b/re.c index 20d1474..06bab58 100644 --- a/re.c +++ b/re.c @@ -99,6 +99,10 @@ int re_matchp(re_t pattern, const char* text, int* matchlength) return idx; } + + // Reset match length for the next starting point + *matchlength = 0; + } while (*text++ != '\0'); } diff --git a/tests/test_end_anchor.c b/tests/test_end_anchor.c new file mode 100644 index 0000000..005fc7b --- /dev/null +++ b/tests/test_end_anchor.c @@ -0,0 +1,20 @@ +#include +#include +#include "../re.h" + +int main() { + + const char *text = "table football"; + const char *pattern = "l$"; + int index,len; + + index = re_match(pattern, text, &len); + + if (index==13 && len==1) { + return 0; + } else { + printf("ERROR! index=%d len=%d \n",index,len); + return -1; + } + +} From 359a38c6ea729661a2109b5b234ff149c414dcba Mon Sep 17 00:00:00 2001 From: Federico Perini Date: Sun, 18 Dec 2022 09:37:29 +0100 Subject: [PATCH 2/2] add end anchor test to makefile --- Makefile | 18 ++++++++++++------ tests/test_end_anchor.c | 2 +- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 857d2ee..c640f46 100644 --- a/Makefile +++ b/Makefile @@ -17,14 +17,15 @@ PYTHON != if (python --version 2>&1 | grep -q 'Python 2\..*'); then \ CFLAGS := -O3 -Wall -Wextra -std=c99 -I. all: - @$(CC) $(CFLAGS) re.c tests/test1.c -o tests/test1 - @$(CC) $(CFLAGS) re.c tests/test2.c -o tests/test2 - @$(CC) $(CFLAGS) re.c tests/test_rand.c -o tests/test_rand - @$(CC) $(CFLAGS) re.c tests/test_rand_neg.c -o tests/test_rand_neg - @$(CC) $(CFLAGS) re.c tests/test_compile.c -o tests/test_compile + @$(CC) $(CFLAGS) re.c tests/test1.c -o tests/test1 + @$(CC) $(CFLAGS) re.c tests/test2.c -o tests/test2 + @$(CC) $(CFLAGS) re.c tests/test_rand.c -o tests/test_rand + @$(CC) $(CFLAGS) re.c tests/test_rand_neg.c -o tests/test_rand_neg + @$(CC) $(CFLAGS) re.c tests/test_compile.c -o tests/test_compile + @$(CC) $(CFLAGS) re.c tests/test_end_anchor.c -o tests/test_end_anchor clean: - @rm -f tests/test1 tests/test2 tests/test_rand tests/test_compile + @rm -f tests/test1 tests/test2 tests/test_rand tests/test_compile tests/test_end_anchor @#@$(foreach test_bin,$(TEST_BINS), rm -f $(test_bin) ; ) @rm -f a.out @rm -f *.o @@ -106,4 +107,9 @@ test: all @./tests/test2 @echo @echo + @echo + @echo + @./tests/test_end_anchor + @echo + @echo diff --git a/tests/test_end_anchor.c b/tests/test_end_anchor.c index 005fc7b..1809f7c 100644 --- a/tests/test_end_anchor.c +++ b/tests/test_end_anchor.c @@ -1,6 +1,6 @@ #include #include -#include "../re.h" +#include "re.h" int main() {