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

Function str_format

third-party/lua-5.3.5/src/lstrlib.c:1016–1093  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1014
1015
1016static int str_format (lua_State *L) {
1017 int top = lua_gettop(L);
1018 int arg = 1;
1019 size_t sfl;
1020 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
1021 const char *strfrmt_end = strfrmt+sfl;
1022 luaL_Buffer b;
1023 luaL_buffinit(L, &b);
1024 while (strfrmt < strfrmt_end) {
1025 if (*strfrmt != L_ESC)
1026 luaL_addchar(&b, *strfrmt++);
1027 else if (*++strfrmt == L_ESC)
1028 luaL_addchar(&b, *strfrmt++); /* %% */
1029 else { /* format item */
1030 char form[MAX_FORMAT]; /* to store the format ('%...') */
1031 char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
1032 int nb = 0; /* number of bytes in added item */
1033 if (++arg > top)
1034 luaL_argerror(L, arg, "no value");
1035 strfrmt = scanformat(L, strfrmt, form);
1036 switch (*strfrmt++) {
1037 case 'c': {
1038 nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
1039 break;
1040 }
1041 case 'd': case 'i':
1042 case 'o': case 'u': case 'x': case 'X': {
1043 lua_Integer n = luaL_checkinteger(L, arg);
1044 addlenmod(form, LUA_INTEGER_FRMLEN);
1045 nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACINT)n);
1046 break;
1047 }
1048 case 'a': case 'A':
1049 addlenmod(form, LUA_NUMBER_FRMLEN);
1050 nb = lua_number2strx(L, buff, MAX_ITEM, form,
1051 luaL_checknumber(L, arg));
1052 break;
1053 case 'e': case 'E': case 'f':
1054 case 'g': case 'G': {
1055 lua_Number n = luaL_checknumber(L, arg);
1056 addlenmod(form, LUA_NUMBER_FRMLEN);
1057 nb = l_sprintf(buff, MAX_ITEM, form, (LUAI_UACNUMBER)n);
1058 break;
1059 }
1060 case 'q': {
1061 addliteral(L, &b, arg);
1062 break;
1063 }
1064 case 's': {
1065 size_t l;
1066 const char *s = luaL_tolstring(L, arg, &l);
1067 if (form[2] == '\0') /* no modifiers? */
1068 luaL_addvalue(&b); /* keep entire string */
1069 else {
1070 luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
1071 if (!strchr(form, '.') && l >= 100) {
1072 /* no precision and string is too long to be formatted */
1073 luaL_addvalue(&b); /* keep entire string */

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
scanformatFunction · 0.70
luaL_checkintegerFunction · 0.70
addlenmodFunction · 0.70
lua_number2strxFunction · 0.70
luaL_checknumberFunction · 0.70
addliteralFunction · 0.70
luaL_tolstringFunction · 0.70

Tested by

no test coverage detected