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