Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function match_times and match_full #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions re.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "re.h"
#include <stdio.h>
#include <ctype.h>
#include <string.h>

/* Definitions: */

Expand Down Expand Up @@ -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: */
Expand Down
7 changes: 7 additions & 0 deletions re.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
38 changes: 38 additions & 0 deletions tests/test3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}