| 901 | |
| 902 | |
| 903 | static int str_format (lua_State *L) { |
| 904 | int top = lua_gettop(L); |
| 905 | int arg = 1; |
| 906 | size_t sfl; |
| 907 | const char *strfrmt = luaL_checklstring(L, arg, &sfl); |
| 908 | const char *strfrmt_end = strfrmt+sfl; |
| 909 | luaL_Buffer b; |
| 910 | luaL_buffinit(L, &b); |
| 911 | while (strfrmt < strfrmt_end) { |
| 912 | if (*strfrmt != L_ESC) |
| 913 | luaL_addchar(&b, *strfrmt++); |
| 914 | else if (*++strfrmt == L_ESC) |
| 915 | luaL_addchar(&b, *strfrmt++); /* %% */ |
| 916 | else { /* format item */ |
| 917 | char form[MAX_FORMAT]; /* to store the format (`%...') */ |
| 918 | char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */ |
| 919 | int nb = 0; /* number of bytes in added item */ |
| 920 | if (++arg > top) |
| 921 | luaL_argerror(L, arg, "no value"); |
| 922 | strfrmt = scanformat(L, strfrmt, form); |
| 923 | switch (*strfrmt++) { |
| 924 | case 'c': { |
| 925 | nb = str_sprintf(buff, form, luaL_checkint(L, arg)); |
| 926 | break; |
| 927 | } |
| 928 | case 'd': case 'i': { |
| 929 | lua_Number n = luaL_checknumber(L, arg); |
| 930 | LUA_INTFRM_T ni = (LUA_INTFRM_T)n; |
| 931 | lua_Number diff = n - (lua_Number)ni; |
| 932 | luaL_argcheck(L, -1 < diff && diff < 1, arg, |
| 933 | "not a number in proper range"); |
| 934 | addlenmod(form, LUA_INTFRMLEN, MAX_FORMAT); |
| 935 | nb = str_sprintf(buff, form, ni); |
| 936 | break; |
| 937 | } |
| 938 | case 'o': case 'u': case 'x': case 'X': { |
| 939 | lua_Number n = luaL_checknumber(L, arg); |
| 940 | unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n; |
| 941 | lua_Number diff = n - (lua_Number)ni; |
| 942 | luaL_argcheck(L, -1 < diff && diff < 1, arg, |
| 943 | "not a non-negative number in proper range"); |
| 944 | addlenmod(form, LUA_INTFRMLEN, MAX_FORMAT); |
| 945 | nb = str_sprintf(buff, form, ni); |
| 946 | break; |
| 947 | } |
| 948 | #if defined(LUA_USE_FLOAT_FORMATS) |
| 949 | case 'e': case 'E': case 'f': |
| 950 | #if defined(LUA_USE_AFORMAT) |
| 951 | case 'a': case 'A': |
| 952 | #endif |
| 953 | case 'g': case 'G': { |
| 954 | addlenmod(form, LUA_FLTFRMLEN, MAX_FORMAT); |
| 955 | nb = str_sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg)); |
| 956 | break; |
| 957 | } |
| 958 | #endif |
| 959 | case 'q': { |
| 960 | addquoted(L, &b, arg); |
nothing calls this directly
no test coverage detected