diff options
Diffstat (limited to 'src/lj_strfmt.c')
-rw-r--r-- | src/lj_strfmt.c | 549 |
1 files changed, 549 insertions, 0 deletions
diff --git a/src/lj_strfmt.c b/src/lj_strfmt.c new file mode 100644 index 00000000..0003de70 --- /dev/null +++ b/src/lj_strfmt.c | |||
@@ -0,0 +1,549 @@ | |||
1 | /* | ||
2 | ** String formatting. | ||
3 | ** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h | ||
4 | */ | ||
5 | |||
6 | #include <stdio.h> | ||
7 | |||
8 | #define lj_strfmt_c | ||
9 | #define LUA_CORE | ||
10 | |||
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" | ||
17 | |||
18 | /* -- Format parser ------------------------------------------------------- */ | ||
19 | |||
20 | static const uint8_t strfmt_map[('x'-'A')+1] = { | ||
21 | STRFMT_A,0,0,0,STRFMT_E,0,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 | ||
26 | }; | ||
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 | |||
90 | /* -- Raw conversions ----------------------------------------------------- */ | ||
91 | |||
92 | /* Write number to bufer. */ | ||
93 | char * LJ_FASTCALL lj_strfmt_wnum(char *p, cTValue *o) | ||
94 | { | ||
95 | if (LJ_LIKELY((o->u32.hi << 1) < 0xffe00000)) { /* Finite? */ | ||
96 | #if __BIONIC__ | ||
97 | if (tvismzero(o)) { *p++ = '-'; *p++ = '0'; return p; } | ||
98 | #endif | ||
99 | return p + lua_number2str(p, o->n); | ||
100 | } else if (((o->u32.hi & 0x000fffff) | o->u32.lo) != 0) { | ||
101 | *p++ = 'n'; *p++ = 'a'; *p++ = 'n'; | ||
102 | } else if ((o->u32.hi & 0x80000000) == 0) { | ||
103 | *p++ = 'i'; *p++ = 'n'; *p++ = 'f'; | ||
104 | } else { | ||
105 | *p++ = '-'; *p++ = 'i'; *p++ = 'n'; *p++ = 'f'; | ||
106 | } | ||
107 | return p; | ||
108 | } | ||
109 | |||
110 | #define WINT_R(x, sh, sc) \ | ||
111 | { uint32_t d = (x*(((1<<sh)+sc-1)/sc))>>sh; x -= d*sc; *p++ = (char)('0'+d); } | ||
112 | |||
113 | /* Write integer to buffer. */ | ||
114 | char * LJ_FASTCALL lj_strfmt_wint(char *p, int32_t k) | ||
115 | { | ||
116 | uint32_t u = (uint32_t)k; | ||
117 | if (k < 0) { u = (uint32_t)-k; *p++ = '-'; } | ||
118 | if (u < 10000) { | ||
119 | if (u < 10) goto dig1; if (u < 100) goto dig2; if (u < 1000) goto dig3; | ||
120 | } else { | ||
121 | uint32_t v = u / 10000; u -= v * 10000; | ||
122 | if (v < 10000) { | ||
123 | if (v < 10) goto dig5; if (v < 100) goto dig6; if (v < 1000) goto dig7; | ||
124 | } else { | ||
125 | uint32_t w = v / 10000; v -= w * 10000; | ||
126 | if (w >= 10) WINT_R(w, 10, 10) | ||
127 | *p++ = (char)('0'+w); | ||
128 | } | ||
129 | WINT_R(v, 23, 1000) | ||
130 | dig7: WINT_R(v, 12, 100) | ||
131 | dig6: WINT_R(v, 10, 10) | ||
132 | dig5: *p++ = (char)('0'+v); | ||
133 | } | ||
134 | WINT_R(u, 23, 1000) | ||
135 | dig3: WINT_R(u, 12, 100) | ||
136 | dig2: WINT_R(u, 10, 10) | ||
137 | dig1: *p++ = (char)('0'+u); | ||
138 | return p; | ||
139 | } | ||
140 | #undef WINT_R | ||
141 | |||
142 | /* Write pointer to buffer. */ | ||
143 | char * LJ_FASTCALL lj_strfmt_wptr(char *p, const void *v) | ||
144 | { | ||
145 | ptrdiff_t x = (ptrdiff_t)v; | ||
146 | MSize i, n = STRFMT_MAXBUF_PTR; | ||
147 | if (x == 0) { | ||
148 | *p++ = 'N'; *p++ = 'U'; *p++ = 'L'; *p++ = 'L'; | ||
149 | return p; | ||
150 | } | ||
151 | #if LJ_64 | ||
152 | /* Shorten output for 64 bit pointers. */ | ||
153 | n = 2+2*4+((x >> 32) ? 2+2*(lj_fls((uint32_t)(x >> 32))>>3) : 0); | ||
154 | #endif | ||
155 | p[0] = '0'; | ||
156 | p[1] = 'x'; | ||
157 | for (i = n-1; i >= 2; i--, x >>= 4) | ||
158 | p[i] = "0123456789abcdef"[(x & 15)]; | ||
159 | return p+n; | ||
160 | } | ||
161 | |||
162 | /* Write ULEB128 to buffer. */ | ||
163 | char * LJ_FASTCALL lj_strfmt_wuleb128(char *p, uint32_t v) | ||
164 | { | ||
165 | for (; v >= 0x80; v >>= 7) | ||
166 | *p++ = (char)((v & 0x7f) | 0x80); | ||
167 | *p++ = (char)v; | ||
168 | return p; | ||
169 | } | ||
170 | |||
171 | /* Return string or write number to buffer and return pointer to start. */ | ||
172 | const char *lj_strfmt_wstrnum(char *buf, cTValue *o, MSize *lenp) | ||
173 | { | ||
174 | if (tvisstr(o)) { | ||
175 | *lenp = strV(o)->len; | ||
176 | return strVdata(o); | ||
177 | } else if (tvisint(o)) { | ||
178 | *lenp = (MSize)(lj_strfmt_wint(buf, intV(o)) - buf); | ||
179 | return buf; | ||
180 | } else if (tvisnum(o)) { | ||
181 | *lenp = (MSize)(lj_strfmt_wnum(buf, o) - buf); | ||
182 | return buf; | ||
183 | } else { | ||
184 | return NULL; | ||
185 | } | ||
186 | } | ||
187 | |||
188 | /* -- Unformatted conversions to buffer ----------------------------------- */ | ||
189 | |||
190 | /* Add integer to buffer. */ | ||
191 | SBuf * LJ_FASTCALL lj_strfmt_putint(SBuf *sb, int32_t k) | ||
192 | { | ||
193 | setsbufP(sb, lj_strfmt_wint(lj_buf_more(sb, STRFMT_MAXBUF_INT), k)); | ||
194 | return sb; | ||
195 | } | ||
196 | |||
197 | #if LJ_HASJIT | ||
198 | /* Add number to buffer. */ | ||
199 | SBuf * LJ_FASTCALL lj_strfmt_putnum(SBuf *sb, cTValue *o) | ||
200 | { | ||
201 | setsbufP(sb, lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM), o)); | ||
202 | return sb; | ||
203 | } | ||
204 | #endif | ||
205 | |||
206 | /* Add quoted string to buffer. */ | ||
207 | SBuf * LJ_FASTCALL lj_strfmt_putquoted(SBuf *sb, GCstr *str) | ||
208 | { | ||
209 | const char *s = strdata(str); | ||
210 | MSize len = str->len; | ||
211 | lj_buf_putb(sb, '"'); | ||
212 | while (len--) { | ||
213 | uint32_t c = (uint32_t)(uint8_t)*s++; | ||
214 | char *p = lj_buf_more(sb, 4); | ||
215 | if (c == '"' || c == '\\' || c == '\n') { | ||
216 | *p++ = '\\'; | ||
217 | } else if (lj_char_iscntrl(c)) { /* This can only be 0-31 or 127. */ | ||
218 | uint32_t d; | ||
219 | *p++ = '\\'; | ||
220 | if (c >= 100 || lj_char_isdigit((uint8_t)*s)) { | ||
221 | *p++ = (char)('0'+(c >= 100)); if (c >= 100) c -= 100; | ||
222 | goto tens; | ||
223 | } else if (c >= 10) { | ||
224 | tens: | ||
225 | d = (c * 205) >> 11; c -= d * 10; *p++ = (char)('0'+d); | ||
226 | } | ||
227 | c += '0'; | ||
228 | } | ||
229 | *p++ = (char)c; | ||
230 | setsbufP(sb, p); | ||
231 | } | ||
232 | lj_buf_putb(sb, '"'); | ||
233 | return sb; | ||
234 | } | ||
235 | |||
236 | /* -- Formatted conversions to buffer ------------------------------------- */ | ||
237 | |||
238 | /* Add formatted char to buffer. */ | ||
239 | SBuf *lj_strfmt_putfchar(SBuf *sb, SFormat sf, int32_t c) | ||
240 | { | ||
241 | MSize width = STRFMT_WIDTH(sf); | ||
242 | char *p = lj_buf_more(sb, width > 1 ? width : 1); | ||
243 | if ((sf & STRFMT_F_LEFT)) *p++ = (char)c; | ||
244 | while (width-- > 1) *p++ = ' '; | ||
245 | if (!(sf & STRFMT_F_LEFT)) *p++ = (char)c; | ||
246 | setsbufP(sb, p); | ||
247 | return sb; | ||
248 | } | ||
249 | |||
250 | /* Add formatted string to buffer. */ | ||
251 | SBuf *lj_strfmt_putfstr(SBuf *sb, SFormat sf, GCstr *str) | ||
252 | { | ||
253 | MSize len = str->len <= STRFMT_PREC(sf) ? str->len : STRFMT_PREC(sf); | ||
254 | MSize width = STRFMT_WIDTH(sf); | ||
255 | char *p = lj_buf_more(sb, width > len ? width : len); | ||
256 | if ((sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len); | ||
257 | while (width-- > len) *p++ = ' '; | ||
258 | if (!(sf & STRFMT_F_LEFT)) p = lj_buf_wmem(p, strdata(str), len); | ||
259 | setsbufP(sb, p); | ||
260 | return sb; | ||
261 | } | ||
262 | |||
263 | /* Add formatted signed/unsigned integer to buffer. */ | ||
264 | SBuf *lj_strfmt_putfxint(SBuf *sb, SFormat sf, uint64_t k) | ||
265 | { | ||
266 | char buf[STRFMT_MAXBUF_XINT], *q = buf + sizeof(buf), *p; | ||
267 | #ifdef LUA_USE_ASSERT | ||
268 | char *ps; | ||
269 | #endif | ||
270 | MSize prefix = 0, len, prec, pprec, width, need; | ||
271 | |||
272 | /* Figure out signed prefixes. */ | ||
273 | if (STRFMT_TYPE(sf) == STRFMT_INT) { | ||
274 | if ((int64_t)k < 0) { | ||
275 | k = (uint64_t)-(int64_t)k; | ||
276 | prefix = 256 + '-'; | ||
277 | } else if ((sf & STRFMT_F_PLUS)) { | ||
278 | prefix = 256 + '+'; | ||
279 | } else if ((sf & STRFMT_F_SPACE)) { | ||
280 | prefix = 256 + ' '; | ||
281 | } | ||
282 | } | ||
283 | |||
284 | /* Convert number and store to fixed-size buffer in reverse order. */ | ||
285 | prec = STRFMT_PREC(sf); | ||
286 | if ((int32_t)prec >= 0) sf &= ~STRFMT_F_ZERO; | ||
287 | if (k == 0) { /* Special-case zero argument. */ | ||
288 | if (prec != 0 || | ||
289 | (sf & (STRFMT_T_OCT|STRFMT_F_ALT)) == (STRFMT_T_OCT|STRFMT_F_ALT)) | ||
290 | *--q = '0'; | ||
291 | } else if (!(sf & (STRFMT_T_HEX|STRFMT_T_OCT))) { /* Decimal. */ | ||
292 | uint32_t k2; | ||
293 | while ((k >> 32)) { *--q = (char)('0' + k % 10); k /= 10; } | ||
294 | k2 = (uint32_t)k; | ||
295 | do { *--q = (char)('0' + k2 % 10); k2 /= 10; } while (k2); | ||
296 | } else if ((sf & STRFMT_T_HEX)) { /* Hex. */ | ||
297 | const char *hexdig = (sf & STRFMT_F_UPPER) ? "0123456789ABCDEF" : | ||
298 | "0123456789abcdef"; | ||
299 | do { *--q = hexdig[(k & 15)]; k >>= 4; } while (k); | ||
300 | if ((sf & STRFMT_F_ALT)) prefix = 512 + 'x'; | ||
301 | } else { /* Octal. */ | ||
302 | do { *--q = (char)('0' + (uint32_t)(k & 7)); k >>= 3; } while (k); | ||
303 | if ((sf & STRFMT_F_ALT)) *--q = '0'; | ||
304 | } | ||
305 | |||
306 | /* Calculate sizes. */ | ||
307 | len = (MSize)(buf + sizeof(buf) - q); | ||
308 | if ((int32_t)len >= (int32_t)prec) prec = len; | ||
309 | width = STRFMT_WIDTH(sf); | ||
310 | pprec = prec + (prefix >> 8); | ||
311 | need = width > pprec ? width : pprec; | ||
312 | p = lj_buf_more(sb, need); | ||
313 | #ifdef LUA_USE_ASSERT | ||
314 | ps = p; | ||
315 | #endif | ||
316 | |||
317 | /* Format number with leading/trailing whitespace and zeros. */ | ||
318 | if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == 0) | ||
319 | while (width-- > pprec) *p++ = ' '; | ||
320 | if (prefix) { | ||
321 | if ((char)prefix == 'x') *p++ = '0'; | ||
322 | *p++ = (char)prefix; | ||
323 | } | ||
324 | if ((sf & (STRFMT_F_LEFT|STRFMT_F_ZERO)) == STRFMT_F_ZERO) | ||
325 | while (width-- > pprec) *p++ = '0'; | ||
326 | while (prec-- > len) *p++ = '0'; | ||
327 | while (q < buf + sizeof(buf)) *p++ = *q++; /* Add number itself. */ | ||
328 | if ((sf & STRFMT_F_LEFT)) | ||
329 | while (width-- > pprec) *p++ = ' '; | ||
330 | |||
331 | lua_assert(need == (MSize)(p - ps)); | ||
332 | setsbufP(sb, p); | ||
333 | return sb; | ||
334 | } | ||
335 | |||
336 | /* Add number formatted as signed integer to buffer. */ | ||
337 | SBuf *lj_strfmt_putfnum_int(SBuf *sb, SFormat sf, lua_Number n) | ||
338 | { | ||
339 | int64_t k = (int64_t)n; | ||
340 | if (checki32(k) && sf == STRFMT_INT) | ||
341 | return lj_strfmt_putint(sb, (int32_t)k); /* Shortcut for plain %d. */ | ||
342 | else | ||
343 | return lj_strfmt_putfxint(sb, sf, (uint64_t)k); | ||
344 | } | ||
345 | |||
346 | /* Add number formatted as unsigned integer to buffer. */ | ||
347 | SBuf *lj_strfmt_putfnum_uint(SBuf *sb, SFormat sf, lua_Number n) | ||
348 | { | ||
349 | int64_t k; | ||
350 | if (n >= 9223372036854775808.0) | ||
351 | k = (int64_t)(n - 18446744073709551616.0); | ||
352 | else | ||
353 | k = (int64_t)n; | ||
354 | return lj_strfmt_putfxint(sb, sf, (uint64_t)k); | ||
355 | } | ||
356 | |||
357 | /* Max. sprintf buffer size needed. At least #string.format("%.99f", -1e308). */ | ||
358 | #define STRFMT_FMTNUMBUF 512 | ||
359 | |||
360 | /* Add formatted floating-point number to buffer. */ | ||
361 | SBuf *lj_strfmt_putfnum(SBuf *sb, SFormat sf, lua_Number n) | ||
362 | { | ||
363 | TValue tv; | ||
364 | tv.n = n; | ||
365 | if (LJ_UNLIKELY((tv.u32.hi << 1) >= 0xffe00000)) { | ||
366 | /* Canonicalize output of non-finite values. */ | ||
367 | MSize width = STRFMT_WIDTH(sf), len = 3; | ||
368 | int prefix = 0, ch = (sf & STRFMT_F_UPPER) ? 0x202020 : 0; | ||
369 | char *p; | ||
370 | if (((tv.u32.hi & 0x000fffff) | tv.u32.lo) != 0) { | ||
371 | ch ^= ('n' << 16) | ('a' << 8) | 'n'; | ||
372 | if ((sf & STRFMT_F_SPACE)) prefix = ' '; | ||
373 | } else { | ||
374 | ch ^= ('i' << 16) | ('n' << 8) | 'f'; | ||
375 | if ((tv.u32.hi & 0x80000000)) prefix = '-'; | ||
376 | else if ((sf & STRFMT_F_PLUS)) prefix = '+'; | ||
377 | else if ((sf & STRFMT_F_SPACE)) prefix = ' '; | ||
378 | } | ||
379 | if (prefix) len = 4; | ||
380 | p = lj_buf_more(sb, width > len ? width : len); | ||
381 | if (!(sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' '; | ||
382 | if (prefix) *p++ = prefix; | ||
383 | *p++ = (char)(ch >> 16); *p++ = (char)(ch >> 8); *p++ = (char)ch; | ||
384 | if ((sf & STRFMT_F_LEFT)) while (width-- > len) *p++ = ' '; | ||
385 | setsbufP(sb, p); | ||
386 | } else { /* Delegate to sprintf() for now. */ | ||
387 | uint8_t width = (uint8_t)STRFMT_WIDTH(sf), prec = (uint8_t)STRFMT_PREC(sf); | ||
388 | char fmt[1+5+2+3+1+1], *p = fmt; | ||
389 | *p++ = '%'; | ||
390 | if ((sf & STRFMT_F_LEFT)) *p++ = '-'; | ||
391 | if ((sf & STRFMT_F_PLUS)) *p++ = '+'; | ||
392 | if ((sf & STRFMT_F_ZERO)) *p++ = '0'; | ||
393 | if ((sf & STRFMT_F_SPACE)) *p++ = ' '; | ||
394 | if ((sf & STRFMT_F_ALT)) *p++ = '#'; | ||
395 | if (width) { | ||
396 | uint8_t x = width / 10, y = width % 10; | ||
397 | if (x) *p++ = '0' + x; | ||
398 | *p++ = '0' + y; | ||
399 | } | ||
400 | if (prec != 255) { | ||
401 | uint8_t x = prec / 10, y = prec % 10; | ||
402 | *p++ = '.'; | ||
403 | if (x) *p++ = '0' + x; | ||
404 | *p++ = '0' + y; | ||
405 | } | ||
406 | *p++ = (0x67666561 >> (STRFMT_FP(sf)<<3)) ^ ((sf & STRFMT_F_UPPER)?0x20:0); | ||
407 | *p = '\0'; | ||
408 | p = lj_buf_more(sb, STRFMT_FMTNUMBUF); | ||
409 | setsbufP(sb, p + sprintf(p, fmt, n)); | ||
410 | } | ||
411 | return sb; | ||
412 | } | ||
413 | |||
414 | /* -- Conversions to strings ---------------------------------------------- */ | ||
415 | |||
416 | /* Convert integer to string. */ | ||
417 | GCstr * LJ_FASTCALL lj_strfmt_int(lua_State *L, int32_t k) | ||
418 | { | ||
419 | char buf[STRFMT_MAXBUF_INT]; | ||
420 | MSize len = (MSize)(lj_strfmt_wint(buf, k) - buf); | ||
421 | return lj_str_new(L, buf, len); | ||
422 | } | ||
423 | |||
424 | /* Convert number to string. */ | ||
425 | GCstr * LJ_FASTCALL lj_strfmt_num(lua_State *L, cTValue *o) | ||
426 | { | ||
427 | char buf[STRFMT_MAXBUF_NUM]; | ||
428 | MSize len = (MSize)(lj_strfmt_wnum(buf, o) - buf); | ||
429 | return lj_str_new(L, buf, len); | ||
430 | } | ||
431 | |||
432 | /* Convert integer or number to string. */ | ||
433 | GCstr * LJ_FASTCALL lj_strfmt_number(lua_State *L, cTValue *o) | ||
434 | { | ||
435 | return tvisint(o) ? lj_strfmt_int(L, intV(o)) : lj_strfmt_num(L, o); | ||
436 | } | ||
437 | |||
438 | #if LJ_HASJIT | ||
439 | /* Convert char value to string. */ | ||
440 | GCstr * LJ_FASTCALL lj_strfmt_char(lua_State *L, int c) | ||
441 | { | ||
442 | char buf[1]; | ||
443 | buf[0] = c; | ||
444 | return lj_str_new(L, buf, 1); | ||
445 | } | ||
446 | #endif | ||
447 | |||
448 | /* Raw conversion of object to string. */ | ||
449 | GCstr * LJ_FASTCALL lj_strfmt_obj(lua_State *L, cTValue *o) | ||
450 | { | ||
451 | if (tvisstr(o)) { | ||
452 | return strV(o); | ||
453 | } else if (tvisnumber(o)) { | ||
454 | return lj_strfmt_number(L, o); | ||
455 | } else if (tvisnil(o)) { | ||
456 | return lj_str_newlit(L, "nil"); | ||
457 | } else if (tvisfalse(o)) { | ||
458 | return lj_str_newlit(L, "false"); | ||
459 | } else if (tvistrue(o)) { | ||
460 | return lj_str_newlit(L, "true"); | ||
461 | } else { | ||
462 | char buf[8+2+2+16], *p = buf; | ||
463 | p = lj_buf_wmem(p, lj_typename(o), (MSize)strlen(lj_typename(o))); | ||
464 | *p++ = ':'; *p++ = ' '; | ||
465 | if (tvisfunc(o) && isffunc(funcV(o))) { | ||
466 | p = lj_buf_wmem(p, "builtin#", 8); | ||
467 | p = lj_strfmt_wint(p, funcV(o)->c.ffid); | ||
468 | } else { | ||
469 | p = lj_strfmt_wptr(p, lj_obj_ptr(o)); | ||
470 | } | ||
471 | return lj_str_new(L, buf, (size_t)(p - buf)); | ||
472 | } | ||
473 | } | ||
474 | |||
475 | /* -- Internal string formatting ------------------------------------------ */ | ||
476 | |||
477 | /* | ||
478 | ** These functions are only used for lua_pushfstring(), lua_pushvfstring() | ||
479 | ** and for internal string formatting (e.g. error messages). Caveat: unlike | ||
480 | ** string.format(), only a limited subset of formats and flags are supported! | ||
481 | ** | ||
482 | ** LuaJIT has support for a couple more formats than Lua 5.1/5.2: | ||
483 | ** - %d %u %o %x with full formatting, 32 bit integers only. | ||
484 | ** - %f and other FP formats are really %.14g. | ||
485 | ** - %s %c %p without formatting. | ||
486 | */ | ||
487 | |||
488 | /* Push formatted message as a string object to Lua stack. va_list variant. */ | ||
489 | const char *lj_strfmt_pushvf(lua_State *L, const char *fmt, va_list argp) | ||
490 | { | ||
491 | SBuf *sb = lj_buf_tmp_(L); | ||
492 | FormatState fs; | ||
493 | SFormat sf; | ||
494 | GCstr *str; | ||
495 | lj_strfmt_init(&fs, fmt, (MSize)strlen(fmt)); | ||
496 | while ((sf = lj_strfmt_parse(&fs)) != STRFMT_EOF) { | ||
497 | switch (STRFMT_TYPE(sf)) { | ||
498 | case STRFMT_LIT: | ||
499 | lj_buf_putmem(sb, fs.str, fs.len); | ||
500 | break; | ||
501 | case STRFMT_INT: | ||
502 | lj_strfmt_putfxint(sb, sf, va_arg(argp, int32_t)); | ||
503 | break; | ||
504 | case STRFMT_UINT: | ||
505 | lj_strfmt_putfxint(sb, sf, va_arg(argp, uint32_t)); | ||
506 | break; | ||
507 | case STRFMT_NUM: { | ||
508 | TValue tv; | ||
509 | tv.n = va_arg(argp, lua_Number); | ||
510 | setsbufP(sb, lj_strfmt_wnum(lj_buf_more(sb, STRFMT_MAXBUF_NUM), &tv)); | ||
511 | break; | ||
512 | } | ||
513 | case STRFMT_STR: { | ||
514 | const char *s = va_arg(argp, char *); | ||
515 | if (s == NULL) s = "(null)"; | ||
516 | lj_buf_putmem(sb, s, (MSize)strlen(s)); | ||
517 | break; | ||
518 | } | ||
519 | case STRFMT_CHAR: | ||
520 | lj_buf_putb(sb, va_arg(argp, int)); | ||
521 | break; | ||
522 | case STRFMT_PTR: | ||
523 | setsbufP(sb, lj_strfmt_wptr(lj_buf_more(sb, STRFMT_MAXBUF_PTR), | ||
524 | va_arg(argp, void *))); | ||
525 | break; | ||
526 | case STRFMT_ERR: | ||
527 | default: | ||
528 | lj_buf_putb(sb, '?'); | ||
529 | lua_assert(0); | ||
530 | break; | ||
531 | } | ||
532 | } | ||
533 | str = lj_buf_str(L, sb); | ||
534 | setstrV(L, L->top, str); | ||
535 | incr_top(L); | ||
536 | return strdata(str); | ||
537 | } | ||
538 | |||
539 | /* Push formatted message as a string object to Lua stack. Vararg variant. */ | ||
540 | const char *lj_strfmt_pushf(lua_State *L, const char *fmt, ...) | ||
541 | { | ||
542 | const char *msg; | ||
543 | va_list argp; | ||
544 | va_start(argp, fmt); | ||
545 | msg = lj_strfmt_pushvf(L, fmt, argp); | ||
546 | va_end(argp); | ||
547 | return msg; | ||
548 | } | ||
549 | |||