Better %*

This commit is contained in:
Kampfkarren 2022-07-27 17:34:16 -07:00
parent e496c86581
commit b43fe82465
2 changed files with 10 additions and 2 deletions

View file

@ -1034,8 +1034,13 @@ static int str_format(lua_State* L)
} }
case '*': case '*':
{ {
sprintf(buff, "%s", luaL_tolstring(L, arg, NULL)); size_t length;
break; const char* string = luaL_tolstring(L, arg, &length);
luaL_reservebuffer(&b, length, -1);
luaL_addlstring(&b, string, length);
continue; /* skip the `addsize' at the end */
} }
default: default:
{ /* also treat cases `pnLlh' */ { /* also treat cases `pnLlh' */

View file

@ -143,6 +143,9 @@ assert(pcall(function()
string.format("%* %* %*", return_two_nils()) string.format("%* %* %*", return_two_nils())
end) == false) end) == false)
assert(string.format("%*", "a\0b\0c") == "a\0b\0c")
assert(string.format("%*", string.rep("doge", 3000)) == string.rep("doge", 3000))
assert(loadstring("return 1\n--comentário sem EOL no final")() == 1) assert(loadstring("return 1\n--comentário sem EOL no final")() == 1)