lua-users home
lua-l archive

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


2014-08-01 5:21 GMT+02:00 Andrew Starks <andrew.starks@trms.com>:
> Specifically, the debug.Csize, integer and float packing functions; there
> usage is clear, but I'd be interested to know about the thinking around
> including these sorts of tools, absent a lua-side buffer type. Most of these
> are in the string library. The cost/benefit must have tilted toward strings.

Four functions with names like string.undumpfloat merely for access
to the machine representation of one number at a time is not elegant.
The intention is clearly to facilitate eventual reading and writing of
binary files without painful conversions, so why not handle several
numbers at once?

One could do the writing part by adding some specifiers to
string.format, or (since string.format implies just the sort of
conversion that we wish to avoid) bring in string.bformat (binary
format) with a similar syntax. For the inverse operation,
string.bscan seems a natural name.

For example, instead of

str = table.concat{string.dumpfloat(n1,"f","b"),string.dumpint(n2,8,"l")}

one would write

str = string.bformat("%fb%8l",n1,n2)

and

n1, n2 = string.scanb(str,"%fb%8l")

instead of some code that I am too terrified even to attempt.