3 ** Copyright (C) 2005-2016 Mike Pall. See Copyright Notice in luajit.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,
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
;
33 if (*p
== '%') { /* Escape char? */
34 if (p
[1] == '%') { /* '%%'? */
40 if (p
!= (const uint8_t *)fs
->str
)
42 for (p
++; (uint32_t)*p
- ' ' <= (uint32_t)('0' - ' '); p
++) {
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
;
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. */
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
];
73 return (sf
| sx
| ((c
& 0x20) ? 0 : STRFMT_F_UPPER
));
76 /* Return error location. */
78 fs
->len
= (MSize
)(p
- (const uint8_t *)fs
->str
);
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
++ = '-'; }
101 if (u
< 10) goto dig1
; if (u
< 100) goto dig2
; if (u
< 1000) goto dig3
;
103 uint32_t v
= u
/ 10000; u
-= v
* 10000;
105 if (v
< 10) goto dig5
; if (v
< 100) goto dig6
; if (v
< 1000) goto dig7
;
107 uint32_t w
= v
/ 10000; v
-= w
* 10000;
108 if (w
>= 10) WINT_R(w
, 10, 10)
109 *p
++ = (char)('0'+w
);
112 dig7
: WINT_R(v
, 12, 100)
113 dig6
: WINT_R(v
, 10, 10)
114 dig5
: *p
++ = (char)('0'+v
);
117 dig3
: WINT_R(u
, 12, 100)
118 dig2
: WINT_R(u
, 10, 10)
119 dig1
: *p
++ = (char)('0'+u
);
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
;
130 *p
++ = 'N'; *p
++ = 'U'; *p
++ = 'L'; *p
++ = 'L';
134 /* Shorten output for 64 bit pointers. */
135 n
= 2+2*4+((x
>> 32) ? 2+2*(lj_fls((uint32_t)(x
>> 32))>>3) : 0);
139 for (i
= n
-1; i
>= 2; i
--, x
>>= 4)
140 p
[i
] = "0123456789abcdef"[(x
& 15)];
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);
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
)
158 *lenp
= strV(o
)->len
;
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
);
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
));
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
);
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
));
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
, '"');
201 uint32_t c
= (uint32_t)(uint8_t)*s
++;
202 char *p
= lj_buf_more(sb
, 4);
203 if (c
== '"' || c
== '\\' || c
== '\n') {
205 } else if (lj_char_iscntrl(c
)) { /* This can only be 0-31 or 127. */
208 if (c
>= 100 || lj_char_isdigit((uint8_t)*s
)) {
209 *p
++ = (char)('0'+(c
>= 100)); if (c
>= 100) c
-= 100;
211 } else if (c
>= 10) {
213 d
= (c
* 205) >> 11; c
-= d
* 10; *p
++ = (char)('0'+d
);
220 lj_buf_putb(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
;
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
);
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
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
;
265 } else if ((sf
& STRFMT_F_PLUS
)) {
267 } else if ((sf
& STRFMT_F_SPACE
)) {
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. */
277 (sf
& (STRFMT_T_OCT
|STRFMT_F_ALT
)) == (STRFMT_T_OCT
|STRFMT_F_ALT
))
279 } else if (!(sf
& (STRFMT_T_HEX
|STRFMT_T_OCT
))) { /* Decimal. */
281 while ((k
>> 32)) { *--q
= (char)('0' + k
% 10); k
/= 10; }
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" :
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
305 /* Format number with leading/trailing whitespace and zeros. */
306 if ((sf
& (STRFMT_F_LEFT
|STRFMT_F_ZERO
)) == 0)
307 while (width
-- > pprec
) *p
++ = ' ';
309 if ((char)prefix
>= 'X') *p
++ = '0';
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
));
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. */
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
)
338 if (n
>= 9223372036854775808.0)
339 k
= (int64_t)(n
- 18446744073709551616.0);
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
);
362 /* Convert char value to string. */
363 GCstr
* LJ_FASTCALL
lj_strfmt_char(lua_State
*L
, int c
)
367 return lj_str_new(L
, buf
, 1);
371 /* Raw conversion of object to string. */
372 GCstr
* LJ_FASTCALL
lj_strfmt_obj(lua_State
*L
, cTValue
*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");
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
);
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
);
418 lj_strfmt_init(&fs
, fmt
, (MSize
)strlen(fmt
));
419 while ((sf
= lj_strfmt_parse(&fs
)) != STRFMT_EOF
) {
420 switch (STRFMT_TYPE(sf
)) {
422 lj_buf_putmem(sb
, fs
.str
, fs
.len
);
425 lj_strfmt_putfxint(sb
, sf
, va_arg(argp
, int32_t));
428 lj_strfmt_putfxint(sb
, sf
, va_arg(argp
, uint32_t));
431 lj_strfmt_putfnum(sb
, STRFMT_G14
, va_arg(argp
, lua_Number
));
434 const char *s
= va_arg(argp
, char *);
435 if (s
== NULL
) s
= "(null)";
436 lj_buf_putmem(sb
, s
, (MSize
)strlen(s
));
440 lj_buf_putb(sb
, va_arg(argp
, int));
443 lj_strfmt_putptr(sb
, va_arg(argp
, void *));
447 lj_buf_putb(sb
, '?');
452 str
= lj_buf_str(L
, sb
);
453 setstrV(L
, L
->top
, 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
, ...)
464 msg
= lj_strfmt_pushvf(L
, fmt
, argp
);