Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

640 lines
17 KiB

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