lib_string.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. ** String library.
  3. ** Copyright (C) 2005-2023 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. #define lib_string_c
  9. #define LUA_LIB
  10. #include "lua.h"
  11. #include "lauxlib.h"
  12. #include "lualib.h"
  13. #include "lj_obj.h"
  14. #include "lj_gc.h"
  15. #include "lj_err.h"
  16. #include "lj_buf.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_strfmt.h"
  25. #include "lj_lib.h"
  26. /* ------------------------------------------------------------------------ */
  27. #define LJLIB_MODULE_string
  28. LJLIB_LUA(string_len) /*
  29. function(s)
  30. CHECK_str(s)
  31. return #s
  32. end
  33. */
  34. LJLIB_ASM(string_byte) LJLIB_REC(string_range 0)
  35. {
  36. GCstr *s = lj_lib_checkstr(L, 1);
  37. int32_t len = (int32_t)s->len;
  38. int32_t start = lj_lib_optint(L, 2, 1);
  39. int32_t stop = lj_lib_optint(L, 3, start);
  40. int32_t n, i;
  41. const unsigned char *p;
  42. if (stop < 0) stop += len+1;
  43. if (start < 0) start += len+1;
  44. if (start <= 0) start = 1;
  45. if (stop > len) stop = len;
  46. if (start > stop) return FFH_RES(0); /* Empty interval: return no results. */
  47. start--;
  48. n = stop - start;
  49. if ((uint32_t)n > LUAI_MAXCSTACK)
  50. lj_err_caller(L, LJ_ERR_STRSLC);
  51. lj_state_checkstack(L, (MSize)n);
  52. p = (const unsigned char *)strdata(s) + start;
  53. for (i = 0; i < n; i++)
  54. setintV(L->base + i-1-LJ_FR2, p[i]);
  55. return FFH_RES(n);
  56. }
  57. LJLIB_ASM(string_char) LJLIB_REC(.)
  58. {
  59. int i, nargs = (int)(L->top - L->base);
  60. char *buf = lj_buf_tmp(L, (MSize)nargs);
  61. for (i = 1; i <= nargs; i++) {
  62. int32_t k = lj_lib_checkint(L, i);
  63. if (!checku8(k))
  64. lj_err_arg(L, i, LJ_ERR_BADVAL);
  65. buf[i-1] = (char)k;
  66. }
  67. setstrV(L, L->base-1-LJ_FR2, lj_str_new(L, buf, (size_t)nargs));
  68. return FFH_RES(1);
  69. }
  70. LJLIB_ASM(string_sub) LJLIB_REC(string_range 1)
  71. {
  72. lj_lib_checkstr(L, 1);
  73. lj_lib_checkint(L, 2);
  74. setintV(L->base+2, lj_lib_optint(L, 3, -1));
  75. return FFH_RETRY;
  76. }
  77. LJLIB_CF(string_rep) LJLIB_REC(.)
  78. {
  79. GCstr *s = lj_lib_checkstr(L, 1);
  80. int32_t rep = lj_lib_checkint(L, 2);
  81. GCstr *sep = lj_lib_optstr(L, 3);
  82. SBuf *sb = lj_buf_tmp_(L);
  83. if (sep && rep > 1) {
  84. GCstr *s2 = lj_buf_cat2str(L, sep, s);
  85. lj_buf_reset(sb);
  86. lj_buf_putstr(sb, s);
  87. s = s2;
  88. rep--;
  89. }
  90. sb = lj_buf_putstr_rep(sb, s, rep);
  91. setstrV(L, L->top-1, lj_buf_str(L, sb));
  92. lj_gc_check(L);
  93. return 1;
  94. }
  95. LJLIB_ASM(string_reverse) LJLIB_REC(string_op IRCALL_lj_buf_putstr_reverse)
  96. {
  97. lj_lib_checkstr(L, 1);
  98. return FFH_RETRY;
  99. }
  100. LJLIB_ASM_(string_lower) LJLIB_REC(string_op IRCALL_lj_buf_putstr_lower)
  101. LJLIB_ASM_(string_upper) LJLIB_REC(string_op IRCALL_lj_buf_putstr_upper)
  102. /* ------------------------------------------------------------------------ */
  103. static int writer_buf(lua_State *L, const void *p, size_t size, void *sb)
  104. {
  105. lj_buf_putmem((SBuf *)sb, p, (MSize)size);
  106. UNUSED(L);
  107. return 0;
  108. }
  109. LJLIB_CF(string_dump)
  110. {
  111. GCproto *pt = lj_lib_checkLproto(L, 1, 1);
  112. uint32_t flags = 0;
  113. SBuf *sb;
  114. TValue *o = L->base+1;
  115. if (o < L->top) {
  116. if (tvisstr(o)) {
  117. const char *mode = strVdata(o);
  118. char c;
  119. while ((c = *mode++)) {
  120. if (c == 's') flags |= BCDUMP_F_STRIP;
  121. if (c == 'd') flags |= BCDUMP_F_DETERMINISTIC;
  122. }
  123. } else if (tvistruecond(o)) {
  124. flags |= BCDUMP_F_STRIP;
  125. }
  126. }
  127. sb = lj_buf_tmp_(L); /* Assumes lj_bcwrite() doesn't use tmpbuf. */
  128. L->top = L->base+1;
  129. if (!pt || lj_bcwrite(L, pt, writer_buf, sb, flags))
  130. lj_err_caller(L, LJ_ERR_STRDUMP);
  131. setstrV(L, L->top-1, lj_buf_str(L, sb));
  132. lj_gc_check(L);
  133. return 1;
  134. }
  135. /* ------------------------------------------------------------------------ */
  136. /* macro to `unsign' a character */
  137. #define uchar(c) ((unsigned char)(c))
  138. #define CAP_UNFINISHED (-1)
  139. #define CAP_POSITION (-2)
  140. typedef struct MatchState {
  141. const char *src_init; /* init of source string */
  142. const char *src_end; /* end (`\0') of source string */
  143. lua_State *L;
  144. int level; /* total number of captures (finished or unfinished) */
  145. int depth;
  146. struct {
  147. const char *init;
  148. ptrdiff_t len;
  149. } capture[LUA_MAXCAPTURES];
  150. } MatchState;
  151. #define L_ESC '%'
  152. static int check_capture(MatchState *ms, int l)
  153. {
  154. l -= '1';
  155. if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
  156. lj_err_caller(ms->L, LJ_ERR_STRCAPI);
  157. return l;
  158. }
  159. static int capture_to_close(MatchState *ms)
  160. {
  161. int level = ms->level;
  162. for (level--; level>=0; level--)
  163. if (ms->capture[level].len == CAP_UNFINISHED) return level;
  164. lj_err_caller(ms->L, LJ_ERR_STRPATC);
  165. return 0; /* unreachable */
  166. }
  167. static const char *classend(MatchState *ms, const char *p)
  168. {
  169. switch (*p++) {
  170. case L_ESC:
  171. if (*p == '\0')
  172. lj_err_caller(ms->L, LJ_ERR_STRPATE);
  173. return p+1;
  174. case '[':
  175. if (*p == '^') p++;
  176. do { /* look for a `]' */
  177. if (*p == '\0')
  178. lj_err_caller(ms->L, LJ_ERR_STRPATM);
  179. if (*(p++) == L_ESC && *p != '\0')
  180. p++; /* skip escapes (e.g. `%]') */
  181. } while (*p != ']');
  182. return p+1;
  183. default:
  184. return p;
  185. }
  186. }
  187. static const unsigned char match_class_map[32] = {
  188. 0,LJ_CHAR_ALPHA,0,LJ_CHAR_CNTRL,LJ_CHAR_DIGIT,0,0,LJ_CHAR_GRAPH,0,0,0,0,
  189. LJ_CHAR_LOWER,0,0,0,LJ_CHAR_PUNCT,0,0,LJ_CHAR_SPACE,0,
  190. LJ_CHAR_UPPER,0,LJ_CHAR_ALNUM,LJ_CHAR_XDIGIT,0,0,0,0,0,0,0
  191. };
  192. static int match_class(int c, int cl)
  193. {
  194. if ((cl & 0xc0) == 0x40) {
  195. int t = match_class_map[(cl&0x1f)];
  196. if (t) {
  197. t = lj_char_isa(c, t);
  198. return (cl & 0x20) ? t : !t;
  199. }
  200. if (cl == 'z') return c == 0;
  201. if (cl == 'Z') return c != 0;
  202. }
  203. return (cl == c);
  204. }
  205. static int matchbracketclass(int c, const char *p, const char *ec)
  206. {
  207. int sig = 1;
  208. if (*(p+1) == '^') {
  209. sig = 0;
  210. p++; /* skip the `^' */
  211. }
  212. while (++p < ec) {
  213. if (*p == L_ESC) {
  214. p++;
  215. if (match_class(c, uchar(*p)))
  216. return sig;
  217. }
  218. else if ((*(p+1) == '-') && (p+2 < ec)) {
  219. p+=2;
  220. if (uchar(*(p-2)) <= c && c <= uchar(*p))
  221. return sig;
  222. }
  223. else if (uchar(*p) == c) return sig;
  224. }
  225. return !sig;
  226. }
  227. static int singlematch(int c, const char *p, const char *ep)
  228. {
  229. switch (*p) {
  230. case '.': return 1; /* matches any char */
  231. case L_ESC: return match_class(c, uchar(*(p+1)));
  232. case '[': return matchbracketclass(c, p, ep-1);
  233. default: return (uchar(*p) == c);
  234. }
  235. }
  236. static const char *match(MatchState *ms, const char *s, const char *p);
  237. static const char *matchbalance(MatchState *ms, const char *s, const char *p)
  238. {
  239. if (*p == 0 || *(p+1) == 0)
  240. lj_err_caller(ms->L, LJ_ERR_STRPATU);
  241. if (*s != *p) {
  242. return NULL;
  243. } else {
  244. int b = *p;
  245. int e = *(p+1);
  246. int cont = 1;
  247. while (++s < ms->src_end) {
  248. if (*s == e) {
  249. if (--cont == 0) return s+1;
  250. } else if (*s == b) {
  251. cont++;
  252. }
  253. }
  254. }
  255. return NULL; /* string ends out of balance */
  256. }
  257. static const char *max_expand(MatchState *ms, const char *s,
  258. const char *p, const char *ep)
  259. {
  260. ptrdiff_t i = 0; /* counts maximum expand for item */
  261. while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
  262. i++;
  263. /* keeps trying to match with the maximum repetitions */
  264. while (i>=0) {
  265. const char *res = match(ms, (s+i), ep+1);
  266. if (res) return res;
  267. i--; /* else didn't match; reduce 1 repetition to try again */
  268. }
  269. return NULL;
  270. }
  271. static const char *min_expand(MatchState *ms, const char *s,
  272. const char *p, const char *ep)
  273. {
  274. for (;;) {
  275. const char *res = match(ms, s, ep+1);
  276. if (res != NULL)
  277. return res;
  278. else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
  279. s++; /* try with one more repetition */
  280. else
  281. return NULL;
  282. }
  283. }
  284. static const char *start_capture(MatchState *ms, const char *s,
  285. const char *p, int what)
  286. {
  287. const char *res;
  288. int level = ms->level;
  289. if (level >= LUA_MAXCAPTURES) lj_err_caller(ms->L, LJ_ERR_STRCAPN);
  290. ms->capture[level].init = s;
  291. ms->capture[level].len = what;
  292. ms->level = level+1;
  293. if ((res=match(ms, s, p)) == NULL) /* match failed? */
  294. ms->level--; /* undo capture */
  295. return res;
  296. }
  297. static const char *end_capture(MatchState *ms, const char *s,
  298. const char *p)
  299. {
  300. int l = capture_to_close(ms);
  301. const char *res;
  302. ms->capture[l].len = s - ms->capture[l].init; /* close capture */
  303. if ((res = match(ms, s, p)) == NULL) /* match failed? */
  304. ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
  305. return res;
  306. }
  307. static const char *match_capture(MatchState *ms, const char *s, int l)
  308. {
  309. size_t len;
  310. l = check_capture(ms, l);
  311. len = (size_t)ms->capture[l].len;
  312. if ((size_t)(ms->src_end-s) >= len &&
  313. memcmp(ms->capture[l].init, s, len) == 0)
  314. return s+len;
  315. else
  316. return NULL;
  317. }
  318. static const char *match(MatchState *ms, const char *s, const char *p)
  319. {
  320. if (++ms->depth > LJ_MAX_XLEVEL)
  321. lj_err_caller(ms->L, LJ_ERR_STRPATX);
  322. init: /* using goto's to optimize tail recursion */
  323. switch (*p) {
  324. case '(': /* start capture */
  325. if (*(p+1) == ')') /* position capture? */
  326. s = start_capture(ms, s, p+2, CAP_POSITION);
  327. else
  328. s = start_capture(ms, s, p+1, CAP_UNFINISHED);
  329. break;
  330. case ')': /* end capture */
  331. s = end_capture(ms, s, p+1);
  332. break;
  333. case L_ESC:
  334. switch (*(p+1)) {
  335. case 'b': /* balanced string? */
  336. s = matchbalance(ms, s, p+2);
  337. if (s == NULL) break;
  338. p+=4;
  339. goto init; /* else s = match(ms, s, p+4); */
  340. case 'f': { /* frontier? */
  341. const char *ep; char previous;
  342. p += 2;
  343. if (*p != '[')
  344. lj_err_caller(ms->L, LJ_ERR_STRPATB);
  345. ep = classend(ms, p); /* points to what is next */
  346. previous = (s == ms->src_init) ? '\0' : *(s-1);
  347. if (matchbracketclass(uchar(previous), p, ep-1) ||
  348. !matchbracketclass(uchar(*s), p, ep-1)) { s = NULL; break; }
  349. p=ep;
  350. goto init; /* else s = match(ms, s, ep); */
  351. }
  352. default:
  353. if (lj_char_isdigit(uchar(*(p+1)))) { /* capture results (%0-%9)? */
  354. s = match_capture(ms, s, uchar(*(p+1)));
  355. if (s == NULL) break;
  356. p+=2;
  357. goto init; /* else s = match(ms, s, p+2) */
  358. }
  359. goto dflt; /* case default */
  360. }
  361. break;
  362. case '\0': /* end of pattern */
  363. break; /* match succeeded */
  364. case '$':
  365. /* is the `$' the last char in pattern? */
  366. if (*(p+1) != '\0') goto dflt;
  367. if (s != ms->src_end) s = NULL; /* check end of string */
  368. break;
  369. default: dflt: { /* it is a pattern item */
  370. const char *ep = classend(ms, p); /* points to what is next */
  371. int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
  372. switch (*ep) {
  373. case '?': { /* optional */
  374. const char *res;
  375. if (m && ((res=match(ms, s+1, ep+1)) != NULL)) {
  376. s = res;
  377. break;
  378. }
  379. p=ep+1;
  380. goto init; /* else s = match(ms, s, ep+1); */
  381. }
  382. case '*': /* 0 or more repetitions */
  383. s = max_expand(ms, s, p, ep);
  384. break;
  385. case '+': /* 1 or more repetitions */
  386. s = (m ? max_expand(ms, s+1, p, ep) : NULL);
  387. break;
  388. case '-': /* 0 or more repetitions (minimum) */
  389. s = min_expand(ms, s, p, ep);
  390. break;
  391. default:
  392. if (m) { s++; p=ep; goto init; } /* else s = match(ms, s+1, ep); */
  393. s = NULL;
  394. break;
  395. }
  396. break;
  397. }
  398. }
  399. ms->depth--;
  400. return s;
  401. }
  402. static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
  403. {
  404. if (i >= ms->level) {
  405. if (i == 0) /* ms->level == 0, too */
  406. lua_pushlstring(ms->L, s, (size_t)(e - s)); /* add whole match */
  407. else
  408. lj_err_caller(ms->L, LJ_ERR_STRCAPI);
  409. } else {
  410. ptrdiff_t l = ms->capture[i].len;
  411. if (l == CAP_UNFINISHED) lj_err_caller(ms->L, LJ_ERR_STRCAPU);
  412. if (l == CAP_POSITION)
  413. lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
  414. else
  415. lua_pushlstring(ms->L, ms->capture[i].init, (size_t)l);
  416. }
  417. }
  418. static int push_captures(MatchState *ms, const char *s, const char *e)
  419. {
  420. int i;
  421. int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
  422. luaL_checkstack(ms->L, nlevels, "too many captures");
  423. for (i = 0; i < nlevels; i++)
  424. push_onecapture(ms, i, s, e);
  425. return nlevels; /* number of strings pushed */
  426. }
  427. static int str_find_aux(lua_State *L, int find)
  428. {
  429. GCstr *s = lj_lib_checkstr(L, 1);
  430. GCstr *p = lj_lib_checkstr(L, 2);
  431. int32_t start = lj_lib_optint(L, 3, 1);
  432. MSize st;
  433. if (start < 0) start += (int32_t)s->len; else start--;
  434. if (start < 0) start = 0;
  435. st = (MSize)start;
  436. if (st > s->len) {
  437. #if LJ_52
  438. setnilV(L->top-1);
  439. return 1;
  440. #else
  441. st = s->len;
  442. #endif
  443. }
  444. if (find && ((L->base+3 < L->top && tvistruecond(L->base+3)) ||
  445. !lj_str_haspattern(p))) { /* Search for fixed string. */
  446. const char *q = lj_str_find(strdata(s)+st, strdata(p), s->len-st, p->len);
  447. if (q) {
  448. setintV(L->top-2, (int32_t)(q-strdata(s)) + 1);
  449. setintV(L->top-1, (int32_t)(q-strdata(s)) + (int32_t)p->len);
  450. return 2;
  451. }
  452. } else { /* Search for pattern. */
  453. MatchState ms;
  454. const char *pstr = strdata(p);
  455. const char *sstr = strdata(s) + st;
  456. int anchor = 0;
  457. if (*pstr == '^') { pstr++; anchor = 1; }
  458. ms.L = L;
  459. ms.src_init = strdata(s);
  460. ms.src_end = strdata(s) + s->len;
  461. do { /* Loop through string and try to match the pattern. */
  462. const char *q;
  463. ms.level = ms.depth = 0;
  464. q = match(&ms, sstr, pstr);
  465. if (q) {
  466. if (find) {
  467. setintV(L->top++, (int32_t)(sstr-(strdata(s)-1)));
  468. setintV(L->top++, (int32_t)(q-strdata(s)));
  469. return push_captures(&ms, NULL, NULL) + 2;
  470. } else {
  471. return push_captures(&ms, sstr, q);
  472. }
  473. }
  474. } while (sstr++ < ms.src_end && !anchor);
  475. }
  476. setnilV(L->top-1); /* Not found. */
  477. return 1;
  478. }
  479. LJLIB_CF(string_find) LJLIB_REC(.)
  480. {
  481. return str_find_aux(L, 1);
  482. }
  483. LJLIB_CF(string_match)
  484. {
  485. return str_find_aux(L, 0);
  486. }
  487. LJLIB_NOREG LJLIB_CF(string_gmatch_aux)
  488. {
  489. const char *p = strVdata(lj_lib_upvalue(L, 2));
  490. GCstr *str = strV(lj_lib_upvalue(L, 1));
  491. const char *s = strdata(str);
  492. TValue *tvpos = lj_lib_upvalue(L, 3);
  493. const char *src = s + tvpos->u32.lo;
  494. MatchState ms;
  495. ms.L = L;
  496. ms.src_init = s;
  497. ms.src_end = s + str->len;
  498. for (; src <= ms.src_end; src++) {
  499. const char *e;
  500. ms.level = ms.depth = 0;
  501. if ((e = match(&ms, src, p)) != NULL) {
  502. int32_t pos = (int32_t)(e - s);
  503. if (e == src) pos++; /* Ensure progress for empty match. */
  504. tvpos->u32.lo = (uint32_t)pos;
  505. return push_captures(&ms, src, e);
  506. }
  507. }
  508. return 0; /* not found */
  509. }
  510. LJLIB_CF(string_gmatch)
  511. {
  512. lj_lib_checkstr(L, 1);
  513. lj_lib_checkstr(L, 2);
  514. L->top = L->base+3;
  515. (L->top-1)->u64 = 0;
  516. lj_lib_pushcc(L, lj_cf_string_gmatch_aux, FF_string_gmatch_aux, 3);
  517. return 1;
  518. }
  519. static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
  520. {
  521. size_t l, i;
  522. const char *news = lua_tolstring(ms->L, 3, &l);
  523. for (i = 0; i < l; i++) {
  524. if (news[i] != L_ESC) {
  525. luaL_addchar(b, news[i]);
  526. } else {
  527. i++; /* skip ESC */
  528. if (!lj_char_isdigit(uchar(news[i]))) {
  529. luaL_addchar(b, news[i]);
  530. } else if (news[i] == '0') {
  531. luaL_addlstring(b, s, (size_t)(e - s));
  532. } else {
  533. push_onecapture(ms, news[i] - '1', s, e);
  534. luaL_addvalue(b); /* add capture to accumulated result */
  535. }
  536. }
  537. }
  538. }
  539. static void add_value(MatchState *ms, luaL_Buffer *b,
  540. const char *s, const char *e)
  541. {
  542. lua_State *L = ms->L;
  543. switch (lua_type(L, 3)) {
  544. case LUA_TNUMBER:
  545. case LUA_TSTRING: {
  546. add_s(ms, b, s, e);
  547. return;
  548. }
  549. case LUA_TFUNCTION: {
  550. int n;
  551. lua_pushvalue(L, 3);
  552. n = push_captures(ms, s, e);
  553. lua_call(L, n, 1);
  554. break;
  555. }
  556. case LUA_TTABLE: {
  557. push_onecapture(ms, 0, s, e);
  558. lua_gettable(L, 3);
  559. break;
  560. }
  561. }
  562. if (!lua_toboolean(L, -1)) { /* nil or false? */
  563. lua_pop(L, 1);
  564. lua_pushlstring(L, s, (size_t)(e - s)); /* keep original text */
  565. } else if (!lua_isstring(L, -1)) {
  566. lj_err_callerv(L, LJ_ERR_STRGSRV, luaL_typename(L, -1));
  567. }
  568. luaL_addvalue(b); /* add result to accumulator */
  569. }
  570. LJLIB_CF(string_gsub)
  571. {
  572. size_t srcl;
  573. const char *src = luaL_checklstring(L, 1, &srcl);
  574. const char *p = luaL_checkstring(L, 2);
  575. int tr = lua_type(L, 3);
  576. int max_s = luaL_optint(L, 4, (int)(srcl+1));
  577. int anchor = (*p == '^') ? (p++, 1) : 0;
  578. int n = 0;
  579. MatchState ms;
  580. luaL_Buffer b;
  581. if (!(tr == LUA_TNUMBER || tr == LUA_TSTRING ||
  582. tr == LUA_TFUNCTION || tr == LUA_TTABLE))
  583. lj_err_arg(L, 3, LJ_ERR_NOSFT);
  584. luaL_buffinit(L, &b);
  585. ms.L = L;
  586. ms.src_init = src;
  587. ms.src_end = src+srcl;
  588. while (n < max_s) {
  589. const char *e;
  590. ms.level = ms.depth = 0;
  591. e = match(&ms, src, p);
  592. if (e) {
  593. n++;
  594. add_value(&ms, &b, src, e);
  595. }
  596. if (e && e>src) /* non empty match? */
  597. src = e; /* skip it */
  598. else if (src < ms.src_end)
  599. luaL_addchar(&b, *src++);
  600. else
  601. break;
  602. if (anchor)
  603. break;
  604. }
  605. luaL_addlstring(&b, src, (size_t)(ms.src_end-src));
  606. luaL_pushresult(&b);
  607. lua_pushinteger(L, n); /* number of substitutions */
  608. return 2;
  609. }
  610. /* ------------------------------------------------------------------------ */
  611. LJLIB_CF(string_format) LJLIB_REC(.)
  612. {
  613. int retry = 0;
  614. SBuf *sb;
  615. do {
  616. sb = lj_buf_tmp_(L);
  617. retry = lj_strfmt_putarg(L, sb, 1, -retry);
  618. } while (retry > 0);
  619. setstrV(L, L->top-1, lj_buf_str(L, sb));
  620. lj_gc_check(L);
  621. return 1;
  622. }
  623. /* ------------------------------------------------------------------------ */
  624. #include "lj_libdef.h"
  625. LUALIB_API int luaopen_string(lua_State *L)
  626. {
  627. GCtab *mt;
  628. global_State *g;
  629. LJ_LIB_REG(L, LUA_STRLIBNAME, string);
  630. mt = lj_tab_new(L, 0, 1);
  631. /* NOBARRIER: basemt is a GC root. */
  632. g = G(L);
  633. setgcref(basemt_it(g, LJ_TSTR), obj2gco(mt));
  634. settabV(L, lj_tab_setstr(L, mt, mmname_str(g, MM_index)), tabV(L->top-1));
  635. mt->nomm = (uint8_t)(~(1u<<MM_index));
  636. #if LJ_HASBUFFER
  637. lj_lib_prereg(L, LUA_STRLIBNAME ".buffer", luaopen_string_buffer, tabV(L->top-1));
  638. #endif
  639. return 1;
  640. }