fix getsup (HH)
[luatex.git] / source / libs / luajit / LuaJIT-src / src / lj_strfmt.c
blob7c7d81d360f9ef8e25971344935376e135ff8d3d
1 /*
2 ** String formatting.
3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #include <stdio.h>
8 #define lj_strfmt_c
9 #define LUA_CORE
11 #include "lj_obj.h"
12 #include "lj_buf.h"
13 #include "lj_str.h"
14 #include "lj_state.h"
15 #include "lj_char.h"
16 #include "lj_strfmt.h"
18 /* -- Format parser ------------------------------------------------------- */
20 static const uint8_t strfmt_map[('x'-'A')+1] = {
21 STRFMT_A,0,0,0,STRFMT_E,STRFMT_F,STRFMT_G,0,0,0,0,0,0,
22 0,0,0,0,0,0,0,0,0,0,STRFMT_X,0,0,
23 0,0,0,0,0,0,
24 STRFMT_A,0,STRFMT_C,STRFMT_D,STRFMT_E,STRFMT_F,STRFMT_G,0,STRFMT_I,0,0,0,0,
25 0,STRFMT_O,STRFMT_P,STRFMT_Q,0,STRFMT_S,0,STRFMT_U,0,0,STRFMT_X
28 SFormat LJ_FASTCALL lj_strfmt_parse(FormatState *fs)
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;
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);
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;
65 sf |= ((prec+1) << STRFMT_SH_PREC);
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));
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;
84 fs->p = p;
85 retlit:
86 fs->len = (MSize)(p - (const uint8_t *)fs->str);
87 return fs->len ? STRFMT_LIT : STRFMT_EOF;
90 /* -- Raw conversions ----------------------------------------------------- */
92 #define WINT_R(x, sh, sc) \
93 { uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); }
95 /* Write integer to buffer. */
96 char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k)
98 uint32_t u = (uint32_t)k;
99 if (k < 0) { u = (uint32_t)-k; *p++ = '-'; }
100 if (u < 10000) {
101 if (u < 10) goto dig1; if (u < 100) goto dig2; if (u < 1000) goto dig3;
102 } else {
103 uint32_t v = u / 10000; u -= v * 10000;
104 if (v < 10000) {
105 if (v < 10) goto dig5; if (v < 100) goto dig6; if (v < 1000) goto dig7;
106 } else {
107 uint32_t w = v / 10000; v -= w * 10000;
108 if (w >= 10) WINT_R(w, 10, 10)
109 *p++ = (char)('0'+w);
111 WINT_R(v, 23, 1000)
112 dig7: WINT_R(v, 12, 100)
113 dig6: WINT_R(v, 10, 10)
114 dig5: *p++ = (char)('0'+v);
116 WINT_R(u, 23, 1000)
117 dig3: WINT_R(u, 12, 100)
118 dig2: WINT_R(u, 10, 10)
119 dig1: *p++ = (char)('0'+u);
120 return p;
122 #undef WINT_R
124 /* Write pointer to buffer. */
125 char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v)
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;
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;
144 /* Write ULEB128 to buffer. */
145 char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v)
147 for (; v >= 0x80; v >>= 7)
148 *p++ = (char)((v & 0x7f) | 0x80);
149 *p++ = (char)v;
150 return p;
153 /* Return string or write number to tmp buffer and return pointer to start. */
154 const char *lj_strfmt_wstrnum(lua_State *L, cTValue *o, MSize *lenp)
156 SBuf *sb;
157 if (tvisstr(o)) {
158 *lenp = strV(o)->len;
159 return strVdata(o);
160 } else if (tvisint(o)) {
161 sb = lj_strfmt_putint(lj_buf_tmp_(L), intV(o));
162 } else if (tvisnum(o)) {
163 sb = lj_strfmt_putfnum(lj_buf_tmp_(L), STRFMT_G14, o->n);
164 } else {
165 return NULL;
167 *lenp = sbuflen(sb);
168 return sbufB(sb);
171 /* -- Unformatted conversions to buffer ----------------------------------- */
173 /* Add integer to buffer. */
174 SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k)
176 setsbufP(sb, lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k));
177 return sb;
180 #if LJ_HASJIT
181 /* Add number to buffer. */
182 SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o)
184 return lj_strfmt_putfnum(sb, STRFMT_G14, o->n);
186 #endif
188 SBuf * LJ_FASTCALL lj_strfmt_putptr(SBuf *sb, const void *v)
190 setsbufP(sb, lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), v));
191 return sb;
194 /* Add quoted string to buffer. */
195 SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str)
197 const char *s = strdata(str);
198 MSize len = str->len;
199 lj_buf_putb(sb, '"');
200 while (len--) {
201 uint32_t c = (uint32_t)(uint8_t)*s++;
202 char *p = lj_buf_more(sb, 4);
203 if (c == '"' || c == '\\' || c == '\n') {
204 *p++ = '\\';
205 } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */
206 uint32_t d;
207 *p++ = '\\';
208 if (c >= 100 || lj_char_isdigit((uint8_t)*s)) {
209 *p++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100;
210 goto tens;
211 } else if (c >= 10) {
212 tens:
213 d = (c * 205) >> 11; c -= d * 10; *p++ = (char)('0'+d);
215 c += '0';
217 *p++ = (char)c;
218 setsbufP(sb, p);
220 lj_buf_putb(sb, '"');
221 return sb;
224 /* -- Formatted conversions to buffer ------------------------------------- */
226 /* Add formatted char to buffer. */
227 SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat sf, int32_t c)
229 MSize width = STRFMT_WIDTH(sf);
230 char *p = lj_buf_more(sb, width > 1 ? width : 1);
231 if ((sf & STRFMT_F_LEFT)) *p++ = (char)c;
232 while (width-- > 1) *p++ = ' ';
233 if (!(sf & STRFMT_F_LEFT)) *p++ = (char)c;
234 setsbufP(sb, p);
235 return sb;
238 /* Add formatted string to buffer. */
239 SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str)
241 MSize len = str->len <= STRFMT_PREC(sf) ? str->len : STRFMT_PREC(sf);
242 MSize width = STRFMT_WIDTH(sf);
243 char *p = lj_buf_more(sb, width > len ? width : len);
244 if ((sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len);
245 while (width-- > len) *p++ = ' ';
246 if (!(sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len);
247 setsbufP(sb, p);
248 return sb;
251 /* Add formatted signed/unsigned integer to buffer. */
252 SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k)
254 char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *p;
255 #ifdef LUA_USE_ASSERT
256 char *ps;
257 #endif
258 MSize prefix = 0, len, prec, pprec, width, need;
260 /* Figure out signed prefixes. */
261 if (STRFMT_TYPE(sf) == STRFMT_INT) {
262 if ((int64_t)k < 0) {
263 k = (uint64_t)-(int64_t)k;
264 prefix = 256 + '-';
265 } else if ((sf & STRFMT_F_PLUS)) {
266 prefix = 256 + '+';
267 } else if ((sf & STRFMT_F_SPACE)) {
268 prefix = 256 + ' ';
272 /* Convert number and store to fixed-size buffer in reverse order. */
273 prec = STRFMT_PREC(sf);
274 if ((int32_t)prec >= 0) sf &= ~STRFMT_F_ZERO;
275 if (k == 0) { /* Special-case zero argument. */
276 if (prec != 0 ||
277 (sf & (STRFMT_T_OCT|STRFMT_F_ALT)) == (STRFMT_T_OCT|STRFMT_F_ALT))
278 *--q = '0';
279 } else if (!(sf & (STRFMT_T_HEX|STRFMT_T_OCT))) { /* Decimal. */
280 uint32_t k2;
281 while ((k >> 32)) { *--q = (char)('0' + k % 10); k /= 10; }
282 k2 = (uint32_t)k;
283 do { *--q = (char)('0' + k2 % 10); k2 /= 10; } while (k2);
284 } else if ((sf & STRFMT_T_HEX)) { /* Hex. */
285 const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEF" :
286 "0123456789abcdef";
287 do { *--q = hexdig[(k & 15)]; k >>= 4; } while (k);
288 if ((sf & STRFMT_F_ALT)) prefix = 512 + ((sf & STRFMT_F_UPPER) ? 'X' : 'x');
289 } else { /* Octal. */
290 do { *--q = (char)('0' + (uint32_t)(k & 7)); k >>= 3; } while (k);
291 if ((sf & STRFMT_F_ALT)) *--q = '0';
294 /* Calculate sizes. */
295 len = (MSize)(buf + sizeof(buf) - q);
296 if ((int32_t)len >= (int32_t)prec) prec = len;
297 width = STRFMT_WIDTH(sf);
298 pprec = prec + (prefix >> 8);
299 need = width > pprec ? width : pprec;
300 p = lj_buf_more(sb, need);
301 #ifdef LUA_USE_ASSERT
302 ps = p;
303 #endif
305 /* Format number with leading/trailing whitespace and zeros. */
306 if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == 0)
307 while (width-- > pprec) *p++ = ' ';
308 if (prefix) {
309 if ((char)prefix >= 'X') *p++ = '0';
310 *p++ = (char)prefix;
312 if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == STRFMT_F_ZERO)
313 while (width-- > pprec) *p++ = '0';
314 while (prec-- > len) *p++ = '0';
315 while (q < buf + sizeof(buf)) *p++ = *q++; /* Add number itself. */
316 if ((sf & STRFMT_F_LEFT))
317 while (width-- > pprec) *p++ = ' ';
319 lua_assert(need == (MSize)(p - ps));
320 setsbufP(sb, p);
321 return sb;
324 /* Add number formatted as signed integer to buffer. */
325 SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n)
327 int64_t k = (int64_t)n;
328 if (checki32(k) && sf == STRFMT_INT)
329 return lj_strfmt_putint(sb, (int32_t)k); /* Shortcut for plain %d. */
330 else
331 return lj_strfmt_putfxint(sb, sf, (uint64_t)k);
334 /* Add number formatted as unsigned integer to buffer. */
335 SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n)
337 int64_t k;
338 if (n >= 9223372036854775808.0)
339 k = (int64_t)(n - 18446744073709551616.0);
340 else
341 k = (int64_t)n;
342 return lj_strfmt_putfxint(sb, sf, (uint64_t)k);
345 /* -- Conversions to strings ---------------------------------------------- */
347 /* Convert integer to string. */
348 GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k)
350 char buf[STRFMT_MAXBUF_INT];
351 MSize len = (MSize)(lj_strfmt_wint(buf, k) - buf);
352 return lj_str_new(L, buf, len);
355 /* Convert integer or number to string. */
356 GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o)
358 return tvisint(o) ? lj_strfmt_int(L, intV(o)) : lj_strfmt_num(L, o);
361 #if LJ_HASJIT
362 /* Convert char value to string. */
363 GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c)
365 char buf[1];
366 buf[0] = c;
367 return lj_str_new(L, buf, 1);
369 #endif
371 /* Raw conversion of object to string. */
372 GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o)
374 if (tvisstr(o)) {
375 return strV(o);
376 } else if (tvisnumber(o)) {
377 return lj_strfmt_number(L, o);
378 } else if (tvisnil(o)) {
379 return lj_str_newlit(L, "nil");
380 } else if (tvisfalse(o)) {
381 return lj_str_newlit(L, "false");
382 } else if (tvistrue(o)) {
383 return lj_str_newlit(L, "true");
384 } else {
385 char buf[8+2+2+16], *p = buf;
386 p = lj_buf_wmem(p, lj_typename(o), (MSize)strlen(lj_typename(o)));
387 *p++ = ':'; *p++ = ' ';
388 if (tvisfunc(o) && isffunc(funcV(o))) {
389 p = lj_buf_wmem(p, "builtin#", 8);
390 p = lj_strfmt_wint(p, funcV(o)->c.ffid);
391 } else {
392 p = lj_strfmt_wptr(p, lj_obj_ptr(o));
394 return lj_str_new(L, buf, (size_t)(p - buf));
398 /* -- Internal string formatting ------------------------------------------ */
401 ** These functions are only used for lua_pushfstring(), lua_pushvfstring()
402 ** and for internal string formatting (e.g. error messages). Caveat: unlike
403 ** string.format(), only a limited subset of formats and flags are supported!
405 ** LuaJIT has support for a couple more formats than Lua 5.1/5.2:
406 ** - %d %u %o %x with full formatting, 32 bit integers only.
407 ** - %f and other FP formats are really %.14g.
408 ** - %s %c %p without formatting.
411 /* Push formatted message as a string object to Lua stack. va_list variant. */
412 const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp)
414 SBuf *sb = lj_buf_tmp_(L);
415 FormatState fs;
416 SFormat sf;
417 GCstr *str;
418 lj_strfmt_init(&fs, fmt, (MSize)strlen(fmt));
419 while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) {
420 switch (STRFMT_TYPE(sf)) {
421 case STRFMT_LIT:
422 lj_buf_putmem(sb, fs.str, fs.len);
423 break;
424 case STRFMT_INT:
425 lj_strfmt_putfxint(sb, sf, va_arg(argp, int32_t));
426 break;
427 case STRFMT_UINT:
428 lj_strfmt_putfxint(sb, sf, va_arg(argp, uint32_t));
429 break;
430 case STRFMT_NUM:
431 lj_strfmt_putfnum(sb, STRFMT_G14, va_arg(argp, lua_Number));
432 break;
433 case STRFMT_STR: {
434 const char *s = va_arg(argp, char *);
435 if (s == NULL) s = "(null)";
436 lj_buf_putmem(sb, s, (MSize)strlen(s));
437 break;
439 case STRFMT_CHAR:
440 lj_buf_putb(sb, va_arg(argp, int));
441 break;
442 case STRFMT_PTR:
443 lj_strfmt_putptr(sb, va_arg(argp, void *));
444 break;
445 case STRFMT_ERR:
446 default:
447 lj_buf_putb(sb, '?');
448 lua_assert(0);
449 break;
452 str = lj_buf_str(L, sb);
453 setstrV(L, L->top, str);
454 incr_top(L);
455 return strdata(str);
458 /* Push formatted message as a string object to Lua stack. Vararg variant. */
459 const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...)
461 const char *msg;
462 va_list argp;
463 va_start(argp, fmt);
464 msg = lj_strfmt_pushvf(L, fmt, argp);
465 va_end(argp);
466 return msg;