MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / str_format

Function str_format

third-party/lua-5.5.0/src/lstrlib.c:1277–1382  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1275
1276
1277static int str_format (lua_State *L) {
1278 int top = lua_gettop(L);
1279 int arg = 1;
1280 size_t sfl;
1281 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
1282 const char *strfrmt_end = strfrmt+sfl;
1283 const char *flags;
1284 luaL_Buffer b;
1285 luaL_buffinit(L, &b);
1286 while (strfrmt < strfrmt_end) {
1287 if (*strfrmt != L_ESC)
1288 luaL_addchar(&b, *strfrmt++);
1289 else if (*++strfrmt == L_ESC)
1290 luaL_addchar(&b, *strfrmt++); /* %% */
1291 else { /* format item */
1292 char form[MAX_FORMAT]; /* to store the format ('%...') */
1293 unsigned maxitem = MAX_ITEM; /* maximum length for the result */
1294 char *buff = luaL_prepbuffsize(&b, maxitem); /* to put result */
1295 int nb = 0; /* number of bytes in result */
1296 if (++arg > top)
1297 return luaL_argerror(L, arg, "no value");
1298 strfrmt = getformat(L, strfrmt, form);
1299 switch (*strfrmt++) {
1300 case 'c': {
1301 checkformat(L, form, L_FMTFLAGSC, 0);
1302 nb = l_sprintf(buff, maxitem, form, (int)luaL_checkinteger(L, arg));
1303 break;
1304 }
1305 case 'd': case 'i':
1306 flags = L_FMTFLAGSI;
1307 goto intcase;
1308 case 'u':
1309 flags = L_FMTFLAGSU;
1310 goto intcase;
1311 case 'o': case 'x': case 'X':
1312 flags = L_FMTFLAGSX;
1313 intcase: {
1314 lua_Integer n = luaL_checkinteger(L, arg);
1315 checkformat(L, form, flags, 1);
1316 addlenmod(form, LUA_INTEGER_FRMLEN);
1317 nb = l_sprintf(buff, maxitem, form, (LUAI_UACINT)n);
1318 break;
1319 }
1320 case 'a': case 'A':
1321 checkformat(L, form, L_FMTFLAGSF, 1);
1322 addlenmod(form, LUA_NUMBER_FRMLEN);
1323 nb = lua_number2strx(L, buff, maxitem, form,
1324 luaL_checknumber(L, arg));
1325 break;
1326 case 'f':
1327 maxitem = MAX_ITEMF; /* extra space for '%f' */
1328 buff = luaL_prepbuffsize(&b, maxitem);
1329 /* FALLTHROUGH */
1330 case 'e': case 'E': case 'g': case 'G': {
1331 lua_Number n = luaL_checknumber(L, arg);
1332 checkformat(L, form, L_FMTFLAGSF, 1);
1333 addlenmod(form, LUA_NUMBER_FRMLEN);
1334 nb = l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.70
luaL_checklstringFunction · 0.70
luaL_buffinitFunction · 0.70
luaL_prepbuffsizeFunction · 0.70
luaL_argerrorFunction · 0.70
getformatFunction · 0.70
checkformatFunction · 0.70
luaL_checkintegerFunction · 0.70
addlenmodFunction · 0.70
lua_number2strxFunction · 0.70
luaL_checknumberFunction · 0.70
lua_topointerFunction · 0.70

Tested by

no test coverage detected