| 754 | |
| 755 | |
| 756 | static 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 | strfrmt = scanformat(L, strfrmt, form); |
| 775 | switch (*strfrmt++) { |
| 776 | case 'c': { |
| 777 | sprintf(buff, form, (int)luaL_checknumber(L, arg)); |
| 778 | break; |
| 779 | } |
| 780 | case 'd': case 'i': { |
| 781 | addintlen(form); |
| 782 | sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg)); |
| 783 | break; |
| 784 | } |
| 785 | case 'o': case 'u': case 'x': case 'X': { |
| 786 | addintlen(form); |
| 787 | sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg)); |
| 788 | break; |
| 789 | } |
| 790 | case 'e': case 'E': case 'f': |
| 791 | case 'g': case 'G': { |
| 792 | sprintf(buff, form, (double)luaL_checknumber(L, arg)); |
| 793 | break; |
| 794 | } |
| 795 | case 'q': { |
| 796 | addquoted(L, &b, arg); |
| 797 | continue; /* skip the 'addsize' at the end */ |
| 798 | } |
| 799 | case 's': { |
| 800 | size_t l; |
| 801 | const char *s = luaL_checklstring(L, arg, &l); |
| 802 | if (!strchr(form, '.') && l >= 100) { |
| 803 | /* no precision and string is too long to be formatted; |
| 804 | keep original string */ |
| 805 | lua_pushvalue(L, arg); |
| 806 | luaL_addvalue(&b); |
| 807 | continue; /* skip the `addsize' at the end */ |
| 808 | } |
| 809 | else { |
| 810 | sprintf(buff, form, s); |
| 811 | break; |
| 812 | } |
| 813 | } |
nothing calls this directly
no test coverage detected