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

Function str_format

other_src/lua/src/lstrlib.cpp:756–825  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

754
755
756static int str_format (lua_State *L) {
757 int top = lua_gettop(L);
758 int arg = 1;
759 size_t sfl;
760 const char *strfrmt = luaL_checklstring(L, arg, &sfl);
761 const char *strfrmt_end = strfrmt+sfl;
762 luaL_Buffer b;
763 luaL_buffinit(L, &b);
764 while (strfrmt < strfrmt_end) {
765 if (*strfrmt != L_ESC)
766 luaL_addchar(&b, *strfrmt++);
767 else if (*++strfrmt == L_ESC)
768 luaL_addchar(&b, *strfrmt++); /* %% */
769 else { /* format item */
770 char form[MAX_FORMAT]; /* to store the format (`%...') */
771 char buff[MAX_ITEM]; /* to store the formatted item */
772 if (++arg > top) {
773 luaL_argerror(L, arg, "no value");
774 }
775 strfrmt = scanformat(L, strfrmt, form);
776 switch (*strfrmt++) {
777 case 'c': {
778 sprintf(buff, form, (int)luaL_checknumber(L, arg));
779 break;
780 }
781 case 'd': case 'i': {
782 addintlen(form);
783 sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
784 break;
785 }
786 case 'o': case 'u': case 'x': case 'X': {
787 addintlen(form);
788 sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
789 break;
790 }
791 case 'e': case 'E': case 'f':
792 case 'g': case 'G': {
793 sprintf(buff, form, (double)luaL_checknumber(L, arg));
794 break;
795 }
796 case 'q': {
797 addquoted(L, &b, arg);
798 continue; /* skip the 'addsize' at the end */
799 }
800 case 's': {
801 size_t l;
802 const char *s = luaL_checklstring(L, arg, &l);
803 if (!strchr(form, '.') && l >= 100) {
804 /* no precision and string is too long to be formatted;
805 keep original string */
806 lua_pushvalue(L, arg);
807 luaL_addvalue(&b);
808 continue; /* skip the `addsize' at the end */
809 }
810 else {
811 sprintf(buff, form, s);
812 break;
813 }

Callers

nothing calls this directly

Calls 13

luaL_checknumberFunction · 0.85
lua_gettopFunction · 0.70
luaL_checklstringFunction · 0.70
luaL_buffinitFunction · 0.70
luaL_argerrorFunction · 0.70
scanformatFunction · 0.70
addintlenFunction · 0.70
addquotedFunction · 0.70
lua_pushvalueFunction · 0.70
luaL_addvalueFunction · 0.70
luaL_errorFunction · 0.70
luaL_addlstringFunction · 0.70

Tested by

no test coverage detected