sqstdrex.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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"),_SC("OP_MB")
  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 OP_MB (MAX_CHAR+14) //match balanced
  36. #define OP_EMPTY (MAX_CHAR+15) //match position
  37. #define SQREX_SYMBOL_ANY_CHAR ('.')
  38. #define SQREX_SYMBOL_GREEDY_ONE_OR_MORE ('+')
  39. #define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*')
  40. #define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?')
  41. #define SQREX_SYMBOL_BRANCH ('|')
  42. #define SQREX_SYMBOL_END_OF_STRING ('$')
  43. #define SQREX_SYMBOL_BEGINNING_OF_STRING ('^')
  44. #define SQREX_SYMBOL_ESCAPE_CHAR ('\\')
  45. typedef int SQRexNodeType;
  46. typedef struct tagSQRexNode{
  47. SQRexNodeType type;
  48. SQInteger left;
  49. SQInteger right;
  50. SQInteger next;
  51. }SQRexNode;
  52. struct SQRex{
  53. const SQChar *_eol;
  54. const SQChar *_bol;
  55. const SQChar *_p;
  56. SQInteger _first;
  57. SQInteger _op;
  58. SQRexNode *_nodes;
  59. SQInteger _nallocated;
  60. SQInteger _nsize;
  61. SQInteger _nsubexpr;
  62. SQRexMatch *_matches;
  63. SQInteger _currsubexp;
  64. void *_jmpbuf;
  65. const SQChar **_error;
  66. };
  67. static SQInteger sqstd_rex_list(SQRex *exp);
  68. static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
  69. {
  70. SQRexNode n;
  71. n.type = type;
  72. n.next = n.right = n.left = -1;
  73. if((type == OP_EXPR) || (type == OP_EMPTY))
  74. n.right = exp->_nsubexpr++;
  75. if(exp->_nallocated < (exp->_nsize + 1)) {
  76. SQInteger oldsize = exp->_nallocated;
  77. exp->_nallocated *= 2;
  78. exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
  79. }
  80. exp->_nodes[exp->_nsize++] = n;
  81. SQInteger newid = exp->_nsize - 1;
  82. return (SQInteger)newid;
  83. }
  84. static void sqstd_rex_error(SQRex *exp,const SQChar *error)
  85. {
  86. if(exp->_error) *exp->_error = error;
  87. longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
  88. }
  89. static void sqstd_rex_expect(SQRex *exp, SQInteger n){
  90. if((*exp->_p) != n)
  91. sqstd_rex_error(exp, _SC("expected paren"));
  92. exp->_p++;
  93. }
  94. static SQChar sqstd_rex_escapechar(SQRex *exp)
  95. {
  96. if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
  97. exp->_p++;
  98. switch(*exp->_p) {
  99. case 'v': exp->_p++; return '\v';
  100. case 'n': exp->_p++; return '\n';
  101. case 't': exp->_p++; return '\t';
  102. case 'r': exp->_p++; return '\r';
  103. case 'f': exp->_p++; return '\f';
  104. default: return (*exp->_p++);
  105. }
  106. } else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
  107. return (*exp->_p++);
  108. }
  109. static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid)
  110. {
  111. SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS);
  112. exp->_nodes[n].left = classid;
  113. return n;
  114. }
  115. static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass)
  116. {
  117. SQChar t;
  118. if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
  119. exp->_p++;
  120. switch(*exp->_p) {
  121. case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
  122. case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
  123. case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
  124. case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
  125. case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
  126. case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
  127. case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
  128. case 'p': case 'P': case 'l': case 'u':
  129. {
  130. t = *exp->_p; exp->_p++;
  131. return sqstd_rex_charclass(exp,t);
  132. }
  133. case 'm':
  134. {
  135. SQChar cb, ce; //cb = character begin match ce = character end match
  136. cb = *++exp->_p; //skip 'm'
  137. ce = *++exp->_p;
  138. exp->_p++; //points to the next char to be parsed
  139. if ((!cb) || (!ce)) sqstd_rex_error(exp,_SC("balanced chars expected"));
  140. if ( cb == ce ) sqstd_rex_error(exp,_SC("open/close char can't be the same"));
  141. SQInteger node = sqstd_rex_newnode(exp,OP_MB);
  142. exp->_nodes[node].left = cb;
  143. exp->_nodes[node].right = ce;
  144. return node;
  145. }
  146. case 'b':
  147. case 'B':
  148. if(!isclass) {
  149. SQInteger node = sqstd_rex_newnode(exp,OP_WB);
  150. exp->_nodes[node].left = *exp->_p;
  151. exp->_p++;
  152. return node;
  153. } //else default
  154. default:
  155. t = *exp->_p; exp->_p++;
  156. return sqstd_rex_newnode(exp,t);
  157. }
  158. }
  159. //else if(!scisprint(*exp->_p)) {
  160. else if(((SQUChar)*exp->_p) < ' ') {
  161. sqstd_rex_error(exp,_SC("letter expected"));
  162. }
  163. t = *exp->_p; exp->_p++;
  164. return sqstd_rex_newnode(exp,t);
  165. }
  166. static SQInteger sqstd_rex_class(SQRex *exp)
  167. {
  168. SQInteger ret = -1;
  169. SQInteger first = -1,chain;
  170. if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
  171. ret = sqstd_rex_newnode(exp,OP_NCLASS);
  172. exp->_p++;
  173. }else ret = sqstd_rex_newnode(exp,OP_CLASS);
  174. if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class"));
  175. chain = ret;
  176. while(*exp->_p != ']' && exp->_p != exp->_eol) {
  177. if(*exp->_p == '-' && first != -1){
  178. SQInteger r;
  179. if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
  180. r = sqstd_rex_newnode(exp,OP_RANGE);
  181. if(exp->_nodes[first].type>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
  182. if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
  183. exp->_nodes[r].left = exp->_nodes[first].type;
  184. SQInteger t = sqstd_rex_escapechar(exp);
  185. exp->_nodes[r].right = t;
  186. exp->_nodes[chain].next = r;
  187. chain = r;
  188. first = -1;
  189. }
  190. else{
  191. if(first!=-1){
  192. SQInteger c = first;
  193. exp->_nodes[chain].next = c;
  194. chain = c;
  195. first = sqstd_rex_charnode(exp,SQTrue);
  196. }
  197. else{
  198. first = sqstd_rex_charnode(exp,SQTrue);
  199. }
  200. }
  201. }
  202. if(first!=-1){
  203. SQInteger c = first;
  204. exp->_nodes[chain].next = c;
  205. }
  206. /* hack? */
  207. exp->_nodes[ret].left = exp->_nodes[ret].next;
  208. exp->_nodes[ret].next = -1;
  209. return ret;
  210. }
  211. static SQInteger sqstd_rex_parsenumber(SQRex *exp)
  212. {
  213. SQInteger ret = *exp->_p-'0';
  214. SQInteger positions = 10;
  215. exp->_p++;
  216. while(isdigit(*exp->_p)) {
  217. ret = ret*10+(*exp->_p++-'0');
  218. if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
  219. positions *= 10;
  220. };
  221. return ret;
  222. }
  223. static SQInteger sqstd_rex_element(SQRex *exp)
  224. {
  225. SQInteger ret = -1;
  226. switch(*exp->_p)
  227. {
  228. case '(': {
  229. SQInteger expr;
  230. exp->_p++;
  231. if(*exp->_p =='?') {
  232. exp->_p++;
  233. sqstd_rex_expect(exp,':');
  234. expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
  235. }
  236. else if(*exp->_p ==')')
  237. {
  238. exp->_p++;
  239. expr = sqstd_rex_newnode(exp,OP_EMPTY);
  240. if(*exp->_p !='\0')
  241. {
  242. SQInteger newn = sqstd_rex_list(exp);
  243. exp->_nodes[expr].next = newn;
  244. }
  245. ret = expr;
  246. break;
  247. }
  248. else
  249. expr = sqstd_rex_newnode(exp,OP_EXPR);
  250. SQInteger newn = sqstd_rex_list(exp);
  251. exp->_nodes[expr].left = newn;
  252. ret = expr;
  253. sqstd_rex_expect(exp,')');
  254. }
  255. break;
  256. case '[':
  257. exp->_p++;
  258. ret = sqstd_rex_class(exp);
  259. sqstd_rex_expect(exp,']');
  260. break;
  261. case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
  262. case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
  263. default:
  264. ret = sqstd_rex_charnode(exp,SQFalse);
  265. break;
  266. }
  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. if(isgreedy) {
  298. SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY);
  299. exp->_nodes[nnode].left = ret;
  300. exp->_nodes[nnode].right = ((p0)<<16)|p1;
  301. ret = nnode;
  302. }
  303. 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')) {
  304. SQInteger nnode = sqstd_rex_element(exp);
  305. exp->_nodes[ret].next = nnode;
  306. }
  307. return ret;
  308. }
  309. static SQInteger sqstd_rex_list(SQRex *exp)
  310. {
  311. SQInteger ret=-1,e;
  312. if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
  313. exp->_p++;
  314. ret = sqstd_rex_newnode(exp,OP_BOL);
  315. }
  316. e = sqstd_rex_element(exp);
  317. if(ret != -1) {
  318. exp->_nodes[ret].next = e;
  319. }
  320. else ret = e;
  321. if(*exp->_p == SQREX_SYMBOL_BRANCH) {
  322. SQInteger temp,tright;
  323. exp->_p++;
  324. temp = sqstd_rex_newnode(exp,OP_OR);
  325. exp->_nodes[temp].left = ret;
  326. tright = sqstd_rex_list(exp);
  327. exp->_nodes[temp].right = tright;
  328. ret = temp;
  329. }
  330. return ret;
  331. }
  332. static inline bool isChClassWord(int c)
  333. {
  334. return (isalnum(c) || c == '_');
  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 isChClassWord(c)?SQTrue:SQFalse;
  342. case 'W': return !isChClassWord(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. static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
  359. {
  360. do {
  361. switch(node->type) {
  362. case OP_RANGE:
  363. if(c >= node->left && c <= node->right) return SQTrue;
  364. break;
  365. case OP_CCLASS:
  366. if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
  367. break;
  368. default:
  369. if(c == node->type)return SQTrue;
  370. }
  371. } while((node->next != -1) && (node = &exp->_nodes[node->next]));
  372. return SQFalse;
  373. }
  374. static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *next)
  375. {
  376. SQRexNodeType type = node->type;
  377. switch(type) {
  378. case OP_GREEDY: {
  379. //SQRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
  380. SQRexNode *greedystop = NULL;
  381. SQInteger p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmatches = 0;
  382. const SQChar *s=str, *good = str;
  383. if(node->next != -1) {
  384. greedystop = &exp->_nodes[node->next];
  385. }
  386. else {
  387. greedystop = next;
  388. }
  389. while((nmatches == 0xFFFF || nmatches < p1)) {
  390. const SQChar *stop, *last_match = s;
  391. SQInteger while_nmatches = 0;
  392. while((last_match = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
  393. {
  394. while_nmatches++;
  395. s = last_match;
  396. if(while_nmatches == p1) break;
  397. }
  398. if(!while_nmatches) break;
  399. nmatches += while_nmatches;
  400. good=s;
  401. if(greedystop && !sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*s)) {
  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 == nmatches) break;
  417. else if(nmatches >= p0 && p1 == 0xFFFF) break;
  418. else if(nmatches >= p0 && nmatches <= p1) break;
  419. }
  420. }
  421. }
  422. if(s >= exp->_eol)
  423. break;
  424. }
  425. if(p0 == p1 && p0 == nmatches) return good;
  426. else if(nmatches >= p0 && p1 == 0xFFFF) return good;
  427. else if(nmatches >= p0 && nmatches <= p1) return good;
  428. return NULL;
  429. }
  430. case OP_OR: {
  431. const SQChar *asd = str;
  432. SQRexNode *temp=&exp->_nodes[node->left];
  433. while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
  434. if(temp->next != -1)
  435. temp = &exp->_nodes[temp->next];
  436. else
  437. return asd;
  438. }
  439. asd = str;
  440. temp = &exp->_nodes[node->right];
  441. while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
  442. if(temp->next != -1)
  443. temp = &exp->_nodes[temp->next];
  444. else
  445. return asd;
  446. }
  447. return NULL;
  448. break;
  449. }
  450. case OP_EMPTY: //zero length capture
  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. if(type == OP_EMPTY)
  461. {
  462. exp->_matches[capture].len = -1;
  463. return cur;
  464. }
  465. }
  466. SQInteger tempcap = exp->_currsubexp;
  467. do {
  468. SQRexNode *subnext = NULL;
  469. if(n->next != -1) {
  470. subnext = &exp->_nodes[n->next];
  471. }else {
  472. subnext = next;
  473. }
  474. if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) {
  475. if(capture != -1){
  476. exp->_matches[capture].begin = 0;
  477. exp->_matches[capture].len = 0;
  478. }
  479. return NULL;
  480. }
  481. } while((n->next != -1) && (n = &exp->_nodes[n->next]));
  482. exp->_currsubexp = tempcap;
  483. if(capture != -1)
  484. exp->_matches[capture].len = cur - exp->_matches[capture].begin;
  485. return cur;
  486. }
  487. case OP_WB:
  488. if((str == exp->_bol && isChClassWord(*str))
  489. || (str == exp->_eol && isChClassWord(*(str-1)))
  490. || (isChClassWord(*str) != isChClassWord(*(str-1))) ) {
  491. return (node->left == 'b')?str:NULL;
  492. }
  493. return (node->left == 'b')?NULL:str;
  494. case OP_BOL:
  495. if(str == exp->_bol) return str;
  496. return NULL;
  497. case OP_EOL:
  498. if(str == exp->_eol) return str;
  499. return NULL;
  500. case OP_DOT:{
  501. if (str == exp->_eol) return NULL;
  502. str++;
  503. }
  504. return str;
  505. case OP_NCLASS:
  506. case OP_CLASS:
  507. if (str == exp->_eol) return NULL;
  508. if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
  509. str++;
  510. return str;
  511. }
  512. return NULL;
  513. case OP_CCLASS:
  514. if (str == exp->_eol) return NULL;
  515. if(sqstd_rex_matchcclass(node->left,*str)) {
  516. str++;
  517. return str;
  518. }
  519. return NULL;
  520. case OP_MB:
  521. {
  522. SQInteger cb = node->left; //char that opens a balanced expression
  523. if(*str != cb) return NULL; // string doesnt start with open char
  524. SQInteger ce = node->right; //char that closes a balanced expression
  525. SQInteger cont = 1;
  526. const SQChar *streol = exp->_eol;
  527. while (++str < streol) {
  528. if (*str == ce) {
  529. if (--cont == 0) {
  530. return ++str;
  531. }
  532. }
  533. else if (*str == cb) cont++;
  534. }
  535. }
  536. return NULL; // string ends out of balance
  537. default: /* char */
  538. if (str == exp->_eol) return NULL;
  539. if(*str != node->type) return NULL;
  540. str++;
  541. return str;
  542. }
  543. return NULL;
  544. }
  545. /* public api */
  546. SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
  547. {
  548. SQInteger plen = (SQInteger)scstrlen(pattern) * sizeof(SQChar);
  549. if(plen == 0)
  550. {
  551. *error = _SC("empty pattern");
  552. return NULL;
  553. }
  554. SQRex * volatile exp = (SQRex *)sq_malloc(sizeof(SQRex)); // "volatile" is needed for setjmp()
  555. exp->_eol = exp->_bol = NULL;
  556. exp->_p = pattern;
  557. exp->_nallocated = plen;
  558. exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
  559. exp->_nsize = 0;
  560. exp->_matches = 0;
  561. exp->_nsubexpr = 0;
  562. exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
  563. exp->_error = error;
  564. exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
  565. if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
  566. SQInteger res = sqstd_rex_list(exp);
  567. exp->_nodes[exp->_first].left = res;
  568. if(*exp->_p!='\0')
  569. sqstd_rex_error(exp,_SC("unexpected character"));
  570. #ifdef _DEBUG
  571. {
  572. SQInteger nsize,i;
  573. SQRexNode *t;
  574. nsize = exp->_nsize;
  575. t = &exp->_nodes[0];
  576. scprintf(_SC("\n"));
  577. for(i = 0;i < nsize; i++) {
  578. if(exp->_nodes[i].type>MAX_CHAR)
  579. scprintf(_SC("[%02d] %10s "),(SQInt32)i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
  580. else
  581. scprintf(_SC("[%02d] %10c "),(SQInt32)i,exp->_nodes[i].type);
  582. scprintf(_SC("left %02d right %02d next %02d\n"), (SQInt32)exp->_nodes[i].left, (SQInt32)exp->_nodes[i].right, (SQInt32)exp->_nodes[i].next);
  583. }
  584. scprintf(_SC("\n"));
  585. }
  586. #endif
  587. exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
  588. memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
  589. }
  590. else{
  591. sqstd_rex_free(exp);
  592. return NULL;
  593. }
  594. return exp;
  595. }
  596. void sqstd_rex_free(SQRex *exp)
  597. {
  598. if(exp) {
  599. if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
  600. if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
  601. if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
  602. sq_free(exp,sizeof(SQRex));
  603. }
  604. }
  605. SQBool sqstd_rex_match(SQRex* exp,const SQChar* text, SQInteger text_size)
  606. {
  607. const SQChar* res = NULL;
  608. exp->_bol = text;
  609. exp->_eol = text + ((text_size < 0) ? scstrlen(text) : text_size);
  610. exp->_currsubexp = 0;
  611. res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL);
  612. if(res == NULL || res != exp->_eol)
  613. return SQFalse;
  614. return SQTrue;
  615. }
  616. SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
  617. {
  618. const SQChar *cur = NULL;
  619. SQInteger node = exp->_first;
  620. if(text_begin >= text_end) return SQFalse;
  621. exp->_bol = text_begin;
  622. exp->_eol = text_end;
  623. do {
  624. cur = text_begin;
  625. while(node != -1) {
  626. exp->_currsubexp = 0;
  627. cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL);
  628. if(!cur)
  629. break;
  630. node = exp->_nodes[node].next;
  631. }
  632. text_begin++;
  633. } while(cur == NULL && text_begin != text_end);
  634. if(cur == NULL)
  635. return SQFalse;
  636. --text_begin;
  637. if(out_begin) *out_begin = text_begin;
  638. if(out_end) *out_end = cur;
  639. return SQTrue;
  640. }
  641. SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
  642. {
  643. return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
  644. }
  645. SQInteger sqstd_rex_getsubexpcount(SQRex* exp)
  646. {
  647. return exp->_nsubexpr;
  648. }
  649. SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp)
  650. {
  651. if( n<0 || n >= exp->_nsubexpr) return SQFalse;
  652. *subexp = exp->_matches[n];
  653. return SQTrue;
  654. }