[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Questions about string.pack() and string.unpack()
- From: Sean Conner <sean@...>
- Date: Wed, 19 Nov 2014 20:06:27 -0500
I'm looking over string.pack() and string.unpack() in Lua 5.3, and I have
a few questions:
1. Why is string.unpack(fmt,s[,pos]) instead of string.unpack(s,fmt[,pos])?
This means I can't do:
magic,timestamp,tag = raw:unpack("I4I4I4")
Instead, it's:
magic,timestamp,tag = string.unpack("I4I4I4",raw)
This also means I may have to cache the string table in a local variable
in a module when it might be avoided. The former also "reads" better
to me.
2. I notice there isn't an option to return the rest of the string. The
closest you have is the "c[n]" format, but that works when you know the
length of the string. In my case, the rest of the packet isn't binary
but all text and thus, the regular Lua patterns or LPeg can be used. I
could use raw:sub(pastbinaryportion) to get it, but again, it seems like
it could be a bit cleaner with a way to return just the rest of the
string.
3. Is the following a legal format?
">I4<i4"
That is, a big endian integer, followed by a little endian integer?
4. The Lua lpack module includes a way to specify native endian.
string.pack() and string.unpack() default to native endian, but if #3 is
valid, then a way to flip back to the native endian would be nice.
5. Could we get these functions in a standalone module for Lua 5.1 and 5.2?
I would love to be able to use these (lots of network parsing and what
not) but I'm currently stuck at Lua 5.1 at work for the time being [1].
-spc (Also, the link to "debug.sizeof" is broken in the 5.3 manual)
[1] Major reason being the remote possibility of using LuaJIT. While
currently we're running on SPARC (not supported by LuaJIT), there is
a very remote possiblity of a switch to Linux.