MCPcopy Create free account
hub / github.com/RomanKubiak/ctrlr / str_format

Function str_format

Source/Misc/lua/src/lua.c:14433–14499  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 11

luaL_checklstringFunction · 0.85
luaL_buffinitFunction · 0.85
scanformatFunction · 0.85
luaL_checknumberFunction · 0.85
addintlenFunction · 0.85
addquotedFunction · 0.85
lua_pushvalueFunction · 0.85
luaL_addvalueFunction · 0.85
luaL_errorFunction · 0.85
luaL_addlstringFunction · 0.85
luaL_pushresultFunction · 0.85

Tested by

no test coverage detected