MCPcopy Create free account
hub / github.com/ArduPilot/ardupilot / str_format

Function str_format

libraries/AP_Scripting/lua/src/lstrlib.c:1023–1100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.85
luaL_checklstringFunction · 0.85
luaL_buffinitFunction · 0.85
luaL_prepbuffsizeFunction · 0.85
luaL_argerrorFunction · 0.85
scanformatFunction · 0.85
luaL_checkintegerFunction · 0.85
addlenmodFunction · 0.85
lua_number2strxFunction · 0.85
luaL_checknumberFunction · 0.85
addliteralFunction · 0.85
luaL_tolstringFunction · 0.85

Tested by

no test coverage detected