lua-regex.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /******************************************************************************
  2. * Copyright (C) 1994-2008 Lua.org, PUC-Rio. All rights reserved.
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. ******************************************************************************/
  23. /*
  24. Adapted by Domingo Alvarez Duarte 2012
  25. http://code.google.com/p/lua-regex-standalone/
  26. */
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30. #include <ctype.h>
  31. #include "lua-regex.h"
  32. /* macro to `unsign' a character */
  33. #define uchar(c) ((unsigned char)(c))
  34. #define L_ESC '%'
  35. #define SPECIALS "^$*+?.([%-"
  36. #define LUA_QL(x) "'" x "'"
  37. #define LUA_QS LUA_QL("%s")
  38. /* translate a relative string position: negative means back from end */
  39. static size_t posrelat (ptrdiff_t pos, size_t len) {
  40. if (pos >= 0) return (size_t)pos;
  41. else if (0u - (size_t)pos > len) return 0;
  42. else return len - ((size_t)-pos) + 1;
  43. }
  44. static int check_capture (LuaMatchState *ms, int *l_out) {
  45. int l;
  46. *l_out -= '1';
  47. l = *l_out;
  48. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED){
  49. ms->error = "invalid capture index";
  50. return 0;
  51. }
  52. return 1;
  53. }
  54. static int capture_to_close (LuaMatchState *ms, int *level_out) {
  55. int level = ms->level;
  56. for (level--; level>=0; level--)
  57. if (ms->capture[level].len == CAP_UNFINISHED) {
  58. *level_out = level;
  59. return 1;
  60. }
  61. ms->error = "invalid pattern capture";
  62. return 0;
  63. }
  64. static int classend (LuaMatchState *ms, const char *p, const char **result) {
  65. switch (*p++) {
  66. case L_ESC: {
  67. if (p == ms->p_end){
  68. ms->error = "malformed pattern (ends with " LUA_QL("%%") ")";
  69. return 0;
  70. }
  71. *result = p+1;
  72. return 1;
  73. }
  74. case '[': {
  75. if (*p == '^') p++;
  76. do { /* look for a `]' */
  77. if (p == ms->p_end){
  78. ms->error = "malformed pattern (missing " LUA_QL("]") ")";
  79. return 0;
  80. }
  81. if (*(p++) == L_ESC && p < ms->p_end)
  82. p++; /* skip escapes (e.g. `%]') */
  83. } while (*p != ']');
  84. *result = p+1;
  85. return 1;
  86. }
  87. default: {
  88. *result = p;
  89. return 1;
  90. }
  91. }
  92. }
  93. static int match_class (int c, int cl) {
  94. int res;
  95. switch (tolower(cl)) {
  96. case 'a' : res = isalpha(c); break;
  97. case 'c' : res = iscntrl(c); break;
  98. case 'd' : res = isdigit(c); break;
  99. case 'g' : res = isgraph(c); break;
  100. case 'l' : res = islower(c); break;
  101. case 'p' : res = ispunct(c); break;
  102. case 's' : res = isspace(c); break;
  103. case 'u' : res = isupper(c); break;
  104. case 'w' : res = isalnum(c); break;
  105. case 'x' : res = isxdigit(c); break;
  106. case 'z' : res = (c == 0); break; /* deprecated option */
  107. default: return (cl == c);
  108. }
  109. return (islower(cl) ? res : !res);
  110. }
  111. static int matchbracketclass (int c, const char *p, const char *ec) {
  112. int sig = 1;
  113. if (*(p+1) == '^') {
  114. sig = 0;
  115. p++; /* skip the `^' */
  116. }
  117. while (++p < ec) {
  118. if (*p == L_ESC) {
  119. p++;
  120. if (match_class(c, uchar(*p)))
  121. return sig;
  122. }
  123. else if ((*(p+1) == '-') && (p+2 < ec)) {
  124. p+=2;
  125. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  126. return sig;
  127. }
  128. else if (uchar(*p) == c) return sig;
  129. }
  130. return !sig;
  131. }
  132. static int singlematch (int c, const char *p, const char *ep) {
  133. switch (*p) {
  134. case '.': return 1; /* matches any char */
  135. case L_ESC: return match_class(c, uchar(*(p+1)));
  136. case '[': return matchbracketclass(c, p, ep-1);
  137. default: return (uchar(*p) == c);
  138. }
  139. }
  140. static const char *match (LuaMatchState *ms, const char *s, const char *p);
  141. static const char *matchbalance (LuaMatchState *ms, const char *s,
  142. const char *p) {
  143. if (p >= ms->p_end - 1){
  144. ms->error = "malformed pattern "
  145. "(missing arguments to " LUA_QL("%%b") ")";
  146. return NULL;
  147. }
  148. if (*s != *p) return NULL;
  149. else {
  150. int b = *p;
  151. int e = *(p+1);
  152. int cont = 1;
  153. while (++s < ms->src_end) {
  154. if (*s == e) {
  155. if (--cont == 0) return s+1;
  156. }
  157. else if (*s == b) cont++;
  158. }
  159. }
  160. return NULL; /* string ends out of balance */
  161. }
  162. static const char *max_expand (LuaMatchState *ms, const char *s,
  163. const char *p, const char *ep) {
  164. ptrdiff_t i = 0; /* counts maximum expand for item */
  165. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
  166. i++;
  167. /* keeps trying to match with the maximum repetitions */
  168. while (i>=0) {
  169. const char *res = match(ms, (s+i), ep+1);
  170. if (res) return res;
  171. i--; /* else didn't match; reduce 1 repetition to try again */
  172. }
  173. return NULL;
  174. }
  175. static const char *min_expand (LuaMatchState *ms, const char *s,
  176. const char *p, const char *ep) {
  177. for (;;) {
  178. const char *res = match(ms, s, ep+1);
  179. if (res != NULL)
  180. return res;
  181. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
  182. s++; /* try with one more repetition */
  183. else return NULL;
  184. }
  185. }
  186. static const char *start_capture (LuaMatchState *ms, const char *s,
  187. const char *p, int what) {
  188. const char *res;
  189. int level = ms->level;
  190. if (level >= LUA_REGEX_MAXCAPTURES) {
  191. ms->error = "too many captures";
  192. return NULL;
  193. }
  194. ms->capture[level].init = s;
  195. ms->capture[level].len = what;
  196. ms->level = level+1;
  197. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  198. ms->level--; /* undo capture */
  199. return res;
  200. }
  201. static const char *end_capture (LuaMatchState *ms, const char *s,
  202. const char *p) {
  203. int l;
  204. const char *res;
  205. if(!capture_to_close(ms, &l)) return NULL;
  206. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  207. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  208. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  209. return res;
  210. }
  211. static const char *match_capture (LuaMatchState *ms, const char *s, int l) {
  212. size_t len;
  213. if(check_capture(ms, &l)){
  214. len = ms->capture[l].len;
  215. if ((size_t)(ms->src_end-s) >= len &&
  216. memcmp(ms->capture[l].init, s, len) == 0)
  217. return s+len;
  218. }
  219. return NULL;
  220. }
  221. static const char *match (LuaMatchState *ms, const char *s, const char *p) {
  222. init: /* using goto's to optimize tail recursion */
  223. if (p == ms->p_end) /* end of pattern? */
  224. return s; /* match succeeded */
  225. switch (*p) {
  226. case '(': { /* start capture */
  227. if (*(p+1) == ')') /* position capture? */
  228. return start_capture(ms, s, p+2, CAP_POSITION);
  229. else
  230. return start_capture(ms, s, p+1, CAP_UNFINISHED);
  231. }
  232. case ')': { /* end capture */
  233. return end_capture(ms, s, p+1);
  234. }
  235. case '$': {
  236. if ((p+1) == ms->p_end) /* is the `$' the last char in pattern? */
  237. return (s == ms->src_end) ? s : NULL; /* check end of string */
  238. else goto dflt;
  239. }
  240. case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
  241. switch (*(p+1)) {
  242. case 'b': { /* balanced string? */
  243. s = matchbalance(ms, s, p+2);
  244. if (s == NULL) return NULL;
  245. p+=4; goto init; /* else return match(ms, s, p+4); */
  246. }
  247. case 'f': { /* frontier? */
  248. const char *ep; char previous;
  249. p += 2;
  250. if (*p != '['){
  251. ms->error = "missing " LUA_QL("[") " after "
  252. LUA_QL("%%f") " in pattern";
  253. return NULL;
  254. }
  255. if(!classend(ms, p, &ep)) return NULL; /* points to what is next */
  256. previous = (s == ms->src_init) ? '\0' : *(s-1);
  257. if (matchbracketclass(uchar(previous), p, ep-1) ||
  258. !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
  259. p=ep; goto init; /* else return match(ms, s, ep); */
  260. }
  261. case '0': case '1': case '2': case '3':
  262. case '4': case '5': case '6': case '7':
  263. case '8': case '9': { /* capture results (%0-%9)? */
  264. s = match_capture(ms, s, uchar(*(p+1)));
  265. if (s == NULL) return NULL;
  266. p+=2; goto init; /* else return match(ms, s, p+2) */
  267. }
  268. default: goto dflt;
  269. }
  270. }
  271. default: dflt: { /* pattern class plus optional suffix */
  272. const char *ep;
  273. int m;
  274. if(!classend(ms, p, &ep)) return NULL; /* points to what is next */
  275. m = s < ms->src_end && singlematch(uchar(*s), p, ep);
  276. switch (*ep) {
  277. case '?': { /* optional */
  278. const char *res;
  279. if (m && ((res=match(ms, s+1, ep+1)) != NULL))
  280. return res;
  281. p=ep+1; goto init; /* else return match(ms, s, ep+1); */
  282. }
  283. case '*': { /* 0 or more repetitions */
  284. return max_expand(ms, s, p, ep);
  285. }
  286. case '+': { /* 1 or more repetitions */
  287. return (m ? max_expand(ms, s+1, p, ep) : NULL);
  288. }
  289. case '-': { /* 0 or more repetitions (minimum) */
  290. return min_expand(ms, s, p, ep);
  291. }
  292. default: {
  293. if (!m) return NULL;
  294. s++; p=ep; goto init; /* else return match(ms, s+1, ep); */
  295. }
  296. }
  297. }
  298. }
  299. }
  300. static const char *lmemfind (const char *s1, size_t l1,
  301. const char *s2, size_t l2) {
  302. if (l2 == 0) return s1; /* empty strings are everywhere */
  303. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  304. else {
  305. const char *init; /* to search for a `*s2' inside `s1' */
  306. l2--; /* 1st char will be checked by `memchr' */
  307. l1 = l1-l2; /* `s2' cannot be found after that */
  308. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  309. init++; /* 1st char is already checked */
  310. if (memcmp(init, s2+1, l2) == 0)
  311. return init-1;
  312. else { /* correct `l1' and `s1' to try again */
  313. l1 -= init-s1;
  314. s1 = init;
  315. }
  316. }
  317. return NULL; /* not found */
  318. }
  319. }
  320. /* check whether pattern has no special characters */
  321. static int nospecials (const char *p, size_t l) {
  322. size_t upto = 0;
  323. do {
  324. if (strpbrk(p + upto, SPECIALS))
  325. return 0; /* pattern has a special character */
  326. upto += strlen(p + upto) + 1; /* may have more after \0 */
  327. } while (upto <= l);
  328. return 1; /* no special chars found */
  329. }
  330. static int str_find_aux (LuaMatchState *ms, int find, const char *s, int ls,
  331. const char *p, int lp, int init, int raw_find,
  332. luaregex_func_param fp, void *udata) {
  333. int result;
  334. ms->error = NULL;
  335. if(ls < 0) ls = strlen(s);
  336. if(lp < 0) lp = strlen(p);
  337. init = posrelat(init, ls);
  338. if (init < 0) init = 0;
  339. else if (init > ls + 1) { /* start after string's end? */
  340. return 0; /* cannot find anything */
  341. }
  342. ms->src_init = s;
  343. ms->src_end = s + ls;
  344. do_again:
  345. result = -1; /* not found */
  346. /* explicit request or no special characters? */
  347. if (find && (raw_find || nospecials(p, lp))) {
  348. /* do a plain search */
  349. const char *s2 = lmemfind(s + init, ls - init + 1, p, lp);
  350. if (s2) {
  351. ms->start_pos = ((int)(s2 - s));
  352. result = ms->end_pos = ms->start_pos+lp;
  353. ms->level = 0;
  354. }
  355. }
  356. else {
  357. const char *s1 = s + init;
  358. int anchor = (*p == '^');
  359. if (anchor) {
  360. p++; lp--; /* skip anchor character */
  361. }
  362. ms->p_end = p + lp;
  363. do {
  364. const char *res;
  365. ms->level = 0;
  366. if ((res=match(ms, s1, p)) != NULL) {
  367. ms->start_pos = s1-s;
  368. result = ms->end_pos = res-s-1;
  369. goto eofunc;
  370. }
  371. } while (s1++ < ms->src_end && !anchor);
  372. }
  373. eofunc:
  374. if(result >= 0){
  375. int i;
  376. for(i=0; i<ms->level; ++i){
  377. if(ms->capture[i].len == CAP_UNFINISHED){
  378. ms->error = "unfinished capture";
  379. return 0;
  380. }
  381. }
  382. if(fp && (*fp)(ms, udata, 0)) {
  383. init = result+1;
  384. if (init < ls) goto do_again;
  385. }
  386. }
  387. return result > 0 ? ms->start_pos : result; //returning the start position
  388. }
  389. int str_find (LuaMatchState *ms, const char *s, int ls,
  390. const char *p, int lp, int init, int raw_find,
  391. luaregex_func_param fp, void *udata) {
  392. return str_find_aux(ms, 1, s, ls, p, lp, init, raw_find, fp, udata);
  393. }
  394. int str_match (LuaMatchState *ms, const char *s, int ls,
  395. const char *p, int lp, int init, int raw_find,
  396. luaregex_func_param fp, void *udata) {
  397. return str_find_aux(ms, 0, s, ls, p, lp, init, raw_find, fp, udata);
  398. }
  399. #define NEW_SIZE(sz) (((sz/1024)+1)*1024)
  400. int char_buffer_add_char(LuaMatchState *ms, char_buffer_st **b, char c){
  401. char_buffer_st *tmp = *b;
  402. if(tmp->used+1 >= tmp->size){
  403. int new_size = tmp->size+2048;
  404. tmp = (char_buffer_st*)realloc(tmp, sizeof(char_buffer_st) + new_size);
  405. if(!tmp){
  406. ms->error = "not enough memory when reallocating";
  407. return 0;
  408. }
  409. *b = tmp;
  410. tmp->size = new_size;
  411. }
  412. tmp->buf[tmp->used++] = c;
  413. return 1;
  414. }
  415. int char_buffer_add_str(LuaMatchState *ms, char_buffer_st **b, const char *str, int len){
  416. char_buffer_st *tmp = *b;
  417. if(len < 0) len = strlen(str);
  418. if(tmp->used+len >= tmp->size){
  419. size_t new_size = tmp->size + NEW_SIZE(len);
  420. tmp = (char_buffer_st*)realloc(tmp, sizeof(char_buffer_st) + new_size);
  421. if(!tmp){
  422. ms->error = "not enough memory when reallocating";
  423. return 0;
  424. }
  425. *b = tmp;
  426. tmp->size = new_size;
  427. }
  428. memcpy(&tmp->buf[tmp->used], str, len);
  429. tmp->used += len;
  430. return 1;
  431. }
  432. static int add_value (LuaMatchState *ms, char_buffer_st **b, const char *s,
  433. const char *e, const char *news, size_t lnews) {
  434. size_t i;
  435. for (i = 0; i < lnews; i++) {
  436. if (news[i] != L_ESC){
  437. if(!char_buffer_add_char(ms, b, news[i])) return 0;
  438. }
  439. else {
  440. i++; /* skip ESC */
  441. if (!isdigit(uchar(news[i]))) {
  442. if (news[i] != L_ESC){
  443. ms->error = "invalid use of replacement string";
  444. return 0;
  445. }
  446. if(!char_buffer_add_char(ms, b, news[i])) return 0;
  447. }
  448. else if (news[i] == '0'){
  449. if(!char_buffer_add_str(ms, b, s, e - s)) return 0;
  450. }
  451. else {
  452. int il = news[i] - '1';
  453. if (il >= ms->level) {
  454. if (il == 0){ /* ms->level == 0, too */
  455. if(!char_buffer_add_str(ms, b, s, e - s)) return 0; /* add whole match */
  456. }
  457. else{
  458. ms->error = "invalid capture index";
  459. return 0;
  460. }
  461. }
  462. else {
  463. ptrdiff_t cl = ms->capture[il].len;
  464. if (cl == CAP_UNFINISHED) {
  465. ms->error = "unfinished capture";
  466. return 0;
  467. }
  468. if (cl == CAP_POSITION){
  469. char buf[32];
  470. snprintf(buf, sizeof(buf), "%d", ms->capture[il].init - ms->src_init + 1);
  471. if(!char_buffer_add_str(ms, b, buf, strlen(buf))) return 0;
  472. }
  473. else
  474. if(!char_buffer_add_str(ms, b, ms->capture[il].init, cl)) return 0;
  475. }
  476. }
  477. }
  478. }
  479. return 1;
  480. }
  481. char_buffer_st *str_gsub (const char *src, int srcl, const char *p, int lp,
  482. const char *tr, int ltr, size_t max_s, const char **error_ptr,
  483. luaregex_func_param fp, void *udata) {
  484. int anchor;
  485. size_t n;
  486. LuaMatchState ms;
  487. char_buffer_st *b;
  488. if(srcl < 0) srcl = strlen(src);
  489. if(lp < 0) lp = strlen(p);
  490. if(ltr < 0) ltr = strlen(tr);
  491. if(max_s == 0) max_s = srcl+1;
  492. anchor = (*p == '^');
  493. n = NEW_SIZE(srcl);
  494. b = (char_buffer_st*)malloc(sizeof(char_buffer_st) + n);
  495. if(!b) return NULL;
  496. b->size = n;
  497. b->used = 0;
  498. n = 0;
  499. if (anchor) {
  500. p++; lp--; /* skip anchor character */
  501. }
  502. ms.error = 0;
  503. ms.src_init = src;
  504. ms.src_end = src+srcl;
  505. ms.p_end = p + lp;
  506. while (n < max_s) {
  507. const char *e;
  508. ms.level = 0;
  509. e = match(&ms, src, p);
  510. if(ms.error) goto free_and_null;
  511. if (e) {
  512. n++;
  513. if(fp){
  514. if(!(*fp)(&ms, udata, &b)) goto free_and_null;
  515. }
  516. else if(!add_value(&ms, &b, src, e, tr, ltr)) goto free_and_null;
  517. }
  518. if (e && e>src) /* non empty match? */
  519. src = e; /* skip it */
  520. else if (src < ms.src_end){
  521. if(!char_buffer_add_char(&ms, &b, *src++)) goto free_and_null;
  522. }
  523. else break;
  524. if (anchor) break;
  525. }
  526. if(!char_buffer_add_str(&ms, &b, src, ms.src_end-src)) goto free_and_null;
  527. b->buf[b->used] = '\0';
  528. return b;
  529. free_and_null:
  530. if(b) free(b);
  531. if(error_ptr) *error_ptr = ms.error;
  532. return NULL;
  533. }