lua-users home
lua-l archive

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


2014-10-27 9:34 GMT+02:00 Alexandre (gaumerie) <gaumerie@hotmail.com>:
>
> It could be an interesting addition to have a capture system in
> the struct pack/unpack functions, similar to captures in the pattern
> matching functions.


True, parentheses are still available for struct syntax.The
rule would have to be: return only the captures if there is at
least one such specified, otherwise return all items.

> If one wants to read a C structure, but only needs some values from
> the structure. Sure, you can:
>   - read all the data structure and get the values you need from
> the return values, but so you have to deal with potentially a lot of
> return values or create a temporary table.
>   - look for the offset of the values you want to read, and read one
> by one, but it is more work and you have to deal with data size and
> alignement in computing the offset.

You could do

wanted = select(i,struct.unpack(fmt,s))

or keep the desired format options as a list, then

offset = #struct.pack(table.concat(options,1,i-1))
nwanted = #offset+1 -- do this once and for all
wanted = struct.unpack(options[i],s,nwanted)