/
jpcre.c
119 lines (108 loc) · 3.02 KB
/
jpcre.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#include "jpcre.h"
char* matches(char* pattern, char* options, char* subject) {
pcre *re;
int rc;
int opts;
const char *error;
int erroffset;
int ovector[OVECCOUNT];
int subject_length;
int match;
char *substring;
int substring_length;
char* result;
opts = parse_options(options, &error, &erroffset);
if (opts < 0) {
asprintf(&result, "error:Error parsing options %s at offset %d: %s", options, erroffset, error);
free(error);
return result;
}
re = pcre_compile(pattern, opts, &error, &erroffset, NULL);
if (re == NULL) {
asprintf(&result, "error:Error compiling %s at offset %d: %s", pattern, erroffset, error);
return result;
}
subject_length = (int)strlen(subject);
rc = pcre_exec(re, NULL, subject, subject_length, 0, 0, ovector, OVECCOUNT);
if (rc < 0) {
switch (rc) {
case PCRE_ERROR_NOMATCH:
asprintf(&result, "success:0");
break;;
default:
asprintf(&result, "error:Matching error: %d");
break;;
}
pcre_free(re);
return result;
}
if (rc == 0) {
rc = OVECCOUNT/3;
}
match = rc - 1;
substring = subject + ovector[2*match];
substring_length = ovector[2*match+1] - ovector[2*match];
asprintf(&result, "success:1,%.*s", substring_length, substring);
pcre_free(re);
return result;
}
int parse_options(char *options, char **error, int *erroffset) {
int opts = 0;
char *p = options;
while (*p != 0) {
switch (*p) {
case 'f':
opts |= PCRE_FIRSTLINE;
break;
case 'i':
opts |= PCRE_CASELESS;
break;
case 'm':
opts |= PCRE_MULTILINE;
break;
case 's':
opts |= PCRE_DOTALL;
break;
case 'x':
opts |= PCRE_EXTENDED;
break;
case 'A':
opts |= PCRE_ANCHORED;
break;
case 'C':
opts |= PCRE_AUTO_CALLOUT;
break;
case 'E':
opts |= PCRE_DOLLAR_ENDONLY;
break;
case 'J':
opts |= PCRE_DUPNAMES;
break;
case 'N':
opts |= PCRE_NO_AUTO_CAPTURE;
break;
case 'U':
opts |= PCRE_UNGREEDY;
break;
case 'X':
opts |= PCRE_EXTRA;
break;
case '8':
opts |= PCRE_UTF8;
break;
case '?':
opts |= PCRE_NO_UTF8_CHECK;
break;
default:
*erroffset = p - options;
asprintf(error, "Invalid option %c", *p);
return -1;
break;
}
p++;
}
return opts;
}