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