sqstdrex.cpp 17 KB

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