lj_strfmt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. ** String formatting.
  3. ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #include <stdio.h>
  6. #define lj_strfmt_c
  7. #define LUA_CORE
  8. #include "lj_obj.h"
  9. #include "lj_err.h"
  10. #include "lj_buf.h"
  11. #include "lj_str.h"
  12. #include "lj_meta.h"
  13. #include "lj_state.h"
  14. #include "lj_char.h"
  15. #include "lj_strfmt.h"
  16. #if LJ_HASFFI
  17. #include "lj_ctype.h"
  18. #endif
  19. #include "lj_lib.h"
  20. /* -- Format parser ------------------------------------------------------- */
  21. static const uint8_t strfmt_map[('x'-'A')+1] = {
  22. STRFMT_A,0,0,0,STRFMT_E,STRFMT_F,STRFMT_G,0,0,0,0,0,0,
  23. 0,0,0,0,0,0,0,0,0,0,STRFMT_X,0,0,
  24. 0,0,0,0,0,0,
  25. STRFMT_A,0,STRFMT_C,STRFMT_D,STRFMT_E,STRFMT_F,STRFMT_G,0,STRFMT_I,0,0,0,0,
  26. 0,STRFMT_O,STRFMT_P,STRFMT_Q,0,STRFMT_S,0,STRFMT_U,0,0,STRFMT_X
  27. };
  28. SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs)
  29. {
  30. const uint8_t *p = fs->p, *e = fs->e;
  31. fs->str = (const char *)p;
  32. for (; p < e; p++) {
  33. if (*p == '%') { /* Escape char? */
  34. if (p[1] == '%') { /* '%%'? */
  35. fs->p = ++p+1;
  36. goto retlit;
  37. } else {
  38. SFormat sf = 0;
  39. uint32_t c;
  40. if (p != (const uint8_t *)fs->str)
  41. break;
  42. for (p++; (uint32_t)*p - ' ' <= (uint32_t)('0' - ' '); p++) {
  43. /* Parse flags. */
  44. if (*p == '-') sf |= STRFMT_F_LEFT;
  45. else if (*p == '+') sf |= STRFMT_F_PLUS;
  46. else if (*p == '0') sf |= STRFMT_F_ZERO;
  47. else if (*p == ' ') sf |= STRFMT_F_SPACE;
  48. else if (*p == '#') sf |= STRFMT_F_ALT;
  49. else break;
  50. }
  51. if ((uint32_t)*p - '0' < 10) { /* Parse width. */
  52. uint32_t width = (uint32_t)*p++ - '0';
  53. if ((uint32_t)*p - '0' < 10)
  54. width = (uint32_t)*p++ - '0' + width*10;
  55. sf |= (width << STRFMT_SH_WIDTH);
  56. }
  57. if (*p == '.') { /* Parse precision. */
  58. uint32_t prec = 0;
  59. p++;
  60. if ((uint32_t)*p - '0' < 10) {
  61. prec = (uint32_t)*p++ - '0';
  62. if ((uint32_t)*p - '0' < 10)
  63. prec = (uint32_t)*p++ - '0' + prec*10;
  64. }
  65. sf |= ((prec+1) << STRFMT_SH_PREC);
  66. }
  67. /* Parse conversion. */
  68. c = (uint32_t)*p - 'A';
  69. if (LJ_LIKELY(c <= (uint32_t)('x' - 'A'))) {
  70. uint32_t sx = strfmt_map[c];
  71. if (sx) {
  72. fs->p = p+1;
  73. return (sf | sx | ((c & 0x20) ? 0 : STRFMT_F_UPPER));
  74. }
  75. }
  76. /* Return error location. */
  77. if (*p >= 32) p++;
  78. fs->len = (MSize)(p - (const uint8_t *)fs->str);
  79. fs->p = fs->e;
  80. return STRFMT_ERR;
  81. }
  82. }
  83. }
  84. fs->p = p;
  85. retlit:
  86. fs->len = (MSize)(p - (const uint8_t *)fs->str);
  87. return fs->len ? STRFMT_LIT : STRFMT_EOF;
  88. }
  89. /* -- Raw conversions ----------------------------------------------------- */
  90. #define WINT_R(x, sh, sc) \
  91. { uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); }
  92. /* Write integer to buffer. */
  93. char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k)
  94. {
  95. uint32_t u = (uint32_t)k;
  96. if (k < 0) { u = ~u+1u; *p++ = '-'; }
  97. if (u < 10000) {
  98. if (u < 10) goto dig1;
  99. if (u < 100) goto dig2;
  100. if (u < 1000) goto dig3;
  101. } else {
  102. uint32_t v = u / 10000; u -= v * 10000;
  103. if (v < 10000) {
  104. if (v < 10) goto dig5;
  105. if (v < 100) goto dig6;
  106. if (v < 1000) goto dig7;
  107. } else {
  108. uint32_t w = v / 10000; v -= w * 10000;
  109. if (w >= 10) WINT_R(w, 10, 10)
  110. *p++ = (char)('0'+w);
  111. }
  112. WINT_R(v, 23, 1000)
  113. dig7: WINT_R(v, 12, 100)
  114. dig6: WINT_R(v, 10, 10)
  115. dig5: *p++ = (char)('0'+v);
  116. }
  117. WINT_R(u, 23, 1000)
  118. dig3: WINT_R(u, 12, 100)
  119. dig2: WINT_R(u, 10, 10)
  120. dig1: *p++ = (char)('0'+u);
  121. return p;
  122. }
  123. #undef WINT_R
  124. /* Write pointer to buffer. */
  125. char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v)
  126. {
  127. ptrdiff_t x = (ptrdiff_t)v;
  128. MSize i, n = STRFMT_MAXBUF_PTR;
  129. if (x == 0) {
  130. *p++ = 'N'; *p++ = 'U'; *p++ = 'L'; *p++ = 'L';
  131. return p;
  132. }
  133. #if LJ_64
  134. /* Shorten output for 64 bit pointers. */
  135. n = 2+2*4+((x >> 32) ? 2+2*(lj_fls((uint32_t)(x >> 32))>>3) : 0);
  136. #endif
  137. p[0] = '0';
  138. p[1] = 'x';
  139. for (i = n-1; i >= 2; i--, x >>= 4)
  140. p[i] = "0123456789abcdef"[(x & 15)];
  141. return p+n;
  142. }
  143. /* Write ULEB128 to buffer. */
  144. char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v)
  145. {
  146. for (; v >= 0x80; v >>= 7)
  147. *p++ = (char)((v & 0x7f) | 0x80);
  148. *p++ = (char)v;
  149. return p;
  150. }
  151. /* Return string or write number to tmp buffer and return pointer to start. */
  152. const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp)
  153. {
  154. SBuf *sb;
  155. if (tvisstr(o)) {
  156. *lenp = strV(o)->len;
  157. return strVdata(o);
  158. } else if (tvisbuf(o)) {
  159. SBufExt *sbx = bufV(o);
  160. *lenp = sbufxlen(sbx);
  161. return sbx->r;
  162. } else if (tvisint(o)) {
  163. sb = lj_strfmt_putint(lj_buf_tmp_(L), intV(o));
  164. } else if (tvisnum(o)) {
  165. sb = lj_strfmt_putfnum(lj_buf_tmp_(L), STRFMT_G14, o->n);
  166. } else {
  167. return NULL;
  168. }
  169. *lenp = sbuflen(sb);
  170. return sb->b;
  171. }
  172. /* -- Unformatted conversions to buffer ----------------------------------- */
  173. /* Add integer to buffer. */
  174. SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k)
  175. {
  176. sb->w = lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k);
  177. return sb;
  178. }
  179. #if LJ_HASJIT
  180. /* Add number to buffer. */
  181. SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o)
  182. {
  183. return lj_strfmt_putfnum(sb, STRFMT_G14, o->n);
  184. }
  185. #endif
  186. SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v)
  187. {
  188. sb->w = lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), v);
  189. return sb;
  190. }
  191. /* Add quoted string to buffer. */
  192. static SBuf *strfmt_putquotedlen(SBuf *sb, const char *s, MSize len)
  193. {
  194. lj_buf_putb(sb, '"');
  195. while (len--) {
  196. uint32_t c = (uint32_t)(uint8_t)*s++;
  197. char *w = lj_buf_more(sb, 4);
  198. if (c == '"' || c == '\\' || c == '\n') {
  199. *w++ = '\\';
  200. } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */
  201. uint32_t d;
  202. *w++ = '\\';
  203. if (c >= 100 || lj_char_isdigit((uint8_t)*s)) {
  204. *w++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100;
  205. goto tens;
  206. } else if (c >= 10) {
  207. tens:
  208. d = (c * 205) >> 11; c -= d * 10; *w++ = (char)('0'+d);
  209. }
  210. c += '0';
  211. }
  212. *w++ = (char)c;
  213. sb->w = w;
  214. }
  215. lj_buf_putb(sb, '"');
  216. return sb;
  217. }
  218. #if LJ_HASJIT
  219. SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str)
  220. {
  221. return strfmt_putquotedlen(sb, strdata(str), str->len);
  222. }
  223. #endif
  224. /* -- Formatted conversions to buffer ------------------------------------- */
  225. /* Add formatted char to buffer. */
  226. SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat sf, int32_t c)
  227. {
  228. MSize width = STRFMT_WIDTH(sf);
  229. char *w = lj_buf_more(sb, width > 1 ? width : 1);
  230. if ((sf & STRFMT_F_LEFT)) *w++ = (char)c;
  231. while (width-- > 1) *w++ = ' ';
  232. if (!(sf & STRFMT_F_LEFT)) *w++ = (char)c;
  233. sb->w = w;
  234. return sb;
  235. }
  236. /* Add formatted string to buffer. */
  237. static SBuf *strfmt_putfstrlen(SBuf *sb, SFormat sf, const char *s, MSize len)
  238. {
  239. MSize width = STRFMT_WIDTH(sf);
  240. char *w;
  241. if (len > STRFMT_PREC(sf)) len = STRFMT_PREC(sf);
  242. w = lj_buf_more(sb, width > len ? width : len);
  243. if ((sf & STRFMT_F_LEFT)) w = lj_buf_wmem(w, s, len);
  244. while (width-- > len) *w++ = ' ';
  245. if (!(sf & STRFMT_F_LEFT)) w = lj_buf_wmem(w, s, len);
  246. sb->w = w;
  247. return sb;
  248. }
  249. #if LJ_HASJIT
  250. SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str)
  251. {
  252. return strfmt_putfstrlen(sb, sf, strdata(str), str->len);
  253. }
  254. #endif
  255. /* Add formatted signed/unsigned integer to buffer. */
  256. SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k)
  257. {
  258. char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *w;
  259. #ifdef LUA_USE_ASSERT
  260. char *ws;
  261. #endif
  262. MSize prefix = 0, len, prec, pprec, width, need;
  263. /* Figure out signed prefixes. */
  264. if (STRFMT_TYPE(sf) == STRFMT_INT) {
  265. if ((int64_t)k < 0) {
  266. k = ~k+1u;
  267. prefix = 256 + '-';
  268. } else if ((sf & STRFMT_F_PLUS)) {
  269. prefix = 256 + '+';
  270. } else if ((sf & STRFMT_F_SPACE)) {
  271. prefix = 256 + ' ';
  272. }
  273. }
  274. /* Convert number and store to fixed-size buffer in reverse order. */
  275. prec = STRFMT_PREC(sf);
  276. if ((int32_t)prec >= 0) sf &= ~STRFMT_F_ZERO;
  277. if (k == 0) { /* Special-case zero argument. */
  278. if (prec != 0 ||
  279. (sf & (STRFMT_T_OCT|STRFMT_F_ALT)) == (STRFMT_T_OCT|STRFMT_F_ALT))
  280. *--q = '0';
  281. } else if (!(sf & (STRFMT_T_HEX|STRFMT_T_OCT))) { /* Decimal. */
  282. uint32_t k2;
  283. while ((k >> 32)) { *--q = (char)('0' + k % 10); k /= 10; }
  284. k2 = (uint32_t)k;
  285. do { *--q = (char)('0' + k2 % 10); k2 /= 10; } while (k2);
  286. } else if ((sf & STRFMT_T_HEX)) { /* Hex. */
  287. const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEF" :
  288. "0123456789abcdef";
  289. do { *--q = hexdig[(k & 15)]; k >>= 4; } while (k);
  290. if ((sf & STRFMT_F_ALT)) prefix = 512 + ((sf & STRFMT_F_UPPER) ? 'X' : 'x');
  291. } else { /* Octal. */
  292. do { *--q = (char)('0' + (uint32_t)(k & 7)); k >>= 3; } while (k);
  293. if ((sf & STRFMT_F_ALT)) *--q = '0';
  294. }
  295. /* Calculate sizes. */
  296. len = (MSize)(buf + sizeof(buf) - q);
  297. if ((int32_t)len >= (int32_t)prec) prec = len;
  298. width = STRFMT_WIDTH(sf);
  299. pprec = prec + (prefix >> 8);
  300. need = width > pprec ? width : pprec;
  301. w = lj_buf_more(sb, need);
  302. #ifdef LUA_USE_ASSERT
  303. ws = w;
  304. #endif
  305. /* Format number with leading/trailing whitespace and zeros. */
  306. if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == 0)
  307. while (width-- > pprec) *w++ = ' ';
  308. if (prefix) {
  309. if ((char)prefix >= 'X') *w++ = '0';
  310. *w++ = (char)prefix;
  311. }
  312. if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == STRFMT_F_ZERO)
  313. while (width-- > pprec) *w++ = '0';
  314. while (prec-- > len) *w++ = '0';
  315. while (q < buf + sizeof(buf)) *w++ = *q++; /* Add number itself. */
  316. if ((sf & STRFMT_F_LEFT))
  317. while (width-- > pprec) *w++ = ' ';
  318. lj_assertX(need == (MSize)(w - ws), "miscalculated format size");
  319. sb->w = w;
  320. return sb;
  321. }
  322. /* Add number formatted as signed integer to buffer. */
  323. SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n)
  324. {
  325. int64_t k = (int64_t)n;
  326. if (checki32(k) && sf == STRFMT_INT)
  327. return lj_strfmt_putint(sb, (int32_t)k); /* Shortcut for plain %d. */
  328. else
  329. return lj_strfmt_putfxint(sb, sf, (uint64_t)k);
  330. }
  331. /* Add number formatted as unsigned integer to buffer. */
  332. SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n)
  333. {
  334. int64_t k;
  335. if (n >= 9223372036854775808.0)
  336. k = (int64_t)(n - 18446744073709551616.0);
  337. else
  338. k = (int64_t)n;
  339. return lj_strfmt_putfxint(sb, sf, (uint64_t)k);
  340. }
  341. /* Format stack arguments to buffer. */
  342. int lj_strfmt_putarg(lua_State *L, SBuf *sb, int arg, int retry)
  343. {
  344. int narg = (int)(L->top - L->base);
  345. GCstr *fmt = lj_lib_checkstr(L, arg);
  346. FormatState fs;
  347. SFormat sf;
  348. lj_strfmt_init(&fs, strdata(fmt), fmt->len);
  349. while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {
  350. if (sf == STRFMT_LIT) {
  351. lj_buf_putmem(sb, fs.str, fs.len);
  352. } else if (sf == STRFMT_ERR) {
  353. lj_err_callerv(L, LJ_ERR_STRFMT,
  354. strdata(lj_str_new(L, fs.str, fs.len)));
  355. } else {
  356. TValue *o = &L->base[arg++];
  357. if (arg > narg)
  358. lj_err_arg(L, arg, LJ_ERR_NOVAL);
  359. switch (STRFMT_TYPE(sf)) {
  360. case STRFMT_INT:
  361. if (tvisint(o)) {
  362. int32_t k = intV(o);
  363. if (sf == STRFMT_INT)
  364. lj_strfmt_putint(sb, k); /* Shortcut for plain %d. */
  365. else
  366. lj_strfmt_putfxint(sb, sf, k);
  367. break;
  368. }
  369. #if LJ_HASFFI
  370. if (tviscdata(o)) {
  371. GCcdata *cd = cdataV(o);
  372. if (cd->ctypeid == CTID_INT64 || cd->ctypeid == CTID_UINT64) {
  373. lj_strfmt_putfxint(sb, sf, *(uint64_t *)cdataptr(cd));
  374. break;
  375. }
  376. }
  377. #endif
  378. lj_strfmt_putfnum_int(sb, sf, lj_lib_checknum(L, arg));
  379. break;
  380. case STRFMT_UINT:
  381. if (tvisint(o)) {
  382. lj_strfmt_putfxint(sb, sf, intV(o));
  383. break;
  384. }
  385. #if LJ_HASFFI
  386. if (tviscdata(o)) {
  387. GCcdata *cd = cdataV(o);
  388. if (cd->ctypeid == CTID_INT64 || cd->ctypeid == CTID_UINT64) {
  389. lj_strfmt_putfxint(sb, sf, *(uint64_t *)cdataptr(cd));
  390. break;
  391. }
  392. }
  393. #endif
  394. lj_strfmt_putfnum_uint(sb, sf, lj_lib_checknum(L, arg));
  395. break;
  396. case STRFMT_NUM:
  397. lj_strfmt_putfnum(sb, sf, lj_lib_checknum(L, arg));
  398. break;
  399. case STRFMT_STR: {
  400. MSize len;
  401. const char *s;
  402. cTValue *mo;
  403. if (LJ_UNLIKELY(!tvisstr(o) && !tvisbuf(o)) && retry >= 0 &&
  404. !tvisnil(mo = lj_meta_lookup(L, o, MM_tostring))) {
  405. /* Call __tostring metamethod once. */
  406. copyTV(L, L->top++, mo);
  407. copyTV(L, L->top++, o);
  408. lua_call(L, 1, 1);
  409. o = &L->base[arg-1]; /* Stack may have been reallocated. */
  410. copyTV(L, o, --L->top); /* Replace inline for retry. */
  411. if (retry < 2) { /* Global buffer may have been overwritten. */
  412. retry = 1;
  413. break;
  414. }
  415. }
  416. if (LJ_LIKELY(tvisstr(o))) {
  417. len = strV(o)->len;
  418. s = strVdata(o);
  419. #if LJ_HASBUFFER
  420. } else if (tvisbuf(o)) {
  421. SBufExt *sbx = bufV(o);
  422. if (sbx == (SBufExt *)sb) lj_err_arg(L, arg+1, LJ_ERR_BUFFER_SELF);
  423. len = sbufxlen(sbx);
  424. s = sbx->r;
  425. #endif
  426. } else {
  427. GCstr *str = lj_strfmt_obj(L, o);
  428. len = str->len;
  429. s = strdata(str);
  430. }
  431. if ((sf & STRFMT_T_QUOTED))
  432. strfmt_putquotedlen(sb, s, len); /* No formatting. */
  433. else
  434. strfmt_putfstrlen(sb, sf, s, len);
  435. break;
  436. }
  437. case STRFMT_CHAR:
  438. lj_strfmt_putfchar(sb, sf, lj_lib_checkint(L, arg));
  439. break;
  440. case STRFMT_PTR: /* No formatting. */
  441. lj_strfmt_putptr(sb, lj_obj_ptr(G(L), o));
  442. break;
  443. default:
  444. lj_assertL(0, "bad string format type");
  445. break;
  446. }
  447. }
  448. }
  449. return retry;
  450. }
  451. /* -- Conversions to strings ---------------------------------------------- */
  452. /* Convert integer to string. */
  453. GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k)
  454. {
  455. char buf[STRFMT_MAXBUF_INT];
  456. MSize len = (MSize)(lj_strfmt_wint(buf, k) - buf);
  457. return lj_str_new(L, buf, len);
  458. }
  459. /* Convert integer or number to string. */
  460. GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o)
  461. {
  462. return tvisint(o) ? lj_strfmt_int(L, intV(o)) : lj_strfmt_num(L, o);
  463. }
  464. #if LJ_HASJIT
  465. /* Convert char value to string. */
  466. GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c)
  467. {
  468. char buf[1];
  469. buf[0] = c;
  470. return lj_str_new(L, buf, 1);
  471. }
  472. #endif
  473. /* Raw conversion of object to string. */
  474. GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o)
  475. {
  476. if (tvisstr(o)) {
  477. return strV(o);
  478. } else if (tvisnumber(o)) {
  479. return lj_strfmt_number(L, o);
  480. } else if (tvisnil(o)) {
  481. return lj_str_newlit(L, "nil");
  482. } else if (tvisfalse(o)) {
  483. return lj_str_newlit(L, "false");
  484. } else if (tvistrue(o)) {
  485. return lj_str_newlit(L, "true");
  486. } else {
  487. char buf[8+2+2+16], *p = buf;
  488. p = lj_buf_wmem(p, lj_typename(o), (MSize)strlen(lj_typename(o)));
  489. *p++ = ':'; *p++ = ' ';
  490. if (tvisfunc(o) && isffunc(funcV(o))) {
  491. p = lj_buf_wmem(p, "builtin#", 8);
  492. p = lj_strfmt_wint(p, funcV(o)->c.ffid);
  493. } else {
  494. p = lj_strfmt_wptr(p, lj_obj_ptr(G(L), o));
  495. }
  496. return lj_str_new(L, buf, (size_t)(p - buf));
  497. }
  498. }
  499. /* -- Internal string formatting ------------------------------------------ */
  500. /*
  501. ** These functions are only used for lua_pushfstring(), lua_pushvfstring()
  502. ** and for internal string formatting (e.g. error messages). Caveat: unlike
  503. ** string.format(), only a limited subset of formats and flags are supported!
  504. **
  505. ** LuaJIT has support for a couple more formats than Lua 5.1/5.2:
  506. ** - %d %u %o %x with full formatting, 32 bit integers only.
  507. ** - %f and other FP formats are really %.14g.
  508. ** - %s %c %p without formatting.
  509. */
  510. /* Push formatted message as a string object to Lua stack. va_list variant. */
  511. const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp)
  512. {
  513. SBuf *sb = lj_buf_tmp_(L);
  514. FormatState fs;
  515. SFormat sf;
  516. GCstr *str;
  517. lj_strfmt_init(&fs, fmt, (MSize)strlen(fmt));
  518. while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {
  519. switch (STRFMT_TYPE(sf)) {
  520. case STRFMT_LIT:
  521. lj_buf_putmem(sb, fs.str, fs.len);
  522. break;
  523. case STRFMT_INT:
  524. lj_strfmt_putfxint(sb, sf, va_arg(argp, int32_t));
  525. break;
  526. case STRFMT_UINT:
  527. lj_strfmt_putfxint(sb, sf, va_arg(argp, uint32_t));
  528. break;
  529. case STRFMT_NUM:
  530. lj_strfmt_putfnum(sb, STRFMT_G14, va_arg(argp, lua_Number));
  531. break;
  532. case STRFMT_STR: {
  533. const char *s = va_arg(argp, char *);
  534. if (s == NULL) s = "(null)";
  535. lj_buf_putmem(sb, s, (MSize)strlen(s));
  536. break;
  537. }
  538. case STRFMT_CHAR:
  539. lj_buf_putb(sb, va_arg(argp, int));
  540. break;
  541. case STRFMT_PTR:
  542. lj_strfmt_putptr(sb, va_arg(argp, void *));
  543. break;
  544. case STRFMT_ERR:
  545. default:
  546. lj_buf_putb(sb, '?');
  547. lj_assertL(0, "bad string format near offset %d", fs.len);
  548. break;
  549. }
  550. }
  551. str = lj_buf_str(L, sb);
  552. setstrV(L, L->top, str);
  553. incr_top(L);
  554. return strdata(str);
  555. }
  556. /* Push formatted message as a string object to Lua stack. Vararg variant. */
  557. const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)
  558. {
  559. const char *msg;
  560. va_list argp;
  561. va_start(argp, fmt);
  562. msg = lj_strfmt_pushvf(L, fmt, argp);
  563. va_end(argp);
  564. return msg;
  565. }