forked from moovweb/rubex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chelper.c
192 lines (167 loc) · 5.8 KB
/
chelper.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef BENCHMARK_CHELP
#include <sys/time.h>
#endif
#include "chelper.h"
int NewOnigRegex( char *pattern, int pattern_length, int option,
OnigRegex *regex, OnigEncoding *encoding, OnigErrorInfo **error_info, char **error_buffer) {
int ret = ONIG_NORMAL;
int error_msg_len = 0;
OnigUChar *pattern_start = (OnigUChar *) pattern;
OnigUChar *pattern_end = (OnigUChar *) (pattern + pattern_length);
*error_info = (OnigErrorInfo *) malloc(sizeof(OnigErrorInfo));
memset(*error_info, 0, sizeof(OnigErrorInfo));
onig_initialize_encoding(*encoding);
*error_buffer = (char*) malloc(ONIG_MAX_ERROR_MESSAGE_LEN * sizeof(char));
memset(*error_buffer, 0, ONIG_MAX_ERROR_MESSAGE_LEN * sizeof(char));
ret = onig_new(regex, pattern_start, pattern_end, (OnigOptionType)(option), *encoding, OnigDefaultSyntax, *error_info);
if (ret != ONIG_NORMAL) {
error_msg_len = onig_error_code_to_str((unsigned char*)(*error_buffer), ret, *error_info);
if (error_msg_len >= ONIG_MAX_ERROR_MESSAGE_LEN) {
error_msg_len = ONIG_MAX_ERROR_MESSAGE_LEN - 1;
}
(*error_buffer)[error_msg_len] = '\0';
}
return ret;
}
int SearchOnigRegex( void *str, int str_length, int offset, int option,
OnigRegex regex, OnigErrorInfo *error_info, char *error_buffer, int *captures, int *numCaptures) {
int ret = ONIG_MISMATCH;
int error_msg_len = 0;
OnigRegion *region;
#ifdef BENCHMARK_CHELP
struct timeval tim1, tim2;
long t;
#endif
OnigUChar *str_start = (OnigUChar *) str;
OnigUChar *str_end = (OnigUChar *) (str_start + str_length);
OnigUChar *search_start = (OnigUChar *)(str_start + offset);
OnigUChar *search_end = str_end;
#ifdef BENCHMARK_CHELP
gettimeofday(&tim1, NULL);
#endif
region = onig_region_new();
ret = onig_search(regex, str_start, str_end, search_start, search_end, region, option);
if (ret < 0 && error_buffer != NULL) {
error_msg_len = onig_error_code_to_str((unsigned char*)(error_buffer), ret, error_info);
if (error_msg_len >= ONIG_MAX_ERROR_MESSAGE_LEN) {
error_msg_len = ONIG_MAX_ERROR_MESSAGE_LEN - 1;
}
error_buffer[error_msg_len] = '\0';
}
else if (captures != NULL) {
int i;
int count = 0;
for (i = 0; i < region->num_regs; i++) {
captures[2*count] = region->beg[i];
captures[2*count+1] = region->end[i];
count ++;
}
*numCaptures = count;
}
onig_region_free(region, 1);
#ifdef BENCHMARK_CHELP
gettimeofday(&tim2, NULL);
t = (tim2.tv_sec - tim1.tv_sec) * 1000000 + tim2.tv_usec - tim1.tv_usec;
printf("%ld microseconds elapsed\n", t);
#endif
return ret;
}
int MatchOnigRegex(void *str, int str_length, int offset, int option,
OnigRegex regex) {
int ret = ONIG_MISMATCH;
int error_msg_len = 0;
OnigRegion *region;
#ifdef BENCHMARK_CHELP
struct timeval tim1, tim2;
long t;
#endif
OnigUChar *str_start = (OnigUChar *) str;
OnigUChar *str_end = (OnigUChar *) (str_start + str_length);
OnigUChar *search_start = (OnigUChar *)(str_start + offset);
#ifdef BENCHMARK_CHELP
gettimeofday(&tim1, NULL);
#endif
region = onig_region_new();
ret = onig_match(regex, str_start, str_end, search_start, region, option);
onig_region_free(region, 1);
#ifdef BENCHMARK_CHELP
gettimeofday(&tim2, NULL);
t = (tim2.tv_sec - tim1.tv_sec) * 1000000 + tim2.tv_usec - tim1.tv_usec;
printf("%ld microseconds elapsed\n", t);
#endif
return ret;
}
int LookupOnigCaptureByName(char *name, int name_length,
OnigRegex regex) {
int ret = ONIGERR_UNDEFINED_NAME_REFERENCE;
OnigRegion *region;
#ifdef BENCHMARK_CHELP
struct timeval tim1, tim2;
long t;
#endif
OnigUChar *name_start = (OnigUChar *) name;
OnigUChar *name_end = (OnigUChar *) (name_start + name_length);
#ifdef BENCHMARK_CHELP
gettimeofday(&tim1, NULL);
#endif
region = onig_region_new();
ret = onig_name_to_backref_number(regex, name_start, name_end, region);
onig_region_free(region, 1);
#ifdef BENCHMARK_CHELP
gettimeofday(&tim2, NULL);
t = (tim2.tv_sec - tim1.tv_sec) * 1000000 + tim2.tv_usec - tim1.tv_usec;
printf("%ld microseconds elapsed\n", t);
#endif
return ret;
}
typedef struct {
char *nameBuffer;
int bufferOffset;
int bufferSize;
int *numbers;
int numIndex;
} group_info_t;
int name_callback(const UChar* name, const UChar* name_end,
int ngroup_num, int* group_nums,
regex_t* reg, void* arg)
{
int nameLen, offset, newOffset;
group_info_t *groupInfo;
groupInfo = (group_info_t*) arg;
offset = groupInfo->bufferOffset;
nameLen = name_end - name;
newOffset = offset + nameLen;
//if there are already names, add a ";"
if (offset > 0) {
newOffset += 1;
}
if (newOffset <= groupInfo->bufferSize) {
if (offset > 0) {
groupInfo->nameBuffer[offset] = ';';
offset += 1;
}
memcpy(&groupInfo->nameBuffer[offset], name, nameLen);
}
groupInfo->bufferOffset = newOffset;
if (ngroup_num > 0) {
groupInfo->numbers[groupInfo->numIndex] = group_nums[ngroup_num-1];
} else {
groupInfo->numbers[groupInfo->numIndex] = -1;
}
groupInfo->numIndex += 1;
return 0; /* 0: continue */
}
int GetCaptureNames(OnigRegex reg, void *buffer, int bufferSize, int* groupNumbers) {
int ret;
group_info_t groupInfo;
groupInfo.nameBuffer = (char*)buffer;
groupInfo.bufferOffset = 0;
groupInfo.bufferSize = bufferSize;
groupInfo.numbers = groupNumbers;
groupInfo.numIndex = 0;
onig_foreach_name(reg, name_callback, (void* )&groupInfo);
return groupInfo.bufferOffset;
}