sqstdrex.cpp 20 KB

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