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