sqstdrex.cpp 18 KB

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