lib_string.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*
  2. ** String library.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. **
  5. ** Major portions taken verbatim or adapted from the Lua interpreter.
  6. ** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
  7. */
  8. #include <stdio.h>
  9. #define lib_string_c
  10. #define LUA_LIB
  11. #include "lua.h"
  12. #include "lauxlib.h"
  13. #include "lualib.h"
  14. #include "lj_obj.h"
  15. #include "lj_gc.h"
  16. #include "lj_err.h"
  17. #include "lj_str.h"
  18. #include "lj_tab.h"
  19. #include "lj_meta.h"
  20. #include "lj_state.h"
  21. #include "lj_ff.h"
  22. #include "lj_bcdump.h"
  23. #include "lj_char.h"
  24. #include "lj_lib.h"
  25. /* ------------------------------------------------------------------------ */
  26. #define LJLIB_MODULE_string
  27. LJLIB_ASM(string_len) LJLIB_REC(.)
  28. {
  29. lj_lib_checkstr(L, 1);
  30. return FFH_RETRY;
  31. }
  32. LJLIB_ASM(string_byte) LJLIB_REC(string_range 0)
  33. {
  34. GCstr *s = lj_lib_checkstr(L, 1);
  35. int32_t len = (int32_t)s->len;
  36. int32_t start = lj_lib_optint(L, 2, 1);
  37. int32_t stop = lj_lib_optint(L, 3, start);
  38. int32_t n, i;
  39. const unsigned char *p;
  40. if (stop < 0) stop += len+1;
  41. if (start < 0) start += len+1;
  42. if (start <= 0) start = 1;
  43. if (stop > len) stop = len;
  44. if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */
  45. start--;
  46. n = stop - start;
  47. if ((uint32_t)n > LUAI_MAXCSTACK)
  48. lj_err_caller(L, LJ_ERR_STRSLC);
  49. lj_state_checkstack(L, (MSize)n);
  50. p = (const unsigned char *)strdata(s) + start;
  51. for (i = 0; i < n; i++)
  52. setintV(L->base + i-1, p[i]);
  53. return FFH_RES(n);
  54. }
  55. LJLIB_ASM(string_char)
  56. {
  57. int i, nargs = (int)(L->top - L->base);
  58. char *buf = lj_str_needbuf(L, &G(L)->tmpbuf, (MSize)nargs);
  59. for (i = 1; i <= nargs; i++) {
  60. int32_t k = lj_lib_checkint(L, i);
  61. if (!checku8(k))
  62. lj_err_arg(L, i, LJ_ERR_BADVAL);
  63. buf[i-1] = (char)k;
  64. }
  65. setstrV(L, L->base-1, lj_str_new(L, buf, (size_t)nargs));
  66. return FFH_RES(1);
  67. }
  68. LJLIB_ASM(string_sub) LJLIB_REC(string_range 1)
  69. {
  70. lj_lib_checkstr(L, 1);
  71. lj_lib_checkint(L, 2);
  72. setintV(L->base+2, lj_lib_optint(L, 3, -1));
  73. return FFH_RETRY;
  74. }
  75. LJLIB_ASM(string_rep)
  76. {
  77. GCstr *s = lj_lib_checkstr(L, 1);
  78. int32_t k = lj_lib_checkint(L, 2);
  79. GCstr *sep = lj_lib_optstr(L, 3);
  80. int32_t len = (int32_t)s->len;
  81. global_State *g = G(L);
  82. int64_t tlen;
  83. const char *src;
  84. char *buf;
  85. if (k <= 0) {
  86. empty:
  87. setstrV(L, L->base-1, &g->strempty);
  88. return FFH_RES(1);
  89. }
  90. if (sep) {
  91. tlen = (int64_t)len + sep->len;
  92. if (tlen > LJ_MAX_STR)
  93. lj_err_caller(L, LJ_ERR_STROV);
  94. tlen *= k;
  95. if (tlen > LJ_MAX_STR)
  96. lj_err_caller(L, LJ_ERR_STROV);
  97. } else {
  98. tlen = (int64_t)k * len;
  99. if (tlen > LJ_MAX_STR)
  100. lj_err_caller(L, LJ_ERR_STROV);
  101. }
  102. if (tlen == 0) goto empty;
  103. buf = lj_str_needbuf(L, &g->tmpbuf, (MSize)tlen);
  104. src = strdata(s);
  105. if (sep) {
  106. tlen -= sep->len; /* Ignore trailing separator. */
  107. if (k > 1) { /* Paste one string and one separator. */
  108. int32_t i;
  109. i = 0; while (i < len) *buf++ = src[i++];
  110. src = strdata(sep); len = sep->len;
  111. i = 0; while (i < len) *buf++ = src[i++];
  112. src = g->tmpbuf.buf; len += s->len; k--; /* Now copy that k-1 times. */
  113. }
  114. }
  115. do {
  116. int32_t i = 0;
  117. do { *buf++ = src[i++]; } while (i < len);
  118. } while (--k > 0);
  119. setstrV(L, L->base-1, lj_str_new(L, g->tmpbuf.buf, (size_t)tlen));
  120. return FFH_RES(1);
  121. }
  122. LJLIB_ASM(string_reverse)
  123. {
  124. GCstr *s = lj_lib_checkstr(L, 1);
  125. lj_str_needbuf(L, &G(L)->tmpbuf, s->len);
  126. return FFH_RETRY;
  127. }
  128. LJLIB_ASM_(string_lower)
  129. LJLIB_ASM_(string_upper)
  130. /* ------------------------------------------------------------------------ */
  131. static int writer_buf(lua_State *L, const void *p, size_t size, void *b)
  132. {
  133. luaL_addlstring((luaL_Buffer *)b, (const char *)p, size);
  134. UNUSED(L);
  135. return 0;
  136. }
  137. LJLIB_CF(string_dump)
  138. {
  139. GCfunc *fn = lj_lib_checkfunc(L, 1);
  140. int strip = L->base+1 < L->top && tvistruecond(L->base+1);
  141. luaL_Buffer b;
  142. L->top = L->base+1;
  143. luaL_buffinit(L, &b);
  144. if (!isluafunc(fn) || lj_bcwrite(L, funcproto(fn), writer_buf, &b, strip))
  145. lj_err_caller(L, LJ_ERR_STRDUMP);
  146. luaL_pushresult(&b);
  147. return 1;
  148. }
  149. /* ------------------------------------------------------------------------ */
  150. /* macro to `unsign' a character */
  151. #define uchar(c) ((unsigned char)(c))
  152. #define CAP_UNFINISHED (-1)
  153. #define CAP_POSITION (-2)
  154. typedef struct MatchState {
  155. const char *src_init; /* init of source string */
  156. const char *src_end; /* end (`\0') of source string */
  157. lua_State *L;
  158. int level; /* total number of captures (finished or unfinished) */
  159. int depth;
  160. struct {
  161. const char *init;
  162. ptrdiff_t len;
  163. } capture[LUA_MAXCAPTURES];
  164. } MatchState;
  165. #define L_ESC '%'
  166. #define SPECIALS "^$*+?.([%-"
  167. static int check_capture(MatchState *ms, int l)
  168. {
  169. l -= '1';
  170. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  171. lj_err_caller(ms->L, LJ_ERR_STRCAPI);
  172. return l;
  173. }
  174. static int capture_to_close(MatchState *ms)
  175. {
  176. int level = ms->level;
  177. for (level--; level>=0; level--)
  178. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  179. lj_err_caller(ms->L, LJ_ERR_STRPATC);
  180. return 0; /* unreachable */
  181. }
  182. static const char *classend(MatchState *ms, const char *p)
  183. {
  184. switch (*p++) {
  185. case L_ESC:
  186. if (*p == '\0')
  187. lj_err_caller(ms->L, LJ_ERR_STRPATE);
  188. return p+1;
  189. case '[':
  190. if (*p == '^') p++;
  191. do { /* look for a `]' */
  192. if (*p == '\0')
  193. lj_err_caller(ms->L, LJ_ERR_STRPATM);
  194. if (*(p++) == L_ESC && *p != '\0')
  195. p++; /* skip escapes (e.g. `%]') */
  196. } while (*p != ']');
  197. return p+1;
  198. default:
  199. return p;
  200. }
  201. }
  202. static const unsigned char match_class_map[32] = {
  203. 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0,
  204. LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0,
  205. LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0
  206. };
  207. static int match_class(int c, int cl)
  208. {
  209. if ((cl & 0xc0) == 0x40) {
  210. int t = match_class_map[(cl&0x1f)];
  211. if (t) {
  212. t = lj_char_isa(c, t);
  213. return (cl & 0x20) ? t : !t;
  214. }
  215. if (cl == 'z') return c == 0;
  216. if (cl == 'Z') return c != 0;
  217. }
  218. return (cl == c);
  219. }
  220. static int matchbracketclass(int c, const char *p, const char *ec)
  221. {
  222. int sig = 1;
  223. if (*(p+1) == '^') {
  224. sig = 0;
  225. p++; /* skip the `^' */
  226. }
  227. while (++p < ec) {
  228. if (*p == L_ESC) {
  229. p++;
  230. if (match_class(c, uchar(*p)))
  231. return sig;
  232. }
  233. else if ((*(p+1) == '-') && (p+2 < ec)) {
  234. p+=2;
  235. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  236. return sig;
  237. }
  238. else if (uchar(*p) == c) return sig;
  239. }
  240. return !sig;
  241. }
  242. static int singlematch(int c, const char *p, const char *ep)
  243. {
  244. switch (*p) {
  245. case '.': return 1; /* matches any char */
  246. case L_ESC: return match_class(c, uchar(*(p+1)));
  247. case '[': return matchbracketclass(c, p, ep-1);
  248. default: return (uchar(*p) == c);
  249. }
  250. }
  251. static const char *match(MatchState *ms, const char *s, const char *p);
  252. static const char *matchbalance(MatchState *ms, const char *s, const char *p)
  253. {
  254. if (*p == 0 || *(p+1) == 0)
  255. lj_err_caller(ms->L, LJ_ERR_STRPATU);
  256. if (*s != *p) {
  257. return NULL;
  258. } else {
  259. int b = *p;
  260. int e = *(p+1);
  261. int cont = 1;
  262. while (++s < ms->src_end) {
  263. if (*s == e) {
  264. if (--cont == 0) return s+1;
  265. } else if (*s == b) {
  266. cont++;
  267. }
  268. }
  269. }
  270. return NULL; /* string ends out of balance */
  271. }
  272. static const char *max_expand(MatchState *ms, const char *s,
  273. const char *p, const char *ep)
  274. {
  275. ptrdiff_t i = 0; /* counts maximum expand for item */
  276. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
  277. i++;
  278. /* keeps trying to match with the maximum repetitions */
  279. while (i>=0) {
  280. const char *res = match(ms, (s+i), ep+1);
  281. if (res) return res;
  282. i--; /* else didn't match; reduce 1 repetition to try again */
  283. }
  284. return NULL;
  285. }
  286. static const char *min_expand(MatchState *ms, const char *s,
  287. const char *p, const char *ep)
  288. {
  289. for (;;) {
  290. const char *res = match(ms, s, ep+1);
  291. if (res != NULL)
  292. return res;
  293. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
  294. s++; /* try with one more repetition */
  295. else
  296. return NULL;
  297. }
  298. }
  299. static const char *start_capture(MatchState *ms, const char *s,
  300. const char *p, int what)
  301. {
  302. const char *res;
  303. int level = ms->level;
  304. if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN);
  305. ms->capture[level].init = s;
  306. ms->capture[level].len = what;
  307. ms->level = level+1;
  308. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  309. ms->level--; /* undo capture */
  310. return res;
  311. }
  312. static const char *end_capture(MatchState *ms, const char *s,
  313. const char *p)
  314. {
  315. int l = capture_to_close(ms);
  316. const char *res;
  317. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  318. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  319. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  320. return res;
  321. }
  322. static const char *match_capture(MatchState *ms, const char *s, int l)
  323. {
  324. size_t len;
  325. l = check_capture(ms, l);
  326. len = (size_t)ms->capture[l].len;
  327. if ((size_t)(ms->src_end-s) >= len &&
  328. memcmp(ms->capture[l].init, s, len) == 0)
  329. return s+len;
  330. else
  331. return NULL;
  332. }
  333. static const char *match(MatchState *ms, const char *s, const char *p)
  334. {
  335. if (++ms->depth > LJ_MAX_XLEVEL)
  336. lj_err_caller(ms->L, LJ_ERR_STRPATX);
  337. init: /* using goto's to optimize tail recursion */
  338. switch (*p) {
  339. case '(': /* start capture */
  340. if (*(p+1) == ')') /* position capture? */
  341. s = start_capture(ms, s, p+2, CAP_POSITION);
  342. else
  343. s = start_capture(ms, s, p+1, CAP_UNFINISHED);
  344. break;
  345. case ')': /* end capture */
  346. s = end_capture(ms, s, p+1);
  347. break;
  348. case L_ESC:
  349. switch (*(p+1)) {
  350. case 'b': /* balanced string? */
  351. s = matchbalance(ms, s, p+2);
  352. if (s == NULL) break;
  353. p+=4;
  354. goto init; /* else s = match(ms, s, p+4); */
  355. case 'f': { /* frontier? */
  356. const char *ep; char previous;
  357. p += 2;
  358. if (*p != '[')
  359. lj_err_caller(ms->L, LJ_ERR_STRPATB);
  360. ep = classend(ms, p); /* points to what is next */
  361. previous = (s == ms->src_init) ? '\0' : *(s-1);
  362. if (matchbracketclass(uchar(previous), p, ep-1) ||
  363. !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; }
  364. p=ep;
  365. goto init; /* else s = match(ms, s, ep); */
  366. }
  367. default:
  368. if (lj_char_isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
  369. s = match_capture(ms, s, uchar(*(p+1)));
  370. if (s == NULL) break;
  371. p+=2;
  372. goto init; /* else s = match(ms, s, p+2) */
  373. }
  374. goto dflt; /* case default */
  375. }
  376. break;
  377. case '\0': /* end of pattern */
  378. break; /* match succeeded */
  379. case '$':
  380. /* is the `$' the last char in pattern? */
  381. if (*(p+1) != '\0') goto dflt;
  382. if (s != ms->src_end) s = NULL; /* check end of string */
  383. break;
  384. default: dflt: { /* it is a pattern item */
  385. const char *ep = classend(ms, p); /* points to what is next */
  386. int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
  387. switch (*ep) {
  388. case '?': { /* optional */
  389. const char *res;
  390. if (m && ((res=match(ms, s+1, ep+1)) != NULL)) {
  391. s = res;
  392. break;
  393. }
  394. p=ep+1;
  395. goto init; /* else s = match(ms, s, ep+1); */
  396. }
  397. case '*': /* 0 or more repetitions */
  398. s = max_expand(ms, s, p, ep);
  399. break;
  400. case '+': /* 1 or more repetitions */
  401. s = (m ? max_expand(ms, s+1, p, ep) : NULL);
  402. break;
  403. case '-': /* 0 or more repetitions (minimum) */
  404. s = min_expand(ms, s, p, ep);
  405. break;
  406. default:
  407. if (m) { s++; p=ep; goto init; } /* else s = match(ms, s+1, ep); */
  408. s = NULL;
  409. break;
  410. }
  411. break;
  412. }
  413. }
  414. ms->depth--;
  415. return s;
  416. }
  417. static const char *lmemfind(const char *s1, size_t l1,
  418. const char *s2, size_t l2)
  419. {
  420. if (l2 == 0) {
  421. return s1; /* empty strings are everywhere */
  422. } else if (l2 > l1) {
  423. return NULL; /* avoids a negative `l1' */
  424. } else {
  425. const char *init; /* to search for a `*s2' inside `s1' */
  426. l2--; /* 1st char will be checked by `memchr' */
  427. l1 = l1-l2; /* `s2' cannot be found after that */
  428. while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
  429. init++; /* 1st char is already checked */
  430. if (memcmp(init, s2+1, l2) == 0) {
  431. return init-1;
  432. } else { /* correct `l1' and `s1' to try again */
  433. l1 -= (size_t)(init-s1);
  434. s1 = init;
  435. }
  436. }
  437. return NULL; /* not found */
  438. }
  439. }
  440. static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
  441. {
  442. if (i >= ms->level) {
  443. if (i == 0) /* ms->level == 0, too */
  444. lua_pushlstring(ms->L, s, (size_t)(e - s)); /* add whole match */
  445. else
  446. lj_err_caller(ms->L, LJ_ERR_STRCAPI);
  447. } else {
  448. ptrdiff_t l = ms->capture[i].len;
  449. if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU);
  450. if (l == CAP_POSITION)
  451. lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
  452. else
  453. lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l);
  454. }
  455. }
  456. static int push_captures(MatchState *ms, const char *s, const char *e)
  457. {
  458. int i;
  459. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  460. luaL_checkstack(ms->L, nlevels, "too many captures");
  461. for (i = 0; i < nlevels; i++)
  462. push_onecapture(ms, i, s, e);
  463. return nlevels; /* number of strings pushed */
  464. }
  465. static ptrdiff_t posrelat(ptrdiff_t pos, size_t len)
  466. {
  467. /* relative string position: negative means back from end */
  468. if (pos < 0) pos += (ptrdiff_t)len + 1;
  469. return (pos >= 0) ? pos : 0;
  470. }
  471. static int str_find_aux(lua_State *L, int find)
  472. {
  473. size_t l1, l2;
  474. const char *s = luaL_checklstring(L, 1, &l1);
  475. const char *p = luaL_checklstring(L, 2, &l2);
  476. ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
  477. if (init < 0) {
  478. init = 0;
  479. } else if ((size_t)(init) > l1) {
  480. #if LJ_52
  481. setnilV(L->top-1);
  482. return 1;
  483. #else
  484. init = (ptrdiff_t)l1;
  485. #endif
  486. }
  487. if (find && (lua_toboolean(L, 4) || /* explicit request? */
  488. strpbrk(p, SPECIALS) == NULL)) { /* or no special characters? */
  489. /* do a plain search */
  490. const char *s2 = lmemfind(s+init, l1-(size_t)init, p, l2);
  491. if (s2) {
  492. lua_pushinteger(L, s2-s+1);
  493. lua_pushinteger(L, s2-s+(ptrdiff_t)l2);
  494. return 2;
  495. }
  496. } else {
  497. MatchState ms;
  498. int anchor = (*p == '^') ? (p++, 1) : 0;
  499. const char *s1=s+init;
  500. ms.L = L;
  501. ms.src_init = s;
  502. ms.src_end = s+l1;
  503. do {
  504. const char *res;
  505. ms.level = ms.depth = 0;
  506. if ((res=match(&ms, s1, p)) != NULL) {
  507. if (find) {
  508. lua_pushinteger(L, s1-s+1); /* start */
  509. lua_pushinteger(L, res-s); /* end */
  510. return push_captures(&ms, NULL, 0) + 2;
  511. } else {
  512. return push_captures(&ms, s1, res);
  513. }
  514. }
  515. } while (s1++ < ms.src_end && !anchor);
  516. }
  517. lua_pushnil(L); /* not found */
  518. return 1;
  519. }
  520. LJLIB_CF(string_find)
  521. {
  522. return str_find_aux(L, 1);
  523. }
  524. LJLIB_CF(string_match)
  525. {
  526. return str_find_aux(L, 0);
  527. }
  528. LJLIB_NOREG LJLIB_CF(string_gmatch_aux)
  529. {
  530. const char *p = strVdata(lj_lib_upvalue(L, 2));
  531. GCstr *str = strV(lj_lib_upvalue(L, 1));
  532. const char *s = strdata(str);
  533. TValue *tvpos = lj_lib_upvalue(L, 3);
  534. const char *src = s + tvpos->u32.lo;
  535. MatchState ms;
  536. ms.L = L;
  537. ms.src_init = s;
  538. ms.src_end = s + str->len;
  539. for (; src <= ms.src_end; src++) {
  540. const char *e;
  541. ms.level = ms.depth = 0;
  542. if ((e = match(&ms, src, p)) != NULL) {
  543. int32_t pos = (int32_t)(e - s);
  544. if (e == src) pos++; /* Ensure progress for empty match. */
  545. tvpos->u32.lo = (uint32_t)pos;
  546. return push_captures(&ms, src, e);
  547. }
  548. }
  549. return 0; /* not found */
  550. }
  551. LJLIB_CF(string_gmatch)
  552. {
  553. lj_lib_checkstr(L, 1);
  554. lj_lib_checkstr(L, 2);
  555. L->top = L->base+3;
  556. (L->top-1)->u64 = 0;
  557. lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3);
  558. return 1;
  559. }
  560. static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
  561. {
  562. size_t l, i;
  563. const char *news = lua_tolstring(ms->L, 3, &l);
  564. for (i = 0; i < l; i++) {
  565. if (news[i] != L_ESC) {
  566. luaL_addchar(b, news[i]);
  567. } else {
  568. i++; /* skip ESC */
  569. if (!lj_char_isdigit(uchar(news[i]))) {
  570. luaL_addchar(b, news[i]);
  571. } else if (news[i] == '0') {
  572. luaL_addlstring(b, s, (size_t)(e - s));
  573. } else {
  574. push_onecapture(ms, news[i] - '1', s, e);
  575. luaL_addvalue(b); /* add capture to accumulated result */
  576. }
  577. }
  578. }
  579. }
  580. static void add_value(MatchState *ms, luaL_Buffer *b,
  581. const char *s, const char *e)
  582. {
  583. lua_State *L = ms->L;
  584. switch (lua_type(L, 3)) {
  585. case LUA_TNUMBER:
  586. case LUA_TSTRING: {
  587. add_s(ms, b, s, e);
  588. return;
  589. }
  590. case LUA_TFUNCTION: {
  591. int n;
  592. lua_pushvalue(L, 3);
  593. n = push_captures(ms, s, e);
  594. lua_call(L, n, 1);
  595. break;
  596. }
  597. case LUA_TTABLE: {
  598. push_onecapture(ms, 0, s, e);
  599. lua_gettable(L, 3);
  600. break;
  601. }
  602. }
  603. if (!lua_toboolean(L, -1)) { /* nil or false? */
  604. lua_pop(L, 1);
  605. lua_pushlstring(L, s, (size_t)(e - s)); /* keep original text */
  606. } else if (!lua_isstring(L, -1)) {
  607. lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1));
  608. }
  609. luaL_addvalue(b); /* add result to accumulator */
  610. }
  611. LJLIB_CF(string_gsub)
  612. {
  613. size_t srcl;
  614. const char *src = luaL_checklstring(L, 1, &srcl);
  615. const char *p = luaL_checkstring(L, 2);
  616. int tr = lua_type(L, 3);
  617. int max_s = luaL_optint(L, 4, (int)(srcl+1));
  618. int anchor = (*p == '^') ? (p++, 1) : 0;
  619. int n = 0;
  620. MatchState ms;
  621. luaL_Buffer b;
  622. if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING ||
  623. tr == LUA_TFUNCTION || tr == LUA_TTABLE))
  624. lj_err_arg(L, 3, LJ_ERR_NOSFT);
  625. luaL_buffinit(L, &b);
  626. ms.L = L;
  627. ms.src_init = src;
  628. ms.src_end = src+srcl;
  629. while (n < max_s) {
  630. const char *e;
  631. ms.level = ms.depth = 0;
  632. e = match(&ms, src, p);
  633. if (e) {
  634. n++;
  635. add_value(&ms, &b, src, e);
  636. }
  637. if (e && e>src) /* non empty match? */
  638. src = e; /* skip it */
  639. else if (src < ms.src_end)
  640. luaL_addchar(&b, *src++);
  641. else
  642. break;
  643. if (anchor)
  644. break;
  645. }
  646. luaL_addlstring(&b, src, (size_t)(ms.src_end-src));
  647. luaL_pushresult(&b);
  648. lua_pushinteger(L, n); /* number of substitutions */
  649. return 2;
  650. }
  651. /* ------------------------------------------------------------------------ */
  652. /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
  653. #define MAX_FMTITEM 512
  654. /* valid flags in a format specification */
  655. #define FMT_FLAGS "-+ #0"
  656. /*
  657. ** maximum size of each format specification (such as '%-099.99d')
  658. ** (+10 accounts for %99.99x plus margin of error)
  659. */
  660. #define MAX_FMTSPEC (sizeof(FMT_FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
  661. static void addquoted(lua_State *L, luaL_Buffer *b, int arg)
  662. {
  663. GCstr *str = lj_lib_checkstr(L, arg);
  664. int32_t len = (int32_t)str->len;
  665. const char *s = strdata(str);
  666. luaL_addchar(b, '"');
  667. while (len--) {
  668. uint32_t c = uchar(*s);
  669. if (c == '"' || c == '\\' || c == '\n') {
  670. luaL_addchar(b, '\\');
  671. } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */
  672. uint32_t d;
  673. luaL_addchar(b, '\\');
  674. if (c >= 100 || lj_char_isdigit(uchar(s[1]))) {
  675. luaL_addchar(b, '0'+(c >= 100)); if (c >= 100) c -= 100;
  676. goto tens;
  677. } else if (c >= 10) {
  678. tens:
  679. d = (c * 205) >> 11; c -= d * 10; luaL_addchar(b, '0'+d);
  680. }
  681. c += '0';
  682. }
  683. luaL_addchar(b, c);
  684. s++;
  685. }
  686. luaL_addchar(b, '"');
  687. }
  688. static const char *scanformat(lua_State *L, const char *strfrmt, char *form)
  689. {
  690. const char *p = strfrmt;
  691. while (*p != '\0' && strchr(FMT_FLAGS, *p) != NULL) p++; /* skip flags */
  692. if ((size_t)(p - strfrmt) >= sizeof(FMT_FLAGS))
  693. lj_err_caller(L, LJ_ERR_STRFMTR);
  694. if (lj_char_isdigit(uchar(*p))) p++; /* skip width */
  695. if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */
  696. if (*p == '.') {
  697. p++;
  698. if (lj_char_isdigit(uchar(*p))) p++; /* skip precision */
  699. if (lj_char_isdigit(uchar(*p))) p++; /* (2 digits at most) */
  700. }
  701. if (lj_char_isdigit(uchar(*p)))
  702. lj_err_caller(L, LJ_ERR_STRFMTW);
  703. *(form++) = '%';
  704. strncpy(form, strfrmt, (size_t)(p - strfrmt + 1));
  705. form += p - strfrmt + 1;
  706. *form = '\0';
  707. return p;
  708. }
  709. static void addintlen(char *form)
  710. {
  711. size_t l = strlen(form);
  712. char spec = form[l - 1];
  713. strcpy(form + l - 1, LUA_INTFRMLEN);
  714. form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
  715. form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
  716. }
  717. static unsigned LUA_INTFRM_T num2intfrm(lua_State *L, int arg)
  718. {
  719. if (sizeof(LUA_INTFRM_T) == 4) {
  720. return (LUA_INTFRM_T)lj_lib_checkbit(L, arg);
  721. } else {
  722. cTValue *o;
  723. lj_lib_checknumber(L, arg);
  724. o = L->base+arg-1;
  725. if (tvisint(o))
  726. return (LUA_INTFRM_T)intV(o);
  727. else
  728. return (LUA_INTFRM_T)numV(o);
  729. }
  730. }
  731. static unsigned LUA_INTFRM_T num2uintfrm(lua_State *L, int arg)
  732. {
  733. if (sizeof(LUA_INTFRM_T) == 4) {
  734. return (unsigned LUA_INTFRM_T)lj_lib_checkbit(L, arg);
  735. } else {
  736. cTValue *o;
  737. lj_lib_checknumber(L, arg);
  738. o = L->base+arg-1;
  739. if (tvisint(o))
  740. return (unsigned LUA_INTFRM_T)intV(o);
  741. else if ((int32_t)o->u32.hi < 0)
  742. return (unsigned LUA_INTFRM_T)(LUA_INTFRM_T)numV(o);
  743. else
  744. return (unsigned LUA_INTFRM_T)numV(o);
  745. }
  746. }
  747. static GCstr *meta_tostring(lua_State *L, int arg)
  748. {
  749. TValue *o = L->base+arg-1;
  750. cTValue *mo;
  751. lua_assert(o < L->top); /* Caller already checks for existence. */
  752. if (LJ_LIKELY(tvisstr(o)))
  753. return strV(o);
  754. if (!tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
  755. copyTV(L, L->top++, mo);
  756. copyTV(L, L->top++, o);
  757. lua_call(L, 1, 1);
  758. L->top--;
  759. if (tvisstr(L->top))
  760. return strV(L->top);
  761. o = L->base+arg-1;
  762. copyTV(L, o, L->top);
  763. }
  764. if (tvisnumber(o)) {
  765. return lj_str_fromnumber(L, o);
  766. } else if (tvisnil(o)) {
  767. return lj_str_newlit(L, "nil");
  768. } else if (tvisfalse(o)) {
  769. return lj_str_newlit(L, "false");
  770. } else if (tvistrue(o)) {
  771. return lj_str_newlit(L, "true");
  772. } else {
  773. if (tvisfunc(o) && isffunc(funcV(o)))
  774. lj_str_pushf(L, "function: builtin#%d", funcV(o)->c.ffid);
  775. else
  776. lj_str_pushf(L, "%s: %p", lj_typename(o), lua_topointer(L, arg));
  777. L->top--;
  778. return strV(L->top);
  779. }
  780. }
  781. LJLIB_CF(string_format)
  782. {
  783. int arg = 1, top = (int)(L->top - L->base);
  784. GCstr *fmt = lj_lib_checkstr(L, arg);
  785. const char *strfrmt = strdata(fmt);
  786. const char *strfrmt_end = strfrmt + fmt->len;
  787. luaL_Buffer b;
  788. luaL_buffinit(L, &b);
  789. while (strfrmt < strfrmt_end) {
  790. if (*strfrmt != L_ESC) {
  791. luaL_addchar(&b, *strfrmt++);
  792. } else if (*++strfrmt == L_ESC) {
  793. luaL_addchar(&b, *strfrmt++); /* %% */
  794. } else { /* format item */
  795. char form[MAX_FMTSPEC]; /* to store the format (`%...') */
  796. char buff[MAX_FMTITEM]; /* to store the formatted item */
  797. if (++arg > top)
  798. luaL_argerror(L, arg, lj_obj_typename[0]);
  799. strfrmt = scanformat(L, strfrmt, form);
  800. switch (*strfrmt++) {
  801. case 'c':
  802. sprintf(buff, form, lj_lib_checkint(L, arg));
  803. break;
  804. case 'd': case 'i':
  805. addintlen(form);
  806. sprintf(buff, form, num2intfrm(L, arg));
  807. break;
  808. case 'o': case 'u': case 'x': case 'X':
  809. addintlen(form);
  810. sprintf(buff, form, num2uintfrm(L, arg));
  811. break;
  812. case 'e': case 'E': case 'f': case 'g': case 'G': case 'a': case 'A': {
  813. TValue tv;
  814. tv.n = lj_lib_checknum(L, arg);
  815. if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) {
  816. /* Canonicalize output of non-finite values. */
  817. char *p, nbuf[LJ_STR_NUMBUF];
  818. size_t len = lj_str_bufnum(nbuf, &tv);
  819. if (strfrmt[-1] < 'a') {
  820. nbuf[len-3] = nbuf[len-3] - 0x20;
  821. nbuf[len-2] = nbuf[len-2] - 0x20;
  822. nbuf[len-1] = nbuf[len-1] - 0x20;
  823. }
  824. nbuf[len] = '\0';
  825. for (p = form; *p < 'A' && *p != '.'; p++) ;
  826. *p++ = 's'; *p = '\0';
  827. sprintf(buff, form, nbuf);
  828. break;
  829. }
  830. sprintf(buff, form, (double)tv.n);
  831. break;
  832. }
  833. case 'q':
  834. addquoted(L, &b, arg);
  835. continue;
  836. case 'p':
  837. lj_str_pushf(L, "%p", lua_topointer(L, arg));
  838. luaL_addvalue(&b);
  839. continue;
  840. case 's': {
  841. GCstr *str = meta_tostring(L, arg);
  842. if (!strchr(form, '.') && str->len >= 100) {
  843. /* no precision and string is too long to be formatted;
  844. keep original string */
  845. setstrV(L, L->top++, str);
  846. luaL_addvalue(&b);
  847. continue;
  848. }
  849. sprintf(buff, form, strdata(str));
  850. break;
  851. }
  852. default:
  853. lj_err_callerv(L, LJ_ERR_STRFMTO, *(strfrmt -1));
  854. break;
  855. }
  856. luaL_addlstring(&b, buff, strlen(buff));
  857. }
  858. }
  859. luaL_pushresult(&b);
  860. return 1;
  861. }
  862. /* ------------------------------------------------------------------------ */
  863. #include "lj_libdef.h"
  864. LUALIB_API int luaopen_string(lua_State *L)
  865. {
  866. GCtab *mt;
  867. global_State *g;
  868. LJ_LIB_REG(L, LUA_STRLIBNAME, string);
  869. #if defined(LUA_COMPAT_GFIND) && !LJ_52
  870. lua_getfield(L, -1, "gmatch");
  871. lua_setfield(L, -2, "gfind");
  872. #endif
  873. mt = lj_tab_new(L, 0, 1);
  874. /* NOBARRIER: basemt is a GC root. */
  875. g = G(L);
  876. setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt));
  877. settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1));
  878. mt->nomm = (uint8_t)(~(1u<<MM_index));
  879. return 1;
  880. }