lua-users home
lua-l archive

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


2014-11-28 20:47 GMT+02:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:

>> I  think  `struct.size` is quite  useful  function  and it hard to implement in
>> Lua itself.
>
> Can't you use #string.pack(fmt, ...), with specific dummy arguments?

0 works everywhere except c[n], which needs the exact length to match.

But "hard to implement in Lua" is fair also for the dummy argument
approach. You need to be able to generate a list `0,0,0,0,...` of at least
the right length, punctuated by the occasional string.rep(' ',n). This
amounts to parsing `fmt` in Lua.

A solution requiring no new named function, only extended semantics,
would be for string.pack(fmt) to return a template by in effect generating
its own dummy arguments. For example, -1 for signed, 0 for unsigned,
0.4 for floating-point would make a pretty picture.

> hex(string.pack('bHT>d'))
FF000000000000000000003FD999999999999A

Conversion specifiers z and s should generate errors.

Not suggesting that an actual `nil` is to be replaced by a dummy, only
the case when vararg has length zero.