| 14431 | |
| 14432 | |
| 14433 | static int str_format (lua_State *L) { |
| 14434 | int arg = 1; |
| 14435 | size_t sfl; |
| 14436 | const char *strfrmt = luaL_checklstring(L, arg, &sfl); |
| 14437 | const char *strfrmt_end = strfrmt+sfl; |
| 14438 | luaL_Buffer b; |
| 14439 | luaL_buffinit(L, &b); |
| 14440 | while (strfrmt < strfrmt_end) { |
| 14441 | if (*strfrmt != L_ESC) |
| 14442 | luaL_addchar(&b, *strfrmt++); |
| 14443 | else if (*++strfrmt == L_ESC) |
| 14444 | luaL_addchar(&b, *strfrmt++); /* %% */ |
| 14445 | else { /* format item */ |
| 14446 | char form[MAX_FORMAT]; /* to store the format (`%...') */ |
| 14447 | char buff[MAX_ITEM]; /* to store the formatted item */ |
| 14448 | arg++; |
| 14449 | strfrmt = scanformat(L, strfrmt, form); |
| 14450 | switch (*strfrmt++) { |
| 14451 | case 'c': { |
| 14452 | sprintf(buff, form, (int)luaL_checknumber(L, arg)); |
| 14453 | break; |
| 14454 | } |
| 14455 | case 'd': case 'i': { |
| 14456 | addintlen(form); |
| 14457 | sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); |
| 14458 | break; |
| 14459 | } |
| 14460 | case 'o': case 'u': case 'x': case 'X': { |
| 14461 | addintlen(form); |
| 14462 | sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); |
| 14463 | break; |
| 14464 | } |
| 14465 | case 'e': case 'E': case 'f': |
| 14466 | case 'g': case 'G': { |
| 14467 | sprintf(buff, form, (double)luaL_checknumber(L, arg)); |
| 14468 | break; |
| 14469 | } |
| 14470 | case 'q': { |
| 14471 | addquoted(L, &b, arg); |
| 14472 | continue; /* skip the 'addsize' at the end */ |
| 14473 | } |
| 14474 | case 's': { |
| 14475 | size_t l; |
| 14476 | const char *s = luaL_checklstring(L, arg, &l); |
| 14477 | if (!strchr(form, '.') && l >= 100) { |
| 14478 | /* no precision and string is too long to be formatted; |
| 14479 | keep original string */ |
| 14480 | lua_pushvalue(L, arg); |
| 14481 | luaL_addvalue(&b); |
| 14482 | continue; /* skip the `addsize' at the end */ |
| 14483 | } |
| 14484 | else { |
| 14485 | sprintf(buff, form, s); |
| 14486 | break; |
| 14487 | } |
| 14488 | } |
| 14489 | default: { /* also treat cases `pnLlh' */ |
| 14490 | return luaL_error(L, "invalid option " LUA_QL("%%%c") " to " |
nothing calls this directly
no test coverage detected