sqstdrex.cpp 17 KB

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