lstrlib.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980
  1. /*
  2. ** $Id: lstrlib.c,v 1.181 2013/06/19 14:29:01 roberto Exp roberto $
  3. ** Standard library for string operations and pattern-matching
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <limits.h>
  8. #include <stddef.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #define lstrlib_c
  13. #define LUA_LIB
  14. #include "lua.h"
  15. #include "lauxlib.h"
  16. #include "lualib.h"
  17. /*
  18. ** maximum number of captures that a pattern can do during
  19. ** pattern-matching. This limit is arbitrary.
  20. */
  21. #if !defined(LUA_MAXCAPTURES)
  22. #define LUA_MAXCAPTURES 32
  23. #endif
  24. /* macro to `unsign' a character */
  25. #define uchar(c) ((unsigned char)(c))
  26. static int str_len (lua_State *L) {
  27. size_t l;
  28. luaL_checklstring(L, 1, &l);
  29. lua_pushinteger(L, (lua_Integer)l);
  30. return 1;
  31. }
  32. /* translate a relative string position: negative means back from end */
  33. static lua_Integer posrelat (lua_Integer pos, size_t len) {
  34. if (pos >= 0) return pos;
  35. else if (0u - (size_t)pos > len) return 0;
  36. else return (lua_Integer)len + pos + 1;
  37. }
  38. static int str_sub (lua_State *L) {
  39. size_t l;
  40. const char *s = luaL_checklstring(L, 1, &l);
  41. lua_Integer start = posrelat(luaL_checkinteger(L, 2), l);
  42. lua_Integer end = posrelat(luaL_optinteger(L, 3, -1), l);
  43. if (start < 1) start = 1;
  44. if (end > (lua_Integer)l) end = l;
  45. if (start <= end)
  46. lua_pushlstring(L, s + start - 1, end - start + 1);
  47. else lua_pushliteral(L, "");
  48. return 1;
  49. }
  50. static int str_reverse (lua_State *L) {
  51. size_t l, i;
  52. luaL_Buffer b;
  53. const char *s = luaL_checklstring(L, 1, &l);
  54. char *p = luaL_buffinitsize(L, &b, l);
  55. for (i = 0; i < l; i++)
  56. p[i] = s[l - i - 1];
  57. luaL_pushresultsize(&b, l);
  58. return 1;
  59. }
  60. static int str_lower (lua_State *L) {
  61. size_t l;
  62. size_t i;
  63. luaL_Buffer b;
  64. const char *s = luaL_checklstring(L, 1, &l);
  65. char *p = luaL_buffinitsize(L, &b, l);
  66. for (i=0; i<l; i++)
  67. p[i] = tolower(uchar(s[i]));
  68. luaL_pushresultsize(&b, l);
  69. return 1;
  70. }
  71. static int str_upper (lua_State *L) {
  72. size_t l;
  73. size_t i;
  74. luaL_Buffer b;
  75. const char *s = luaL_checklstring(L, 1, &l);
  76. char *p = luaL_buffinitsize(L, &b, l);
  77. for (i=0; i<l; i++)
  78. p[i] = toupper(uchar(s[i]));
  79. luaL_pushresultsize(&b, l);
  80. return 1;
  81. }
  82. /* reasonable limit to avoid arithmetic overflow and strings too big */
  83. #if INT_MAX / 2 <= 0x10000000
  84. #define MAXSIZE ((size_t)(INT_MAX / 2))
  85. #else
  86. #define MAXSIZE ((size_t)0x10000000)
  87. #endif
  88. static int str_rep (lua_State *L) {
  89. size_t l, lsep;
  90. const char *s = luaL_checklstring(L, 1, &l);
  91. lua_Integer n = luaL_checkinteger(L, 2);
  92. const char *sep = luaL_optlstring(L, 3, "", &lsep);
  93. if (n <= 0) lua_pushliteral(L, "");
  94. else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
  95. return luaL_error(L, "resulting string too large");
  96. else {
  97. size_t totallen = n * l + (n - 1) * lsep;
  98. luaL_Buffer b;
  99. char *p = luaL_buffinitsize(L, &b, totallen);
  100. while (n-- > 1) { /* first n-1 copies (followed by separator) */
  101. memcpy(p, s, l * sizeof(char)); p += l;
  102. if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */
  103. memcpy(p, sep, lsep * sizeof(char)); p += lsep;
  104. }
  105. }
  106. memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
  107. luaL_pushresultsize(&b, totallen);
  108. }
  109. return 1;
  110. }
  111. static int str_byte (lua_State *L) {
  112. size_t l;
  113. const char *s = luaL_checklstring(L, 1, &l);
  114. lua_Integer posi = posrelat(luaL_optinteger(L, 2, 1), l);
  115. lua_Integer pose = posrelat(luaL_optinteger(L, 3, posi), l);
  116. int n, i;
  117. if (posi < 1) posi = 1;
  118. if (pose > (lua_Integer)l) pose = l;
  119. if (posi > pose) return 0; /* empty interval; return no values */
  120. n = (int)(pose - posi + 1);
  121. if (posi + n <= pose) /* arithmetic overflow? */
  122. return luaL_error(L, "string slice too long");
  123. luaL_checkstack(L, n, "string slice too long");
  124. for (i=0; i<n; i++)
  125. lua_pushinteger(L, uchar(s[posi+i-1]));
  126. return n;
  127. }
  128. static int str_char (lua_State *L) {
  129. int n = lua_gettop(L); /* number of arguments */
  130. int i;
  131. luaL_Buffer b;
  132. char *p = luaL_buffinitsize(L, &b, n);
  133. for (i=1; i<=n; i++) {
  134. lua_Integer c = luaL_checkinteger(L, i);
  135. luaL_argcheck(L, uchar(c) == c, i, "value out of range");
  136. p[i - 1] = uchar(c);
  137. }
  138. luaL_pushresultsize(&b, n);
  139. return 1;
  140. }
  141. static int writer (lua_State *L, const void *b, size_t size, void *B) {
  142. (void)L;
  143. luaL_addlstring((luaL_Buffer *) B, (const char *)b, size);
  144. return 0;
  145. }
  146. static int str_dump (lua_State *L) {
  147. luaL_Buffer b;
  148. luaL_checktype(L, 1, LUA_TFUNCTION);
  149. lua_settop(L, 1);
  150. luaL_buffinit(L,&b);
  151. if (lua_dump(L, writer, &b) != 0)
  152. return luaL_error(L, "unable to dump given function");
  153. luaL_pushresult(&b);
  154. return 1;
  155. }
  156. /*
  157. ** {======================================================
  158. ** PATTERN MATCHING
  159. ** =======================================================
  160. */
  161. #define CAP_UNFINISHED (-1)
  162. #define CAP_POSITION (-2)
  163. typedef struct MatchState {
  164. int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
  165. const char *src_init; /* init of source string */
  166. const char *src_end; /* end ('\0') of source string */
  167. const char *p_end; /* end ('\0') of pattern */
  168. lua_State *L;
  169. int level; /* total number of captures (finished or unfinished) */
  170. struct {
  171. const char *init;
  172. ptrdiff_t len;
  173. } capture[LUA_MAXCAPTURES];
  174. } MatchState;
  175. /* recursive function */
  176. static const char *match (MatchState *ms, const char *s, const char *p);
  177. /* maximum recursion depth for 'match' */
  178. #if !defined(MAXCCALLS)
  179. #define MAXCCALLS 200
  180. #endif
  181. #define L_ESC '%'
  182. #define SPECIALS "^$*+?.([%-"
  183. static int check_capture (MatchState *ms, int l) {
  184. l -= '1';
  185. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  186. return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
  187. return l;
  188. }
  189. static int capture_to_close (MatchState *ms) {
  190. int level = ms->level;
  191. for (level--; level>=0; level--)
  192. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  193. return luaL_error(ms->L, "invalid pattern capture");
  194. }
  195. static const char *classend (MatchState *ms, const char *p) {
  196. switch (*p++) {
  197. case L_ESC: {
  198. if (p == ms->p_end)
  199. luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
  200. return p+1;
  201. }
  202. case '[': {
  203. if (*p == '^') p++;
  204. do { /* look for a `]' */
  205. if (p == ms->p_end)
  206. luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
  207. if (*(p++) == L_ESC && p < ms->p_end)
  208. p++; /* skip escapes (e.g. `%]') */
  209. } while (*p != ']');
  210. return p+1;
  211. }
  212. default: {
  213. return p;
  214. }
  215. }
  216. }
  217. static int match_class (int c, int cl) {
  218. int res;
  219. switch (tolower(cl)) {
  220. case 'a' : res = isalpha(c); break;
  221. case 'c' : res = iscntrl(c); break;
  222. case 'd' : res = isdigit(c); break;
  223. case 'g' : res = isgraph(c); break;
  224. case 'l' : res = islower(c); break;
  225. case 'p' : res = ispunct(c); break;
  226. case 's' : res = isspace(c); break;
  227. case 'u' : res = isupper(c); break;
  228. case 'w' : res = isalnum(c); break;
  229. case 'x' : res = isxdigit(c); break;
  230. case 'z' : res = (c == 0); break; /* deprecated option */
  231. default: return (cl == c);
  232. }
  233. return (islower(cl) ? res : !res);
  234. }
  235. static int matchbracketclass (int c, const char *p, const char *ec) {
  236. int sig = 1;
  237. if (*(p+1) == '^') {
  238. sig = 0;
  239. p++; /* skip the `^' */
  240. }
  241. while (++p < ec) {
  242. if (*p == L_ESC) {
  243. p++;
  244. if (match_class(c, uchar(*p)))
  245. return sig;
  246. }
  247. else if ((*(p+1) == '-') && (p+2 < ec)) {
  248. p+=2;
  249. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  250. return sig;
  251. }
  252. else if (uchar(*p) == c) return sig;
  253. }
  254. return !sig;
  255. }
  256. static int singlematch (MatchState *ms, const char *s, const char *p,
  257. const char *ep) {
  258. if (s >= ms->src_end)
  259. return 0;
  260. else {
  261. int c = uchar(*s);
  262. switch (*p) {
  263. case '.': return 1; /* matches any char */
  264. case L_ESC: return match_class(c, uchar(*(p+1)));
  265. case '[': return matchbracketclass(c, p, ep-1);
  266. default: return (uchar(*p) == c);
  267. }
  268. }
  269. }
  270. static const char *matchbalance (MatchState *ms, const char *s,
  271. const char *p) {
  272. if (p >= ms->p_end - 1)
  273. luaL_error(ms->L, "malformed pattern "
  274. "(missing arguments to " LUA_QL("%%b") ")");
  275. if (*s != *p) return NULL;
  276. else {
  277. int b = *p;
  278. int e = *(p+1);
  279. int cont = 1;
  280. while (++s < ms->src_end) {
  281. if (*s == e) {
  282. if (--cont == 0) return s+1;
  283. }
  284. else if (*s == b) cont++;
  285. }
  286. }
  287. return NULL; /* string ends out of balance */
  288. }
  289. static const char *max_expand (MatchState *ms, const char *s,
  290. const char *p, const char *ep) {
  291. ptrdiff_t i = 0; /* counts maximum expand for item */
  292. while (singlematch(ms, s + i, p, ep))
  293. i++;
  294. /* keeps trying to match with the maximum repetitions */
  295. while (i>=0) {
  296. const char *res = match(ms, (s+i), ep+1);
  297. if (res) return res;
  298. i--; /* else didn't match; reduce 1 repetition to try again */
  299. }
  300. return NULL;
  301. }
  302. static const char *min_expand (MatchState *ms, const char *s,
  303. const char *p, const char *ep) {
  304. for (;;) {
  305. const char *res = match(ms, s, ep+1);
  306. if (res != NULL)
  307. return res;
  308. else if (singlematch(ms, s, p, ep))
  309. s++; /* try with one more repetition */
  310. else return NULL;
  311. }
  312. }
  313. static const char *start_capture (MatchState *ms, const char *s,
  314. const char *p, int what) {
  315. const char *res;
  316. int level = ms->level;
  317. if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
  318. ms->capture[level].init = s;
  319. ms->capture[level].len = what;
  320. ms->level = level+1;
  321. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  322. ms->level--; /* undo capture */
  323. return res;
  324. }
  325. static const char *end_capture (MatchState *ms, const char *s,
  326. const char *p) {
  327. int l = capture_to_close(ms);
  328. const char *res;
  329. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  330. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  331. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  332. return res;
  333. }
  334. static const char *match_capture (MatchState *ms, const char *s, int l) {
  335. size_t len;
  336. l = check_capture(ms, l);
  337. len = ms->capture[l].len;
  338. if ((size_t)(ms->src_end-s) >= len &&
  339. memcmp(ms->capture[l].init, s, len) == 0)
  340. return s+len;
  341. else return NULL;
  342. }
  343. static const char *match (MatchState *ms, const char *s, const char *p) {
  344. if (ms->matchdepth-- == 0)
  345. luaL_error(ms->L, "pattern too complex");
  346. init: /* using goto's to optimize tail recursion */
  347. if (p != ms->p_end) { /* end of pattern? */
  348. switch (*p) {
  349. case '(': { /* start capture */
  350. if (*(p + 1) == ')') /* position capture? */
  351. s = start_capture(ms, s, p + 2, CAP_POSITION);
  352. else
  353. s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
  354. break;
  355. }
  356. case ')': { /* end capture */
  357. s = end_capture(ms, s, p + 1);
  358. break;
  359. }
  360. case '$': {
  361. if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
  362. goto dflt; /* no; go to default */
  363. s = (s == ms->src_end) ? s : NULL; /* check end of string */
  364. break;
  365. }
  366. case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
  367. switch (*(p + 1)) {
  368. case 'b': { /* balanced string? */
  369. s = matchbalance(ms, s, p + 2);
  370. if (s != NULL) {
  371. p += 4; goto init; /* return match(ms, s, p + 4); */
  372. } /* else fail (s == NULL) */
  373. break;
  374. }
  375. case 'f': { /* frontier? */
  376. const char *ep; char previous;
  377. p += 2;
  378. if (*p != '[')
  379. luaL_error(ms->L, "missing " LUA_QL("[") " after "
  380. LUA_QL("%%f") " in pattern");
  381. ep = classend(ms, p); /* points to what is next */
  382. previous = (s == ms->src_init) ? '\0' : *(s - 1);
  383. if (!matchbracketclass(uchar(previous), p, ep - 1) &&
  384. matchbracketclass(uchar(*s), p, ep - 1)) {
  385. p = ep; goto init; /* return match(ms, s, ep); */
  386. }
  387. s = NULL; /* match failed */
  388. break;
  389. }
  390. case '0': case '1': case '2': case '3':
  391. case '4': case '5': case '6': case '7':
  392. case '8': case '9': { /* capture results (%0-%9)? */
  393. s = match_capture(ms, s, uchar(*(p + 1)));
  394. if (s != NULL) {
  395. p += 2; goto init; /* return match(ms, s, p + 2) */
  396. }
  397. break;
  398. }
  399. default: goto dflt;
  400. }
  401. break;
  402. }
  403. default: dflt: { /* pattern class plus optional suffix */
  404. const char *ep = classend(ms, p); /* points to optional suffix */
  405. /* does not match at least once? */
  406. if (!singlematch(ms, s, p, ep)) {
  407. if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
  408. p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
  409. }
  410. else /* '+' or no suffix */
  411. s = NULL; /* fail */
  412. }
  413. else { /* matched once */
  414. switch (*ep) { /* handle optional suffix */
  415. case '?': { /* optional */
  416. const char *res;
  417. if ((res = match(ms, s + 1, ep + 1)) != NULL)
  418. s = res;
  419. else {
  420. p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
  421. }
  422. break;
  423. }
  424. case '+': /* 1 or more repetitions */
  425. s++; /* 1 match already done */
  426. /* go through */
  427. case '*': /* 0 or more repetitions */
  428. s = max_expand(ms, s, p, ep);
  429. break;
  430. case '-': /* 0 or more repetitions (minimum) */
  431. s = min_expand(ms, s, p, ep);
  432. break;
  433. default: /* no suffix */
  434. s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
  435. }
  436. }
  437. break;
  438. }
  439. }
  440. }
  441. ms->matchdepth++;
  442. return s;
  443. }
  444. static const char *lmemfind (const char *s1, size_t l1,
  445. const char *s2, size_t l2) {
  446. if (l2 == 0) return s1; /* empty strings are everywhere */
  447. else if (l2 > l1) return NULL; /* avoids a negative `l1' */
  448. else {
  449. const char *init; /* to search for a `*s2' inside `s1' */
  450. l2--; /* 1st char will be checked by `memchr' */
  451. l1 = l1-l2; /* `s2' cannot be found after that */
  452. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  453. init++; /* 1st char is already checked */
  454. if (memcmp(init, s2+1, l2) == 0)
  455. return init-1;
  456. else { /* correct `l1' and `s1' to try again */
  457. l1 -= init-s1;
  458. s1 = init;
  459. }
  460. }
  461. return NULL; /* not found */
  462. }
  463. }
  464. static void push_onecapture (MatchState *ms, int i, const char *s,
  465. const char *e) {
  466. if (i >= ms->level) {
  467. if (i == 0) /* ms->level == 0, too */
  468. lua_pushlstring(ms->L, s, e - s); /* add whole match */
  469. else
  470. luaL_error(ms->L, "invalid capture index");
  471. }
  472. else {
  473. ptrdiff_t l = ms->capture[i].len;
  474. if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
  475. if (l == CAP_POSITION)
  476. lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
  477. else
  478. lua_pushlstring(ms->L, ms->capture[i].init, l);
  479. }
  480. }
  481. static int push_captures (MatchState *ms, const char *s, const char *e) {
  482. int i;
  483. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  484. luaL_checkstack(ms->L, nlevels, "too many captures");
  485. for (i = 0; i < nlevels; i++)
  486. push_onecapture(ms, i, s, e);
  487. return nlevels; /* number of strings pushed */
  488. }
  489. /* check whether pattern has no special characters */
  490. static int nospecials (const char *p, size_t l) {
  491. size_t upto = 0;
  492. do {
  493. if (strpbrk(p + upto, SPECIALS))
  494. return 0; /* pattern has a special character */
  495. upto += strlen(p + upto) + 1; /* may have more after \0 */
  496. } while (upto <= l);
  497. return 1; /* no special chars found */
  498. }
  499. static int str_find_aux (lua_State *L, int find) {
  500. size_t ls, lp;
  501. const char *s = luaL_checklstring(L, 1, &ls);
  502. const char *p = luaL_checklstring(L, 2, &lp);
  503. lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
  504. if (init < 1) init = 1;
  505. else if (init > (lua_Integer)ls + 1) { /* start after string's end? */
  506. lua_pushnil(L); /* cannot find anything */
  507. return 1;
  508. }
  509. /* explicit request or no special characters? */
  510. if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
  511. /* do a plain search */
  512. const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
  513. if (s2) {
  514. lua_pushinteger(L, s2 - s + 1);
  515. lua_pushinteger(L, s2 - s + lp);
  516. return 2;
  517. }
  518. }
  519. else {
  520. MatchState ms;
  521. const char *s1 = s + init - 1;
  522. int anchor = (*p == '^');
  523. if (anchor) {
  524. p++; lp--; /* skip anchor character */
  525. }
  526. ms.L = L;
  527. ms.matchdepth = MAXCCALLS;
  528. ms.src_init = s;
  529. ms.src_end = s + ls;
  530. ms.p_end = p + lp;
  531. do {
  532. const char *res;
  533. ms.level = 0;
  534. lua_assert(ms.matchdepth == MAXCCALLS);
  535. if ((res=match(&ms, s1, p)) != NULL) {
  536. if (find) {
  537. lua_pushinteger(L, s1 - s + 1); /* start */
  538. lua_pushinteger(L, res - s); /* end */
  539. return push_captures(&ms, NULL, 0) + 2;
  540. }
  541. else
  542. return push_captures(&ms, s1, res);
  543. }
  544. } while (s1++ < ms.src_end && !anchor);
  545. }
  546. lua_pushnil(L); /* not found */
  547. return 1;
  548. }
  549. static int str_find (lua_State *L) {
  550. return str_find_aux(L, 1);
  551. }
  552. static int str_match (lua_State *L) {
  553. return str_find_aux(L, 0);
  554. }
  555. static int gmatch_aux (lua_State *L) {
  556. MatchState ms;
  557. size_t ls, lp;
  558. const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
  559. const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
  560. const char *src;
  561. ms.L = L;
  562. ms.matchdepth = MAXCCALLS;
  563. ms.src_init = s;
  564. ms.src_end = s+ls;
  565. ms.p_end = p + lp;
  566. for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
  567. src <= ms.src_end;
  568. src++) {
  569. const char *e;
  570. ms.level = 0;
  571. lua_assert(ms.matchdepth == MAXCCALLS);
  572. if ((e = match(&ms, src, p)) != NULL) {
  573. lua_Integer newstart = e-s;
  574. if (e == src) newstart++; /* empty match? go at least one position */
  575. lua_pushinteger(L, newstart);
  576. lua_replace(L, lua_upvalueindex(3));
  577. return push_captures(&ms, src, e);
  578. }
  579. }
  580. return 0; /* not found */
  581. }
  582. static int gmatch (lua_State *L) {
  583. luaL_checkstring(L, 1);
  584. luaL_checkstring(L, 2);
  585. lua_settop(L, 2);
  586. lua_pushinteger(L, 0);
  587. lua_pushcclosure(L, gmatch_aux, 3);
  588. return 1;
  589. }
  590. static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
  591. const char *e) {
  592. size_t l, i;
  593. const char *news = lua_tolstring(ms->L, 3, &l);
  594. for (i = 0; i < l; i++) {
  595. if (news[i] != L_ESC)
  596. luaL_addchar(b, news[i]);
  597. else {
  598. i++; /* skip ESC */
  599. if (!isdigit(uchar(news[i]))) {
  600. if (news[i] != L_ESC)
  601. luaL_error(ms->L, "invalid use of " LUA_QL("%c")
  602. " in replacement string", L_ESC);
  603. luaL_addchar(b, news[i]);
  604. }
  605. else if (news[i] == '0')
  606. luaL_addlstring(b, s, e - s);
  607. else {
  608. push_onecapture(ms, news[i] - '1', s, e);
  609. luaL_addvalue(b); /* add capture to accumulated result */
  610. }
  611. }
  612. }
  613. }
  614. static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
  615. const char *e, int tr) {
  616. lua_State *L = ms->L;
  617. switch (tr) {
  618. case LUA_TFUNCTION: {
  619. int n;
  620. lua_pushvalue(L, 3);
  621. n = push_captures(ms, s, e);
  622. lua_call(L, n, 1);
  623. break;
  624. }
  625. case LUA_TTABLE: {
  626. push_onecapture(ms, 0, s, e);
  627. lua_gettable(L, 3);
  628. break;
  629. }
  630. default: { /* LUA_TNUMBER or LUA_TSTRING */
  631. add_s(ms, b, s, e);
  632. return;
  633. }
  634. }
  635. if (!lua_toboolean(L, -1)) { /* nil or false? */
  636. lua_pop(L, 1);
  637. lua_pushlstring(L, s, e - s); /* keep original text */
  638. }
  639. else if (!lua_isstring(L, -1))
  640. luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
  641. luaL_addvalue(b); /* add result to accumulator */
  642. }
  643. static int str_gsub (lua_State *L) {
  644. size_t srcl, lp;
  645. const char *src = luaL_checklstring(L, 1, &srcl);
  646. const char *p = luaL_checklstring(L, 2, &lp);
  647. int tr = lua_type(L, 3);
  648. size_t max_s = luaL_optinteger(L, 4, srcl+1);
  649. int anchor = (*p == '^');
  650. size_t n = 0;
  651. MatchState ms;
  652. luaL_Buffer b;
  653. luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
  654. tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
  655. "string/function/table expected");
  656. luaL_buffinit(L, &b);
  657. if (anchor) {
  658. p++; lp--; /* skip anchor character */
  659. }
  660. ms.L = L;
  661. ms.matchdepth = MAXCCALLS;
  662. ms.src_init = src;
  663. ms.src_end = src+srcl;
  664. ms.p_end = p + lp;
  665. while (n < max_s) {
  666. const char *e;
  667. ms.level = 0;
  668. lua_assert(ms.matchdepth == MAXCCALLS);
  669. e = match(&ms, src, p);
  670. if (e) {
  671. n++;
  672. add_value(&ms, &b, src, e, tr);
  673. }
  674. if (e && e>src) /* non empty match? */
  675. src = e; /* skip it */
  676. else if (src < ms.src_end)
  677. luaL_addchar(&b, *src++);
  678. else break;
  679. if (anchor) break;
  680. }
  681. luaL_addlstring(&b, src, ms.src_end-src);
  682. luaL_pushresult(&b);
  683. lua_pushinteger(L, n); /* number of substitutions */
  684. return 2;
  685. }
  686. /* }====================================================== */
  687. /*
  688. ** {======================================================
  689. ** STRING FORMAT
  690. ** =======================================================
  691. */
  692. /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
  693. #define MAX_ITEM 512
  694. /* valid flags in a format specification */
  695. #define FLAGS "-+ #0"
  696. /*
  697. ** maximum size of each format specification (such as "%-099.99d")
  698. ** (+2 for length modifiers; +10 accounts for %99.99x plus margin of error)
  699. */
  700. #define MAX_FORMAT (sizeof(FLAGS) + 2 + 10)
  701. static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  702. size_t l;
  703. const char *s = luaL_checklstring(L, arg, &l);
  704. luaL_addchar(b, '"');
  705. while (l--) {
  706. if (*s == '"' || *s == '\\' || *s == '\n') {
  707. luaL_addchar(b, '\\');
  708. luaL_addchar(b, *s);
  709. }
  710. else if (*s == '\0' || iscntrl(uchar(*s))) {
  711. char buff[10];
  712. if (!isdigit(uchar(*(s+1))))
  713. sprintf(buff, "\\%d", (int)uchar(*s));
  714. else
  715. sprintf(buff, "\\%03d", (int)uchar(*s));
  716. luaL_addstring(b, buff);
  717. }
  718. else
  719. luaL_addchar(b, *s);
  720. s++;
  721. }
  722. luaL_addchar(b, '"');
  723. }
  724. static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
  725. const char *p = strfrmt;
  726. while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
  727. if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
  728. luaL_error(L, "invalid format (repeated flags)");
  729. if (isdigit(uchar(*p))) p++; /* skip width */
  730. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  731. if (*p == '.') {
  732. p++;
  733. if (isdigit(uchar(*p))) p++; /* skip precision */
  734. if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
  735. }
  736. if (isdigit(uchar(*p)))
  737. luaL_error(L, "invalid format (width or precision too long)");
  738. *(form++) = '%';
  739. memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
  740. form += p - strfrmt + 1;
  741. *form = '\0';
  742. return p;
  743. }
  744. /*
  745. ** add length modifier into formats
  746. */
  747. static void addlenmod (char *form, const char *lenmod) {
  748. size_t l = strlen(form);
  749. size_t lm = strlen(lenmod);
  750. char spec = form[l - 1];
  751. strcpy(form + l - 1, lenmod);
  752. form[l + lm - 1] = spec;
  753. form[l + lm] = '\0';
  754. }
  755. static int str_format (lua_State *L) {
  756. int top = lua_gettop(L);
  757. int arg = 1;
  758. size_t sfl;
  759. const char *strfrmt = luaL_checklstring(L, arg, &sfl);
  760. const char *strfrmt_end = strfrmt+sfl;
  761. luaL_Buffer b;
  762. luaL_buffinit(L, &b);
  763. while (strfrmt < strfrmt_end) {
  764. if (*strfrmt != L_ESC)
  765. luaL_addchar(&b, *strfrmt++);
  766. else if (*++strfrmt == L_ESC)
  767. luaL_addchar(&b, *strfrmt++); /* %% */
  768. else { /* format item */
  769. char form[MAX_FORMAT]; /* to store the format (`%...') */
  770. char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
  771. int nb = 0; /* number of bytes in added item */
  772. if (++arg > top)
  773. luaL_argerror(L, arg, "no value");
  774. strfrmt = scanformat(L, strfrmt, form);
  775. switch (*strfrmt++) {
  776. case 'c': {
  777. nb = sprintf(buff, form, luaL_checkint(L, arg));
  778. break;
  779. }
  780. case 'd': case 'i':
  781. case 'o': case 'u': case 'x': case 'X': {
  782. lua_Integer n = luaL_checkinteger(L, arg);
  783. addlenmod(form, LUA_INTEGER_FRMLEN);
  784. nb = sprintf(buff, form, n);
  785. break;
  786. }
  787. case 'e': case 'E': case 'f':
  788. #if defined(LUA_USE_AFORMAT)
  789. case 'a': case 'A':
  790. #endif
  791. case 'g': case 'G': {
  792. addlenmod(form, LUA_NUMBER_FRMLEN);
  793. nb = sprintf(buff, form, luaL_checknumber(L, arg));
  794. break;
  795. }
  796. case 'q': {
  797. addquoted(L, &b, arg);
  798. break;
  799. }
  800. case 's': {
  801. size_t l;
  802. const char *s = luaL_tolstring(L, arg, &l);
  803. if (!strchr(form, '.') && l >= 100) {
  804. /* no precision and string is too long to be formatted;
  805. keep original string */
  806. luaL_addvalue(&b);
  807. break;
  808. }
  809. else {
  810. nb = sprintf(buff, form, s);
  811. lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
  812. break;
  813. }
  814. }
  815. default: { /* also treat cases `pnLlh' */
  816. return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
  817. LUA_QL("format"), *(strfrmt - 1));
  818. }
  819. }
  820. luaL_addsize(&b, nb);
  821. }
  822. }
  823. luaL_pushresult(&b);
  824. return 1;
  825. }
  826. /* }====================================================== */
  827. static const luaL_Reg strlib[] = {
  828. {"byte", str_byte},
  829. {"char", str_char},
  830. {"dump", str_dump},
  831. {"find", str_find},
  832. {"format", str_format},
  833. {"gmatch", gmatch},
  834. {"gsub", str_gsub},
  835. {"len", str_len},
  836. {"lower", str_lower},
  837. {"match", str_match},
  838. {"rep", str_rep},
  839. {"reverse", str_reverse},
  840. {"sub", str_sub},
  841. {"upper", str_upper},
  842. {NULL, NULL}
  843. };
  844. static void createmetatable (lua_State *L) {
  845. lua_createtable(L, 0, 1); /* table to be metatable for strings */
  846. lua_pushliteral(L, ""); /* dummy string */
  847. lua_pushvalue(L, -2); /* copy table */
  848. lua_setmetatable(L, -2); /* set table as metatable for strings */
  849. lua_pop(L, 1); /* pop dummy string */
  850. lua_pushvalue(L, -2); /* get string library */
  851. lua_setfield(L, -2, "__index"); /* metatable.__index = string */
  852. lua_pop(L, 1); /* pop metatable */
  853. }
  854. /*
  855. ** Open string library
  856. */
  857. LUAMOD_API int luaopen_string (lua_State *L) {
  858. luaL_newlib(L, strlib);
  859. createmetatable(L);
  860. return 1;
  861. }