lua-regex.c 18 KB

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