lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Dec 06, 2010 at 08:11:55AM +0200, steve donovan wrote:
> local format = string.format
>
> function formatx (fmt,...)
>     local args = {...}
>     local i = 1
>     for p in fmt:gmatch('%%s') do
>         if type(args[i]) ~= 'string' then
>             args[i] = tostring(args[i])
>         end
>         i = i + 1
>     end
>     return format(fmt,unpack(args))
> end

That doesn't work for formats with any non-%s specifiers. Any non-%s
specifier will throw off the i counter for later arguments.

formatx('%s(%x)=%s(%x)',1,5,nil,10)
lua: test.lua:13: bad argument #4 to 'format' (string expected, got nil)

It also isn't safe for argument lists with holes as {}/unpack isn't
guaranteed to preserve the full list when presented with holes, but this
is easy enough to fix with select.

    -Etan