iryont/lua-struct

Enhancement: 'c0' meaning for `pack`

EnTerr opened this issue · 0 comments

I find myself in need of a non-zero terminated string with variable length to be packed (it's part of the protocol i am dealing with), so i made a change to my copy to match the behavior of Ierusalimschy struct for 'c0' - here is my mod:

      --if length > 0 then
      --  local str = tostring(table.remove(vars, 1))
      --  if length - str:len() > 0 then
      --    str = str .. string.rep(' ', length - str:len())
      --  end
      --  table.insert(stream, str:sub(1, length))
      --end
      local str = tostring(table.remove(vars, 1))
      if length == 0 then
        length = str:len() -- 'c0' meaning use the given string length
      elseif length > str:len() then
        str = str .. string.rep(' ', length - str:len())
      end
      table.insert(stream, str:sub(1, length))

Submitting in case you find useful. I have not looked into adding 'c0' for unpack, since i haven't had need for it (yet?).