forked from alimpfard/citron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcre_split.h
201 lines (170 loc) · 4.72 KB
/
pcre_split.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
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
193
194
195
196
197
198
199
200
201
#ifndef __GUARD_PCRE_SPLIT_H
#define __GUARD_PCRE_SPLIT_H
/*
*/
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#include "citron.h"
ctr_object *pcre_split(char *, char *);
ssize_t pcre_indexof_internal(pcre* re, char* string);
ssize_t pcre_last_indexof_internal(pcre* re, char* string);
ssize_t pcre_last_indexof(char* pattern, char* string);
ssize_t pcre_indexof(char* pattern, char* string);
#define OVECCOUNT 30
typedef struct split_t_internal {
char *string;
char *match;
unsigned int length;
unsigned int offset;
} split_t_internal;
split_t_internal *pcre_split_internal(pcre *, char *);
ssize_t pcre_indexof(char* pattern, char* string) {
pcre *re;
const char* error;
int erroffset;
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
if (re == NULL) {
char serror[1024];
size_t len = sprintf(serror, "Error: PCRE compilation failed at offset %d: %s\n", erroffset, error);
CtrStdFlow = ctr_build_string(serror, len);
return 0;
}
ssize_t offset = pcre_indexof_internal(re, string);
pcre_free(re);
return offset;
}
ssize_t pcre_last_indexof(char* pattern, char* string) {
pcre *re;
const char* error;
int erroffset;
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
if (re == NULL) {
char serror[1024];
size_t len = sprintf(serror, "Error: PCRE compilation failed at offset %d: %s\n", erroffset, error);
CtrStdFlow = ctr_build_string(serror, len);
return 0;
}
ssize_t offset = pcre_indexof_internal(re, string);
size_t position = offset;
while(1) {
offset = pcre_indexof_internal(re, string+position);
if(offset != -1)
position = position + offset;
else break;
}
pcre_free(re);
return position;
}
/* Initialise the RegEx */
ctr_object *pcre_split(char *pattern, char *string) {
pcre *re;
const char *error;
int erroffset;
split_t_internal *si;
ctr_argument* push_arg = ctr_heap_allocate(sizeof(ctr_argument));
ctr_object* sarr = ctr_array_new(CtrStdArray, NULL);
/*
Make a local copy of pattern and string
*/
char *s = string;
/*
Setup RegEx
*/
re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
/*
Check if compilation was successful
*/
if(re == NULL) {
char serror[1079];
size_t len = sprintf(serror, "Error: PCRE compilation failed at offset %d: %s\n", erroffset, error);
CtrStdFlow = ctr_build_string(serror, len);
return NULL;
}
/*
Loop thorough string
*/
char *match;
do {
/* Return first occurence of match of RegEx */
si = pcre_split_internal(re, s);
/* Copy pointer of match */
match = si->match;
/* Push into array */
if(si->offset || si->length) {
push_arg->object = ctr_build_string(s, si->offset);
ctr_array_push(sarr, push_arg);
}
/* Increment string pointer to skip previous match */
s += si->offset + si->length;
ctr_heap_free(si);
} while(match && *s!=0);
/*
Free locally allocated memory
*/
if(*s != 0) {
push_arg->object = ctr_build_string_from_cstring(s);
ctr_array_push(sarr, push_arg);
}
pcre_free(re);
ctr_heap_free(push_arg);
return sarr;
}
split_t_internal *pcre_split_internal(pcre *re, char *string)
{
int rc;
int ovector[OVECCOUNT];
int length;
split_t_internal *s = (split_t_internal *)ctr_heap_allocate(sizeof(split_t_internal));
s->offset = 0;
length = (int)strlen(string);
rc = pcre_exec(re, NULL, string, length, 0, 0, ovector, OVECCOUNT);
/* check for matches */
if(rc < 0) {
switch(rc) {
case PCRE_ERROR_NOMATCH:
s->string = (char *)ctr_heap_allocate(sizeof(char)*(length + 1));
strncpy(s->string, string, length);
s->match = NULL;
return s;
break;
default: {
char err[1024];
size_t len = sprintf(err, "Error: Matching error: %d\n", rc);
CtrStdFlow = ctr_build_string(err, len);
return (split_t_internal *)NULL;
break;
}
}
}
/* check if output vector was large enough */
if(rc == 0) {
rc = OVECCOUNT/3;
fprintf(stderr, "Warning: ovector only has room for %d captured substrings\n", rc-1);
}
s->string = ctr_heap_allocate(sizeof(char) * (ovector[0] + 1));
strncpy(s->string, string, ovector[0]);
s->match = ctr_heap_allocate(sizeof(char) * ((ovector[1] - ovector[0]) + 1));
strncpy(s->match, (char *)(string + ovector[0]), (ovector[1] - ovector[0]));
s->length = ovector[1] - ovector[0];
s->offset = ovector[0];
return (split_t_internal *)s;
}
ssize_t pcre_indexof_internal(pcre* re, char* string) {
int rc;
int ovector[OVECCOUNT];
int length;
length = (int)strlen(string);
rc = pcre_exec(re, NULL, string, length, 0, 0, ovector, OVECCOUNT);
/* check for matches */
if(rc < 0) {
return -1;
}
/* check if output vector was large enough */
if(rc == 0) {
rc = OVECCOUNT/3;
fprintf(stderr, "Warning: ovector only has room for %d captured substrings\n", rc-1);
}
return ovector[1];
}
#endif