2
0

lua-regex.c 18 KB

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