-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmatch.h
57 lines (54 loc) · 1.39 KB
/
bmatch.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#ifndef BMATCH_H
#define BMATCH_H
#include <string.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) == 0 ? -1 : 0;
int total_steps = 0;
do
{
total_steps++;
if (bmatchhere(regexp, text))
return total_steps;
} while (*text++ != '\0');
return -1;
}
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;
}
unsigned int bmatch_count(char * expr, char * text){
unsigned int occurence_count = 0;
size_t expr_length = strlen(expr);
while(1){
int match_info = bmatch(expr, text);
if(match_info == -1)
break;
occurence_count++;
text += match_info + expr_length;
}
return occurence_count;
}
#endif