lua-users home
lua-l archive

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


Here the new command option list for pack/unpack... .

(If you want to read more background/ motivation text, then start reading
the next blog. The lua_Number conversion for 'N' type is specified in the
last blog).

I grouped the command options into the following groups:
-	basic options with specified length (e. g. q, i, f, c…)
-	basic options with variable length (e. g. N, s, S, z)
-	non-nested indexed tables, specified length possible (^)
-	non-nested tables, variable table length, specified element length
possible (|, :, *)
-	non-nested tables, variable table length, N type (t)
-	nested tables, variable table length, N type (T)
-	dynamic repeat specifier (#)
-	dynamic element length specifier (_)
-	Background options: endianess (>,<),  delimiters (space, colon, semicolon)

If a number format exceeds the native size of the lua system:
pack will add zero’s / 0xFF’s on the MSB side for ints, floats it “pimp up”
correctly
unpack will use MININT/MAXINT/-INF/+INF and push error

BASIC OPTIONS WITH SPECIFIED LENGTH:
q (q1): Boolean False/True, bit packed for successive q’s
Q (Q1): integer 0/1 bit packed for successive q’s
q[n]/Q[n]: (n=2..16): signed int (Q unsigned) with n bits, bit-packed…
b/B: signed int, 1 byte (char type in C) (B: unsigned)  (b=i1)
h/H: signed int, 2 byte (short type in C) (H: unsigned)  (h=i2)
l/L: signed int, 4 byte (long type in C) (L: unsigned)  (l=i4)
in/In: (n=1..16) signed int, n bytes (I: unsigned) (default: 8bytes)
f[n]: (n=2,4,8,16): float with 2/4/8/16 bytes (default: 4 bytes)
d: double (=f8) 
cn: a fixed string with n Bytes (n=1...MAX_INT) (n NOT written in the packed
string)
If a numerical value exceeds a “specified length”, or has not the expected
format, then pack will push an error, and insert zeroes in the packed
string).

BASIC OPTIONS WITH VARIABLE LENGTH:
N:  lua_Number (size-optimized packing, int / float / Boolean / strings /
Nil) 
(pack will insert 0xF.. error codes for functions, userdata…, and return
“error counter”, unpack will create errors if it meets such error codes)
z: a zero terminated string 
s[n]: a string preceded by its length coded as an unsigned integer with n
bytes (default: length uses N format)

NON-NESTED TABLES, ONLY INDEX TABLE PART, SPECIFIED TABLE LENGTH:
^ between counter option spec (e. g. 5^i2, or 100^q …) (counter NOT written
in the packed string – before counter a delimiter space/colon/semicolon is
required – a further repeat count is NOT allowed in this case)
pack will push error if table too small (larger table is allowed)
unpack will create table with exact size (including “n” hash)
pack( “N# #^N”, #tab, tab) is the same as pack( “|N”, tab), or as pack(
“t1”, tab)

NON-NESTED TABLES:
| as pre-char: only index part (e. g. |i2, or |q…), preceded by index part
length in N format
: as pre-char: only hash part (e. g. :i2, or :q…), keys as strings s, 
values use specified number command option, both preceded by hash part
length in N format
* as pre-char: index+hash (e. g. *i2 or *q…), index preceded by index length
in N format, then keys as strings s preceded by hash length in N format,
then hash-values again preceded by hash length in N format
t[n] (n=1..3) table using N option (1: index part, 2: hash part, 3
(=default) both parts) (t1=|N, t2=:N, t3=t=*N)
(if sub-tables are met (or functions or user data), pack will insert the
0xF…error codes in the packed string and push error, unpack will then push
error meeting them)

NESTED TABLES:
T[n] (n=1..3) nested table using N option for the elements, and table start/
end / hash start specifiers.
(if functions or users dat ais mat, pack will insert 0xF… error codes in the
packed string and push erorr, unpack will then push error meeting them)

REPEAT SPECIFIER # (or specific number): 
# post-char: specifies repeat count, e. g. N#, or I2#, or Q3#, …
# pre-char: uses this repeat count, e. g. #N, or #i2, or #q3, or #t, …
Any number 1..MAX_INT pre-char: repeat count, e. g. 100N, 250i2 or 50q3, or
4t…
Any repeat count must be preceded by a delimiter
(space/colon/semicolon/string start)
In case of the ^ option, the repeat specifier specifies the table counter.

ELEMENT LENGTH SPECIFIER _ (or specific number … see options above): 
_ pre-char: specifies element length, e. g. _B, or _Q3, …
_ post-char: uses this element length, e. g. i_, I_, q_, Q_, s_, c_, t_, T_,
Any length specifier must be preceded by a code option (i, c, s, q, t, T)

BACKGROUND OPTIONS (DELIMITERS AND ENDIANESS)
These options do NOT add to the packed string, only for format string.
>: use little endian (default)
<: use big endian
‘ ‘/’,’/’;’ (space, colon, semicolon): delimiters

If named metatables would be supported, then I would also support the
following codes for tables, which use metatable function access for
pack/unpack:
t[n]'typename'
^'typename'
|'typename'
:'typename'
*'typename'
... and the nested table version 'T' would include the metatable typename
info in the packed string. (see remark at the end of my last blog).





--
Sent from: http://lua.2524044.n2.nabble.com/Lua-l-f2524044.html