diff --git a/Makefile b/Makefile index 857d2ee..f452c12 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ all: @$(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/test3.c -o tests/test3 clean: @rm -f tests/test1 tests/test2 tests/test_rand tests/test_compile diff --git a/re.c b/re.c index 20d1474..dbc4381 100644 --- a/re.c +++ b/re.c @@ -32,6 +32,7 @@ #include "re.h" #include #include +#include /* Definitions: */ @@ -285,6 +286,33 @@ void re_print(regex_t* pattern) } } +/* Return the number of occurence matched, 0 if there is not match + * Accept a function to call at each success match with index and length of match + */ +unsigned int match_times(re_t pattern, const char* text, void (*success_function)(int,int*)) +{ + int keep = 0, success = 0, match_length = 0; + unsigned int occurence = 0; + + success = re_matchp(pattern,text,&match_length); + while( success != -1 ) + { + occurence++; + (*success_function)(keep+success,&match_length); // (int index, int* match_length) + keep += success+match_length; + success = re_matchp(pattern,text+keep,&match_length); + } + return occurence; +} + +/* Return 1 if pattern match perfectly with text, 0 otherwise */ +int match_full(re_t pattern, const char* text) +{ + int match_length; + // if index of start match is 0 and size of text is egal to the matched length + if (re_matchp(pattern,text,&match_length) == 0 && strlen(text) == (size_t)match_length) return 1; // true + return 0; // false +} /* Private functions: */ diff --git a/re.h b/re.h index 69facc6..604038b 100644 --- a/re.h +++ b/re.h @@ -57,6 +57,13 @@ int re_matchp(re_t pattern, const char* text, int* matchlength); /* Find matches of the txt pattern inside text (will compile automatically first). */ int re_match(const char* pattern, const char* text, int* matchlength); +/* Return 1 if pattern match perfectly with text, 0 otherwise */ +int match_full(re_t pattern, const char* text); + +/* Return the number of occurence matched, 0 if there is not match + * Accept a function to call at each success match with index and length of match + */ + unsigned int match_times(re_t pattern, const char* text, void (*success_function)(int,int*)); #ifdef __cplusplus } diff --git a/tests/test3.c b/tests/test3.c new file mode 100644 index 0000000..5d25e14 --- /dev/null +++ b/tests/test3.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "re.h" + +static void default_function(int index, int* match_length) +{ + printf("match at index %i, with length of %i\n", index, *match_length); +} + +void realize_a_test(char* text, char* pattern) +{ + printf("text is \"%s\" and pattern \"%s\"\n", text, pattern); + int occurrence; + re_t my_regex = re_compile(pattern); + + /* MATCH TIMES */ + if ( (occurrence = match_times(my_regex,text,default_function)) ) + printf("there is %i occurence\n", occurrence); + else + puts("0 match"); + + /* MATCH FULL */ + if (match_full(my_regex,text)) + puts("pattern match perfectly\n"); + else + puts("pattern doesn't match perfectly\n"); +} + +int main(void) +{ + realize_a_test("pseudo404","."); + realize_a_test("pseudo404",".+"); + realize_a_test("554864","5"); + realize_a_test("554864","[0-9][0-9]"); + realize_a_test("lettre","[0-9]"); // no occurence and obviously no match + return 0; +} \ No newline at end of file