lib_string.c 23 KB

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