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