1 /* see copyright notice in squirrel.h */
2 #include <squirrel.h>
3 #include <string.h>
4 #include <ctype.h>
5 #include <setjmp.h>
6 #include <sqstdstring.h>
7
8 #ifdef _DEBUG
9 #include <stdio.h>
10
11 static const SQChar *g_nnames[] =
12 {
13 _SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
14 _SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
15 _SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
16 _SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB"),_SC("OP_MB")
17 };
18
19 #endif
20
21 #define OP_GREEDY (MAX_CHAR+1) // * + ? {n}
22 #define OP_OR (MAX_CHAR+2)
23 #define OP_EXPR (MAX_CHAR+3) //parentesis ()
24 #define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:)
25 #define OP_DOT (MAX_CHAR+5)
26 #define OP_CLASS (MAX_CHAR+6)
27 #define OP_CCLASS (MAX_CHAR+7)
28 #define OP_NCLASS (MAX_CHAR+8) //negates class the [^
29 #define OP_RANGE (MAX_CHAR+9)
30 #define OP_CHAR (MAX_CHAR+10)
31 #define OP_EOL (MAX_CHAR+11)
32 #define OP_BOL (MAX_CHAR+12)
33 #define OP_WB (MAX_CHAR+13)
34 #define OP_MB (MAX_CHAR+14) //match balanced
35
36 #define SQREX_SYMBOL_ANY_CHAR ('.')
37 #define SQREX_SYMBOL_GREEDY_ONE_OR_MORE ('+')
38 #define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*')
39 #define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?')
40 #define SQREX_SYMBOL_BRANCH ('|')
41 #define SQREX_SYMBOL_END_OF_STRING ('$')
42 #define SQREX_SYMBOL_BEGINNING_OF_STRING ('^')
43 #define SQREX_SYMBOL_ESCAPE_CHAR ('\\')
44
45
46 typedef int SQRexNodeType;
47
48 typedef struct tagSQRexNode{
49 SQRexNodeType type;
50 SQInteger left;
51 SQInteger right;
52 SQInteger next;
53 }SQRexNode;
54
55 struct SQRex{
56 const SQChar *_eol;
57 const SQChar *_bol;
58 const SQChar *_p;
59 SQInteger _first;
60 SQInteger _op;
61 SQRexNode *_nodes;
62 SQInteger _nallocated;
63 SQInteger _nsize;
64 SQInteger _nsubexpr;
65 SQRexMatch *_matches;
66 SQInteger _currsubexp;
67 void *_jmpbuf;
68 const SQChar **_error;
69 };
70
71 static SQInteger sqstd_rex_list(SQRex *exp);
72
sqstd_rex_newnode(SQRex * exp,SQRexNodeType type)73 static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
74 {
75 SQRexNode n;
76 n.type = type;
77 n.next = n.right = n.left = -1;
78 if(type == OP_EXPR)
79 n.right = exp->_nsubexpr++;
80 if(exp->_nallocated < (exp->_nsize + 1)) {
81 SQInteger oldsize = exp->_nallocated;
82 exp->_nallocated *= 2;
83 exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
84 }
85 exp->_nodes[exp->_nsize++] = n;
86 SQInteger newid = exp->_nsize - 1;
87 return (SQInteger)newid;
88 }
89
sqstd_rex_error(SQRex * exp,const SQChar * error)90 static void sqstd_rex_error(SQRex *exp,const SQChar *error)
91 {
92 if(exp->_error) *exp->_error = error;
93 longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
94 }
95
sqstd_rex_expect(SQRex * exp,SQInteger n)96 static void sqstd_rex_expect(SQRex *exp, SQInteger n){
97 if((*exp->_p) != n)
98 sqstd_rex_error(exp, _SC("expected paren"));
99 exp->_p++;
100 }
101
sqstd_rex_escapechar(SQRex * exp)102 static SQChar sqstd_rex_escapechar(SQRex *exp)
103 {
104 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
105 exp->_p++;
106 switch(*exp->_p) {
107 case 'v': exp->_p++; return '\v';
108 case 'n': exp->_p++; return '\n';
109 case 't': exp->_p++; return '\t';
110 case 'r': exp->_p++; return '\r';
111 case 'f': exp->_p++; return '\f';
112 default: return (*exp->_p++);
113 }
114 } else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
115 return (*exp->_p++);
116 }
117
sqstd_rex_charclass(SQRex * exp,SQInteger classid)118 static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid)
119 {
120 SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS);
121 exp->_nodes[n].left = classid;
122 return n;
123 }
124
sqstd_rex_charnode(SQRex * exp,SQBool isclass)125 static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass)
126 {
127 SQChar t;
128 if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
129 exp->_p++;
130 switch(*exp->_p) {
131 case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
132 case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
133 case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
134 case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
135 case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
136 case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
137 case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
138 case 'p': case 'P': case 'l': case 'u':
139 {
140 t = *exp->_p; exp->_p++;
141 return sqstd_rex_charclass(exp,t);
142 }
143 case 'm':
144 {
145 SQChar cb, ce; //cb = character begin match ce = character end match
146 cb = *++exp->_p; //skip 'm'
147 ce = *++exp->_p;
148 exp->_p++; //points to the next char to be parsed
149 if ((!cb) || (!ce)) sqstd_rex_error(exp,_SC("balanced chars expected"));
150 if ( cb == ce ) sqstd_rex_error(exp,_SC("open/close char can't be the same"));
151 SQInteger node = sqstd_rex_newnode(exp,OP_MB);
152 exp->_nodes[node].left = cb;
153 exp->_nodes[node].right = ce;
154 return node;
155 }
156 case 'b':
157 case 'B':
158 if(!isclass) {
159 SQInteger node = sqstd_rex_newnode(exp,OP_WB);
160 exp->_nodes[node].left = *exp->_p;
161 exp->_p++;
162 return node;
163 } //else default
164 default:
165 t = *exp->_p; exp->_p++;
166 return sqstd_rex_newnode(exp,t);
167 }
168 }
169 else if(!scisprint(*exp->_p)) {
170
171 sqstd_rex_error(exp,_SC("letter expected"));
172 }
173 t = *exp->_p; exp->_p++;
174 return sqstd_rex_newnode(exp,t);
175 }
sqstd_rex_class(SQRex * exp)176 static SQInteger sqstd_rex_class(SQRex *exp)
177 {
178 SQInteger ret = -1;
179 SQInteger first = -1,chain;
180 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
181 ret = sqstd_rex_newnode(exp,OP_NCLASS);
182 exp->_p++;
183 }else ret = sqstd_rex_newnode(exp,OP_CLASS);
184
185 if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class"));
186 chain = ret;
187 while(*exp->_p != ']' && exp->_p != exp->_eol) {
188 if(*exp->_p == '-' && first != -1){
189 SQInteger r;
190 if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
191 r = sqstd_rex_newnode(exp,OP_RANGE);
192 if(exp->_nodes[first].type>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
193 if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
194 exp->_nodes[r].left = exp->_nodes[first].type;
195 SQInteger t = sqstd_rex_escapechar(exp);
196 exp->_nodes[r].right = t;
197 exp->_nodes[chain].next = r;
198 chain = r;
199 first = -1;
200 }
201 else{
202 if(first!=-1){
203 SQInteger c = first;
204 exp->_nodes[chain].next = c;
205 chain = c;
206 first = sqstd_rex_charnode(exp,SQTrue);
207 }
208 else{
209 first = sqstd_rex_charnode(exp,SQTrue);
210 }
211 }
212 }
213 if(first!=-1){
214 SQInteger c = first;
215 exp->_nodes[chain].next = c;
216 }
217 /* hack? */
218 exp->_nodes[ret].left = exp->_nodes[ret].next;
219 exp->_nodes[ret].next = -1;
220 return ret;
221 }
222
sqstd_rex_parsenumber(SQRex * exp)223 static SQInteger sqstd_rex_parsenumber(SQRex *exp)
224 {
225 SQInteger ret = *exp->_p-'0';
226 SQInteger positions = 10;
227 exp->_p++;
228 while(isdigit(*exp->_p)) {
229 ret = ret*10+(*exp->_p++-'0');
230 if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
231 positions *= 10;
232 };
233 return ret;
234 }
235
sqstd_rex_element(SQRex * exp)236 static SQInteger sqstd_rex_element(SQRex *exp)
237 {
238 SQInteger ret = -1;
239 switch(*exp->_p)
240 {
241 case '(': {
242 SQInteger expr;
243 exp->_p++;
244
245
246 if(*exp->_p =='?') {
247 exp->_p++;
248 sqstd_rex_expect(exp,':');
249 expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
250 }
251 else
252 expr = sqstd_rex_newnode(exp,OP_EXPR);
253 SQInteger newn = sqstd_rex_list(exp);
254 exp->_nodes[expr].left = newn;
255 ret = expr;
256 sqstd_rex_expect(exp,')');
257 }
258 break;
259 case '[':
260 exp->_p++;
261 ret = sqstd_rex_class(exp);
262 sqstd_rex_expect(exp,']');
263 break;
264 case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
265 case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
266 default:
267 ret = sqstd_rex_charnode(exp,SQFalse);
268 break;
269 }
270
271
272 SQBool isgreedy = SQFalse;
273 unsigned short p0 = 0, p1 = 0;
274 switch(*exp->_p){
275 case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
276 case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
277 case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = SQTrue; break;
278 case '{':
279 exp->_p++;
280 if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected"));
281 p0 = (unsigned short)sqstd_rex_parsenumber(exp);
282 /*******************************/
283 switch(*exp->_p) {
284 case '}':
285 p1 = p0; exp->_p++;
286 break;
287 case ',':
288 exp->_p++;
289 p1 = 0xFFFF;
290 if(isdigit(*exp->_p)){
291 p1 = (unsigned short)sqstd_rex_parsenumber(exp);
292 }
293 sqstd_rex_expect(exp,'}');
294 break;
295 default:
296 sqstd_rex_error(exp,_SC(", or } expected"));
297 }
298 /*******************************/
299 isgreedy = SQTrue;
300 break;
301
302 }
303 if(isgreedy) {
304 SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY);
305 exp->_nodes[nnode].left = ret;
306 exp->_nodes[nnode].right = ((p0)<<16)|p1;
307 ret = nnode;
308 }
309
310 if((*exp->_p != SQREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != SQREX_SYMBOL_GREEDY_ZERO_OR_MORE) && (*exp->_p != SQREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) {
311 SQInteger nnode = sqstd_rex_element(exp);
312 exp->_nodes[ret].next = nnode;
313 }
314
315 return ret;
316 }
317
sqstd_rex_list(SQRex * exp)318 static SQInteger sqstd_rex_list(SQRex *exp)
319 {
320 SQInteger ret=-1,e;
321 if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
322 exp->_p++;
323 ret = sqstd_rex_newnode(exp,OP_BOL);
324 }
325 e = sqstd_rex_element(exp);
326 if(ret != -1) {
327 exp->_nodes[ret].next = e;
328 }
329 else ret = e;
330
331 if(*exp->_p == SQREX_SYMBOL_BRANCH) {
332 SQInteger temp,tright;
333 exp->_p++;
334 temp = sqstd_rex_newnode(exp,OP_OR);
335 exp->_nodes[temp].left = ret;
336 tright = sqstd_rex_list(exp);
337 exp->_nodes[temp].right = tright;
338 ret = temp;
339 }
340 return ret;
341 }
342
sqstd_rex_matchcclass(SQInteger cclass,SQChar c)343 static SQBool sqstd_rex_matchcclass(SQInteger cclass,SQChar c)
344 {
345 switch(cclass) {
346 case 'a': return isalpha(c)?SQTrue:SQFalse;
347 case 'A': return !isalpha(c)?SQTrue:SQFalse;
348 case 'w': return (isalnum(c) || c == '_')?SQTrue:SQFalse;
349 case 'W': return (!isalnum(c) && c != '_')?SQTrue:SQFalse;
350 case 's': return isspace(c)?SQTrue:SQFalse;
351 case 'S': return !isspace(c)?SQTrue:SQFalse;
352 case 'd': return isdigit(c)?SQTrue:SQFalse;
353 case 'D': return !isdigit(c)?SQTrue:SQFalse;
354 case 'x': return isxdigit(c)?SQTrue:SQFalse;
355 case 'X': return !isxdigit(c)?SQTrue:SQFalse;
356 case 'c': return iscntrl(c)?SQTrue:SQFalse;
357 case 'C': return !iscntrl(c)?SQTrue:SQFalse;
358 case 'p': return ispunct(c)?SQTrue:SQFalse;
359 case 'P': return !ispunct(c)?SQTrue:SQFalse;
360 case 'l': return islower(c)?SQTrue:SQFalse;
361 case 'u': return isupper(c)?SQTrue:SQFalse;
362 }
363 return SQFalse; /*cannot happen*/
364 }
365
sqstd_rex_matchclass(SQRex * exp,SQRexNode * node,SQChar c)366 static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
367 {
368 do {
369 switch(node->type) {
370 case OP_RANGE:
371 if(c >= node->left && c <= node->right) return SQTrue;
372 break;
373 case OP_CCLASS:
374 if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
375 break;
376 default:
377 if(c == node->type)return SQTrue;
378 }
379 } while((node->next != -1) && (node = &exp->_nodes[node->next]));
380 return SQFalse;
381 }
382
sqstd_rex_matchnode(SQRex * exp,SQRexNode * node,const SQChar * str,SQRexNode * next)383 static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *next)
384 {
385
386 SQRexNodeType type = node->type;
387 switch(type) {
388 case OP_GREEDY: {
389 //SQRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
390 SQRexNode *greedystop = NULL;
391 SQInteger p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
392 const SQChar *s=str, *good = str;
393
394 if(node->next != -1) {
395 greedystop = &exp->_nodes[node->next];
396 }
397 else {
398 greedystop = next;
399 }
400
401 while((nmaches == 0xFFFF || nmaches < p1)) {
402
403 const SQChar *stop;
404 if(!(s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
405 break;
406 nmaches++;
407 good=s;
408 if(greedystop) {
409 //checks that 0 matches satisfy the expression(if so skips)
410 //if not would always stop(for instance if is a '?')
411 if(greedystop->type != OP_GREEDY ||
412 (greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0))
413 {
414 SQRexNode *gnext = NULL;
415 if(greedystop->next != -1) {
416 gnext = &exp->_nodes[greedystop->next];
417 }else if(next && next->next != -1){
418 gnext = &exp->_nodes[next->next];
419 }
420 stop = sqstd_rex_matchnode(exp,greedystop,s,gnext);
421 if(stop) {
422 //if satisfied stop it
423 if(p0 == p1 && p0 == nmaches) break;
424 else if(nmaches >= p0 && p1 == 0xFFFF) break;
425 else if(nmaches >= p0 && nmaches <= p1) break;
426 }
427 }
428 }
429
430 if(s >= exp->_eol)
431 break;
432 }
433 if(p0 == p1 && p0 == nmaches) return good;
434 else if(nmaches >= p0 && p1 == 0xFFFF) return good;
435 else if(nmaches >= p0 && nmaches <= p1) return good;
436 return NULL;
437 }
438 case OP_OR: {
439 const SQChar *asd = str;
440 SQRexNode *temp=&exp->_nodes[node->left];
441 while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
442 if(temp->next != -1)
443 temp = &exp->_nodes[temp->next];
444 else
445 return asd;
446 }
447 asd = str;
448 temp = &exp->_nodes[node->right];
449 while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
450 if(temp->next != -1)
451 temp = &exp->_nodes[temp->next];
452 else
453 return asd;
454 }
455 return NULL;
456 break;
457 }
458 case OP_EXPR:
459 case OP_NOCAPEXPR:{
460 SQRexNode *n = &exp->_nodes[node->left];
461 const SQChar *cur = str;
462 SQInteger capture = -1;
463 if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
464 capture = exp->_currsubexp;
465 exp->_matches[capture].begin = cur;
466 exp->_currsubexp++;
467 }
468 SQInteger tempcap = exp->_currsubexp;
469 do {
470 SQRexNode *subnext = NULL;
471 if(n->next != -1) {
472 subnext = &exp->_nodes[n->next];
473 }else {
474 subnext = next;
475 }
476 if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) {
477 if(capture != -1){
478 exp->_matches[capture].begin = 0;
479 exp->_matches[capture].len = 0;
480 }
481 return NULL;
482 }
483 } while((n->next != -1) && (n = &exp->_nodes[n->next]));
484
485 exp->_currsubexp = tempcap;
486 if(capture != -1)
487 exp->_matches[capture].len = cur - exp->_matches[capture].begin;
488 return cur;
489 }
490 case OP_WB:
491 if((str == exp->_bol && !isspace(*str))
492 || (str == exp->_eol && !isspace(*(str-1)))
493 || (!isspace(*str) && isspace(*(str+1)))
494 || (isspace(*str) && !isspace(*(str+1))) ) {
495 return (node->left == 'b')?str:NULL;
496 }
497 return (node->left == 'b')?NULL:str;
498 case OP_BOL:
499 if(str == exp->_bol) return str;
500 return NULL;
501 case OP_EOL:
502 if(str == exp->_eol) return str;
503 return NULL;
504 case OP_DOT:{
505 if (str == exp->_eol) return NULL;
506 str++;
507 }
508 return str;
509 case OP_NCLASS:
510 case OP_CLASS:
511 if (str == exp->_eol) return NULL;
512 if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
513 str++;
514 return str;
515 }
516 return NULL;
517 case OP_CCLASS:
518 if (str == exp->_eol) return NULL;
519 if(sqstd_rex_matchcclass(node->left,*str)) {
520 str++;
521 return str;
522 }
523 return NULL;
524 case OP_MB:
525 {
526 SQInteger cb = node->left; //char that opens a balanced expression
527 if(*str != cb) return NULL; // string doesnt start with open char
528 SQInteger ce = node->right; //char that closes a balanced expression
529 SQInteger cont = 1;
530 const SQChar *streol = exp->_eol;
531 while (++str < streol) {
532 if (*str == ce) {
533 if (--cont == 0) {
534 return ++str;
535 }
536 }
537 else if (*str == cb) cont++;
538 }
539 }
540 return NULL; // string ends out of balance
541 default: /* char */
542 if (str == exp->_eol) return NULL;
543 if(*str != node->type) return NULL;
544 str++;
545 return str;
546 }
547 return NULL;
548 }
549
550 /* public api */
sqstd_rex_compile(const SQChar * pattern,const SQChar ** error)551 SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
552 {
553 SQRex * volatile exp = (SQRex *)sq_malloc(sizeof(SQRex)); // "volatile" is needed for setjmp()
554 exp->_eol = exp->_bol = NULL;
555 exp->_p = pattern;
556 exp->_nallocated = (SQInteger)scstrlen(pattern) * sizeof(SQChar);
557 exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
558 exp->_nsize = 0;
559 exp->_matches = 0;
560 exp->_nsubexpr = 0;
561 exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
562 exp->_error = error;
563 exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
564 if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
565 SQInteger res = sqstd_rex_list(exp);
566 exp->_nodes[exp->_first].left = res;
567 if(*exp->_p!='\0')
568 sqstd_rex_error(exp,_SC("unexpected character"));
569 #ifdef _DEBUG
570 {
571 SQInteger nsize,i;
572 SQRexNode *t;
573 nsize = exp->_nsize;
574 t = &exp->_nodes[0];
575 scprintf(_SC("\n"));
576 for(i = 0;i < nsize; i++) {
577 if(exp->_nodes[i].type>MAX_CHAR)
578 scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
579 else
580 scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type);
581 scprintf(_SC("left %02d right %02d next %02d\n"), (SQInt32)exp->_nodes[i].left, (SQInt32)exp->_nodes[i].right, (SQInt32)exp->_nodes[i].next);
582 }
583 scprintf(_SC("\n"));
584 }
585 #endif
586 exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
587 memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
588 }
589 else{
590 sqstd_rex_free(exp);
591 return NULL;
592 }
593 return exp;
594 }
595
sqstd_rex_free(SQRex * exp)596 void sqstd_rex_free(SQRex *exp)
597 {
598 if(exp) {
599 if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
600 if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
601 if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
602 sq_free(exp,sizeof(SQRex));
603 }
604 }
605
sqstd_rex_match(SQRex * exp,const SQChar * text)606 SQBool sqstd_rex_match(SQRex* exp,const SQChar* text)
607 {
608 const SQChar* res = NULL;
609 exp->_bol = text;
610 exp->_eol = text + scstrlen(text);
611 exp->_currsubexp = 0;
612 res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL);
613 if(res == NULL || res != exp->_eol)
614 return SQFalse;
615 return SQTrue;
616 }
617
sqstd_rex_searchrange(SQRex * exp,const SQChar * text_begin,const SQChar * text_end,const SQChar ** out_begin,const SQChar ** out_end)618 SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
619 {
620 const SQChar *cur = NULL;
621 SQInteger node = exp->_first;
622 if(text_begin >= text_end) return SQFalse;
623 exp->_bol = text_begin;
624 exp->_eol = text_end;
625 do {
626 cur = text_begin;
627 while(node != -1) {
628 exp->_currsubexp = 0;
629 cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL);
630 if(!cur)
631 break;
632 node = exp->_nodes[node].next;
633 }
634 text_begin++;
635 } while(cur == NULL && text_begin != text_end);
636
637 if(cur == NULL)
638 return SQFalse;
639
640 --text_begin;
641
642 if(out_begin) *out_begin = text_begin;
643 if(out_end) *out_end = cur;
644 return SQTrue;
645 }
646
sqstd_rex_search(SQRex * exp,const SQChar * text,const SQChar ** out_begin,const SQChar ** out_end)647 SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
648 {
649 return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
650 }
651
sqstd_rex_getsubexpcount(SQRex * exp)652 SQInteger sqstd_rex_getsubexpcount(SQRex* exp)
653 {
654 return exp->_nsubexpr;
655 }
656
sqstd_rex_getsubexp(SQRex * exp,SQInteger n,SQRexMatch * subexp)657 SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp)
658 {
659 if( n<0 || n >= exp->_nsubexpr) return SQFalse;
660 *subexp = exp->_matches[n];
661 return SQTrue;
662 }
663
664