lua-users home
lua-l archive

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



Quoting Sean Conner <sean@conman.org>:

It was thus said that the Great tobias@justdreams.de once stated:

The main reason why I have a buffer module is not just for mutable network
buffers.  I do have a binary packer/parser that works on Buffer,Segments
and Lua strings alike, however, when you pass it a buffer it can do simple
writes which makes it really convenient:

  We have a difference of approach.  I tend to convert raw binary data into
native Lua data (usually a table of fields) as soon as possible, do all the
manipulations using straight Lua, then pack everything back up into raw
binary data.

  -spc

While t.Pack can do more it could assist with that approach:
Pack   = require( "t.Pack" )
Buffer = require( "t.Buffer" )
binary = "WhatEver is in the PayLOad..."
p = Pack( ">I6<i4c11d" )  -- "Sequence" Packer
t = p( binary)
print( #binary, Pack.getSize( p ), t[1], t[2], t[3], t[4] )
29 29 96105823225206 1763734117 s in the Pa 3.0343664813352e-86
-- or named ( those have to be single table key/val pairs to preserve order )
p1 = Pack( {a = ">I6"}, {b = "<i4"}, {c = "c11"}, {d = "d"} )
o1 = p1( binary )
print( o1 )    -- OrderedHashTable is provided in the library
t.OrderedHashTable: 0x55774a1f28c0
for k,v in pairs(o1) do print(k,v) end -- iterates in guaranteed ordered fashion
a       96105823225206
b       1763734117
c       s in the Pa
d       3.0343664813352e-86

I don't have yet a Constructor which allows to generate a Packed buffer/string
from an ordered table but it's somewhere on the roadmap ...

- Tobias