this function handles only `%d', `%c', %f, %p, and `%s' formats */
| 7369 | |
| 7370 | /* this function handles only `%d', `%c', %f, %p, and `%s' formats */ |
| 7371 | const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) { |
| 7372 | int n = 1; |
| 7373 | pushstr(L, ""); |
| 7374 | for (;;) { |
| 7375 | const char *e = strchr(fmt, '%'); |
| 7376 | if (e == NULL) break; |
| 7377 | setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt)); |
| 7378 | incr_top(L); |
| 7379 | switch (*(e+1)) { |
| 7380 | case 's': { |
| 7381 | const char *s = va_arg(argp, char *); |
| 7382 | if (s == NULL) s = "(null)"; |
| 7383 | pushstr(L, s); |
| 7384 | break; |
| 7385 | } |
| 7386 | case 'c': { |
| 7387 | char buff[2]; |
| 7388 | buff[0] = cast(char, va_arg(argp, int)); |
| 7389 | buff[1] = '\0'; |
| 7390 | pushstr(L, buff); |
| 7391 | break; |
| 7392 | } |
| 7393 | case 'd': { |
| 7394 | setnvalue(L->top, cast_num(va_arg(argp, int))); |
| 7395 | incr_top(L); |
| 7396 | break; |
| 7397 | } |
| 7398 | case 'f': { |
| 7399 | setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber))); |
| 7400 | incr_top(L); |
| 7401 | break; |
| 7402 | } |
| 7403 | case 'p': { |
| 7404 | char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */ |
| 7405 | sprintf(buff, "%p", va_arg(argp, void *)); |
| 7406 | pushstr(L, buff); |
| 7407 | break; |
| 7408 | } |
| 7409 | case '%': { |
| 7410 | pushstr(L, "%"); |
| 7411 | break; |
| 7412 | } |
| 7413 | default: { |
| 7414 | char buff[3]; |
| 7415 | buff[0] = '%'; |
| 7416 | buff[1] = *(e+1); |
| 7417 | buff[2] = '\0'; |
| 7418 | pushstr(L, buff); |
| 7419 | break; |
| 7420 | } |
| 7421 | } |
| 7422 | n += 2; |
| 7423 | fmt = e+2; |
| 7424 | } |
| 7425 | pushstr(L, fmt); |
| 7426 | luaV_concat(L, n+1, cast_int(L->top - L->base) - 1); |
| 7427 | L->top -= n; |
| 7428 | return svalue(L->top - 1); |
no test coverage detected