Skip to content

Commit

Permalink
Extracted bmatch.h
Browse files Browse the repository at this point in the history
  • Loading branch information
GerbenAaltink committed Sep 28, 2024
1 parent b779a07 commit 2db7cfa
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 39 deletions.
39 changes: 39 additions & 0 deletions bmatch.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef BMATCH_H
#define BMATCH_H
#include <stdio.h>
#include <assert.h>
int bmatchhere(char *regexp, char *text);
int bmatchstar(int c, char *regexp, char *text);
int bmatch(char *regexp, char *text)
{
if (regexp[0] == '^')
return bmatchhere(regexp + 1, text);
do
{
if (bmatchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
int bmatchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return bmatchstar(regexp[0], regexp + 2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
return bmatchhere(regexp + 1, text + 1);
return 0;
}
int bmatchstar(int c, char *regexp, char *text)
{
do
{
if (bmatchhere(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}
#endif
38 changes: 0 additions & 38 deletions rmatch.c
Original file line number Diff line number Diff line change
@@ -1,42 +1,4 @@
#include <stdio.h>
#include "rmatch.h"
#include <assert.h>

int matchhere(char *regexp, char *text);
int matchstar(int c, char *regexp, char *text);
int match(char *regexp, char *text)
{
if (regexp[0] == '^')
return matchhere(regexp + 1, text);
do
{
if (matchhere(regexp, text))
return 1;
} while (*text++ != '\0');
return 0;
}
int matchhere(char *regexp, char *text)
{
if (regexp[0] == '\0')
return 1;
if (regexp[1] == '*')
return matchstar(regexp[0], regexp + 2, text);
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (*text != '\0' && (regexp[0] == '.' || regexp[0] == *text))
return matchhere(regexp + 1, text + 1);
return 0;
}
int matchstar(int c, char *regexp, char *text)
{
do
{
if (matchhere(regexp, text))
return 1;
} while (*text != '\0' && (*text++ == c || c == '.'));
return 0;
}


int main()
{
Expand Down
5 changes: 4 additions & 1 deletion rmatch.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#ifndef RMATCH_H
#define RMATCH_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand Down Expand Up @@ -204,4 +206,5 @@ void rmatch_tests()
rmatch_test("T.*e q.*k b.*n f+.*x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n fo+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n f+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){NULL});
}
}
#endif

0 comments on commit 2db7cfa

Please sign in to comment.