-
Notifications
You must be signed in to change notification settings - Fork 0
/
pcre8_imp.cpp
421 lines (353 loc) · 10 KB
/
pcre8_imp.cpp
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#include "pcre8_imp.h"
#ifndef PHPCPP_H
#include <phpcpp.h>
#endif
#include <sstream>
#include <utility>
//! Internal class to hold state of pcre2 calls for first and all matches, free _matchData
class Pcre8_calls {
public:
//! construct with compiled regular expression pointer.
Pcre8_calls(pcre2_code* re) : _re(re), _matchData(nullptr), _ovector(nullptr) {
}
~Pcre8_calls() {
if (_matchData) {
pcre2_match_data_free(_matchData);
// _ovector is part of _matchData
_ovector = nullptr;
_matchData = nullptr;
}
}
//! find zero or more PRE matches
int doMatch(const svx::string_view &subject, Pcre8_match& matches)
{
if (_matchData == nullptr) {
_matchData = pcre2_match_data_create_from_pattern(_re, nullptr);
}
auto start = (const unsigned char*) subject.data();
auto slen = subject.size();
auto rcount = pcre2_match(_re, start, slen, 0, 0, _matchData, nullptr);
matches._slist.clear();
if (rcount <= 0) {
//std::string test(reinterpret_cast<const char*>(start), slen);
return rcount;
}
if (_ovector == nullptr) {
_ovector = pcre2_get_ovector_pointer(_matchData);
}
if (_ovector[0] > _ovector[1]) {
return -1;
}
addMatchData( matches, start, rcount );
return rcount;
}
//! There are multiple sets of matches.
int doMatchAll(const svx::string_view &seg, Pcre8_matchAll& matchSet)
{
uint32_t option_bits;
uint32_t newline;
// make 1 match results
matchSet.resize(1);
auto mcount = doMatch(seg, matchSet[0]);
// follow the pcre2 demo exactly as possible
if (mcount <= 0) {
return 0;
}
pcre2_pattern_info(_re,PCRE2_INFO_ALLOPTIONS, &option_bits );
bool isUTF8 = (option_bits & PCRE2_UTF) != 0;
pcre2_pattern_info(_re,PCRE2_INFO_NEWLINE, &newline );
bool crlf_is_newline = (newline == PCRE2_NEWLINE_ANY ||
newline == PCRE2_NEWLINE_CRLF ||
newline == PCRE2_NEWLINE_ANYCRLF);
for(;;) {
uint32_t options = 0;
auto subject_length = seg.size();
auto subject = (const unsigned char*) seg.data();
PCRE2_SIZE start_offset = _ovector[1]; // end of previous match
if (_ovector[0] == _ovector[1]) {
if (_ovector[0] == subject_length)
break;
options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
}
else {
PCRE2_SIZE startchar = pcre2_get_startchar(_matchData);
if (startchar >= subject_length)
break;
start_offset = startchar + 1;
if (isUTF8) {
for( ; start_offset < subject_length; start_offset++){
if ((subject[start_offset] & 0xC0) != 0x80)
break;
}
}
}
// next match
auto rcount = pcre2_match(
_re, subject, subject_length,
start_offset,
options,
_matchData,
nullptr
);
if (rcount == PCRE2_ERROR_NOMATCH) {
if (options == 0)
break; // all matches found
_ovector[1] = start_offset + 1;
if (crlf_is_newline && (start_offset < subject_length - 1) &&
subject[start_offset] == '\r' &&
subject[start_offset+1] == '\n')
_ovector[1] += 1;
else if (isUTF8) {
while(_ovector[1] < subject_length) {
if ((subject[start_offset] & 0xC0) != 0x80)
break;
_ovector[1] += 1;
}
}
continue;
}
if (rcount <= 0) {
//not recoverable
return rcount;
}
if (_ovector[0] > _ovector[1]) {
// see pcre2demo, "\\K was used in an assertion to set the match start after its end."
return -1;
}
auto matchCount = matchSet.size();
matchSet.resize(matchCount + 1);
Pcre8_match& match = matchSet[matchCount];
match._rcode = rcount;
addMatchData(match, subject, rcount);
}
return matchSet.size();
}
private:
//! start: match subject, rcount: number of matches
void addMatchData(Pcre8_match& matches, const unsigned char* start, int rcount) {
for(int i = 0; i < rcount; i++)
{
auto offset = 2*i;
auto substart = reinterpret_cast<const char*>(start + _ovector[offset]);
size_t sublen = _ovector[offset+1] - _ovector[offset];
matches._slist.push_back(std::move(std::string(substart, sublen)));
}
}
// this is burrowed.
pcre2_code* _re;
// these are managed
pcre2_match_data* _matchData;
PCRE2_SIZE* _ovector;
};
IdList toIdList(const Php::Value& v) {
IdList result;
auto ct = v.size();
result.reserve(ct);
for(int i = 0; i < v.size(); i++)
{
result.push_back(v[i]);
}
return result;
}
void Pcre8_map::setRex(const Pcre8_share& reg)
{
auto index = reg.get()->_id;
auto pit = _map.find(index);
if (pit != _map.end())
{ // replace, delete old
pit->second = reg;
}
else {
_map.insert(std::pair<int, Pcre8_share>(index,reg));
}
}
bool Pcre8_map::hasKey(int index) const
{
auto pit = _map.find(index);
return (pit != _map.end());
}
int
Pcre8_map::firstMatch(
const svx::string_view& sv,
const IdList& ids,
Pcre8_match& matches)
{
int result = 0;
auto idend = ids.end();
for (auto mid = ids.begin(); mid != idend; mid++) {
result = this->match(sv,(*mid), matches);
if (result > 0)
break;
}
return result;
}
int Pcre8_map::match(const svx::string_view& sv,
int mapId,
Pcre8_match& matches)
{
Pcre8_share reg;
if (getRex(mapId, reg)) {
return reg.get()->match(sv, matches);
}
return 0;
}
bool Pcre8_map::getRex(int index, Pcre8_share& reg) const
{
auto pit = _map.find(index);
if (pit != _map.end())
{
reg = pit->second;
return true;
}
else
return false;
}
int Pcre8_map::eraseRex(int index)
{
return _map.erase(index);
}
Pcre8_imp::Pcre8_imp() : _re(nullptr), _id(0)
{
}
Pcre8_imp::~Pcre8_imp()
{
(*this).setRex(nullptr);
}
void Pcre8_imp::setRex(pcre2_code* re) {
if (_re != nullptr) {
//Php::out << "Freed Re" << std::endl;
pcre2_code_free(_re);
}
_re = re;
}
bool Pcre8_imp::compile(std::string& _error)
{
char const* sptr = _eStr.data();
auto pattern = reinterpret_cast<const unsigned char*>(sptr);
PCRE2_SIZE psize = _eStr.size();
int errornumber;
PCRE2_SIZE erroroffset;
_re = pcre2_compile(
pattern,
psize,
0, /* default options */
&errornumber, /* for error number */
&erroroffset, /* for error offset */
0
);
if (_re == nullptr) {
std::stringstream ss;
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errornumber, buffer, sizeof(buffer));
ss << "PCRE2 compilation failed at offset "
<< (int)erroroffset << std::endl;
ss << buffer << std::endl;
_error = ss.str();
return false;
}
else {
_error.clear();
return true;
}
}
int
Pcre8_imp::match(const svx::string_view& sv, Pcre8_match& matches)
{
int rct;
auto slen = sv.size();
if (slen > 0) {
Pcre8_calls pre_call(_re);
rct = pre_call.doMatch(sv, matches);
if (rct > 0) {
// _rcode to hold match mapId
//Php::out << "Matched " << pimp->_id << std::endl;
matches._rcode = this->_id;
return matches._rcode;
}
}
matches._rcode = 0;
return 0;
}
int
Pcre8_imp::doMatchAll(const svx::string_view& sv, Pcre8_matchAll& matchSet)
{
Pcre8_calls pre_call(_re);
return pre_call.doMatchAll(sv, matchSet);
}
int
Pcre8_imp::doMatch(const svx::string_view& sv, Pcre8_match& matches)
{
Pcre8_calls pre_call(_re);
return pre_call.doMatch(sv, matches);
}
Pcre8_match::Pcre8_match() : _rcode(0)
{
}
Pcre8_match::Pcre8_match(const Pcre8_match &m)
: _slist(m._slist), _rcode(m._rcode)
{
}
Pcre8_match &
Pcre8_match::operator=(const Pcre8_match &m)
{
if (this != &m) {
_rcode = m._rcode;
_slist = m._slist;
}
return *this;
}
Pcre8_match::Pcre8_match(const Pcre8_match &&m)
: _slist(std::move(m._slist)), _rcode(std::move(m._rcode))
{
}
Pcre8_match &
Pcre8_match::operator=(const Pcre8_match &&m)
{
_slist = std::move(m._slist);
_rcode = std::move(m._rcode);
return *this;
}
Pcre8_share
pun::makeSharedRe(int mapId, const char* estr, unsigned int slen)
{
Pcre8_share sp = std::make_shared<Pcre8_imp>();
auto pimp = sp.get();
pimp->_eStr.assign(estr,slen);
pimp->_id = mapId;
std::string errorMsg;
if (!pimp->compile(errorMsg)) {
throw Php::Exception(errorMsg);
}
return sp;
}
bool CharMap::hasKey(char32_t ix) const
{
auto pit = _map.find(ix);
return (pit != _map.end());
}
void CharMap::setKV(char32_t ix, int tokenId)
{
auto pit = _map.find(ix);
if (pit != _map.end())
{ // replace, delete old
pit->second = tokenId;
}
else {
_map.insert(std::pair<char32_t, int>(ix,tokenId));
}
}
int CharMap::getV(char32_t ix) const
{
auto pit = _map.find(ix);
if (pit != _map.end())
{
//Php::out << "Single " << (unsigned int) ix << " found " << pit->second << std::endl;
return pit->second;
}
//Php::out << "Single " << (unsigned int) ix << " Not found " << std::endl;
return 0;
}
int CharMap::eraseV(char32_t ix)
{
return _map.erase(ix);
}