this function handles only '%d', '%c', '%f', '%p', and '%s' conventional formats, plus Lua-specific '%I' and '%U' */
| 346 | /* this function handles only '%d', '%c', '%f', '%p', and '%s' |
| 347 | conventional formats, plus Lua-specific '%I' and '%U' */ |
| 348 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { |
| 349 | int n = 0; |
| 350 | for (;;) { |
| 351 | const char *e = strchr(fmt, '%'); |
| 352 | if (e == NULL) break; |
| 353 | luaD_checkstack(L, 2); /* fmt + item */ |
| 354 | pushstr(L, fmt, e - fmt); |
| 355 | switch (*(e+1)) { |
| 356 | case 's': { |
| 357 | const char *s = va_arg(argp, char *); |
| 358 | if (s == NULL) s = "(null)"; |
| 359 | pushstr(L, s, strlen(s)); |
| 360 | break; |
| 361 | } |
| 362 | case 'c': { |
| 363 | char buff = cast(char, va_arg(argp, int)); |
| 364 | if (lisprint(cast_uchar(buff))) |
| 365 | pushstr(L, &buff, 1); |
| 366 | else /* non-printable character; print its code */ |
| 367 | luaO_pushfstring(L, "<\\%d>", cast_uchar(buff)); |
| 368 | break; |
| 369 | } |
| 370 | case 'd': { |
| 371 | setivalue(L->top++, va_arg(argp, int)); |
| 372 | luaO_tostring(L, L->top - 1); |
| 373 | break; |
| 374 | } |
| 375 | case 'I': { |
| 376 | setivalue(L->top++, cast(lua_Integer, va_arg(argp, l_uacInt))); |
| 377 | luaO_tostring(L, L->top - 1); |
| 378 | break; |
| 379 | } |
| 380 | case 'f': { |
| 381 | setfltvalue(L->top++, cast_num(va_arg(argp, l_uacNumber))); |
| 382 | luaO_tostring(L, L->top - 1); |
| 383 | break; |
| 384 | } |
| 385 | case 'p': { |
| 386 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a '%p' */ |
| 387 | int l = sprintf(buff, "%p", va_arg(argp, void *)); |
| 388 | pushstr(L, buff, l); |
| 389 | break; |
| 390 | } |
| 391 | case 'U': { |
| 392 | char buff[UTF8BUFFSZ]; |
| 393 | int l = luaO_utf8esc(buff, cast(long, va_arg(argp, long))); |
| 394 | pushstr(L, buff + UTF8BUFFSZ - l, l); |
| 395 | break; |
| 396 | } |
| 397 | case '%': { |
| 398 | pushstr(L, "%", 1); |
| 399 | break; |
| 400 | } |
| 401 | default: { |
| 402 | luaG_runerror(L, "invalid option '%%%c' to 'lua_pushfstring'", |
| 403 | *(e + 1)); |
| 404 | } |
| 405 | } |
no test coverage detected