MCPcopy Create free account
hub / github.com/Achain-Dev/Achain / str_format

Function str_format

src/Chain/libraries/glua/lstrlib.cpp:1049–1125  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1047}
1048
1049static int str_format(lua_State *L) {
1050 int top = lua_gettop(L);
1051 int arg = 1;
1052 size_t sfl;
1053 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
1054 const char *strfrmt_end = strfrmt + sfl;
1055 luaL_Buffer b;
1056 luaL_buffinit(L, &b);
1057 while (strfrmt < strfrmt_end) {
1058 if (*strfrmt != L_ESC)
1059 luaL_addchar(&b, *strfrmt++);
1060 else if (*++strfrmt == L_ESC)
1061 luaL_addchar(&b, *strfrmt++); /* %% */
1062 else { /* format item */
1063 char form[MAX_FORMAT]; /* to store the format ('%...') */
1064 char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
1065 int nb = 0; /* number of bytes in added item */
1066 if (++arg > top)
1067 luaL_argerror(L, arg, "no value");
1068 strfrmt = scanformat(L, strfrmt, form);
1069 switch (*strfrmt++) {
1070 case 'c': {
1071 nb = l_sprintf(buff, MAX_ITEM, form, (int)luaL_checkinteger(L, arg));
1072 break;
1073 }
1074 case 'd': case 'i':
1075 case 'o': case 'u': case 'x': case 'X': {
1076 lua_Integer n = luaL_checkinteger(L, arg);
1077 addlenmod(form, LUA_INTEGER_FRMLEN);
1078 nb = l_sprintf(buff, MAX_ITEM, form, n);
1079 break;
1080 }
1081 case 'a': case 'A':
1082 addlenmod(form, LUA_NUMBER_FRMLEN);
1083 nb = lua_number2strx(L, buff, MAX_ITEM, form,
1084 luaL_checknumber(L, arg));
1085 break;
1086 case 'e': case 'E': case 'f':
1087 case 'g': case 'G': {
1088 addlenmod(form, LUA_NUMBER_FRMLEN);
1089 nb = l_sprintf(buff, MAX_ITEM, form, luaL_checknumber(L, arg));
1090 break;
1091 }
1092 case 'q': {
1093 addquoted(L, &b, arg);
1094 break;
1095 }
1096 case 's': {
1097 size_t l;
1098 const char *s = luaL_tolstring(L, arg, &l);
1099 if (form[2] == '\0') /* no modifiers? */
1100 luaL_addvalue(&b); /* keep entire string */
1101 else {
1102 luaL_argcheck(L, l == strlen(s), arg, "string contains zeros");
1103 if (!strchr(form, '.') && l >= 100) {
1104 /* no precision and string is too long to be formatted */
1105 luaL_addvalue(&b); /* keep entire string */
1106 }

Callers

nothing calls this directly

Calls 13

lua_gettopFunction · 0.85
luaL_checklstringFunction · 0.85
luaL_buffinitFunction · 0.85
luaL_prepbuffsizeFunction · 0.85
luaL_argerrorFunction · 0.85
scanformatFunction · 0.85
addlenmodFunction · 0.85
lua_number2strxFunction · 0.85
addquotedFunction · 0.85
luaL_tolstringFunction · 0.85
luaL_addvalueFunction · 0.85
luaL_errorFunction · 0.85

Tested by

no test coverage detected