Skip to content

Commit

Permalink
add support for arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
megagrump committed Nov 4, 2018
1 parent c54ae77 commit 3a66f09
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.sublime-*
41 changes: 40 additions & 1 deletion BlobReader.moon
Expand Up @@ -3,7 +3,7 @@ ffi = require('ffi')
band = bit.band

local _native, _endian, _parseByteOrder
local _tags, _getTag, _taggedReaders, _unpackMap
local _tags, _getTag, _taggedReaders, _unpackMap, _arrayTypeMap

--- Parses binary data from memory.
class BlobReader
Expand Down Expand Up @@ -225,6 +225,25 @@ class BlobReader
assert(len < 2 ^ 32, "String too long")
ffi.string(ffi.cast('uint8_t*', @_data + start), len - 1)

--- Reads a sequential table of typed values.
--
-- Expects preceding `vu32` encoded array length information, as written by `BlobWriter:array`.
--
-- @tparam string valueType Type of the values in the array
--
-- Valid types are `s8`, `u8`, `s16`, `u16`, `s32`, `u32`, `vs32`, `vu32`, `s64`, `u64`, `f32`, `f64`,
-- `number`, `string`, `bool`, `cstring`, and `table`.
--
-- @tparam[opt] table result Table to put the values in
-- @treturn table A sequential table, starting at index 1
-- @see BlobWriter:array
array: (valueType, result = {}) =>
reader = _arrayTypeMap[valueType]
assert(reader, reader or "Invalid array type <#{valueType}")
length = @vu32!
result[#result + 1] = reader(@) for i = 1, length
result

--- Parses data into separate values according to a format string.
--
-- See `BlobWriter:pack` for a list of supported format specifiers.
Expand Down Expand Up @@ -366,6 +385,26 @@ _taggedReaders = {
=> false
}

with BlobReader
_arrayTypeMap =
s8: .s8
u8: .u8
s16: .s16
u16: .u16
s32: .s32
u32: .u32
s64: .s64
u64: .u64
vs32: .vs32
vu32: .vu32
f32: .f32
f64: .f64
number: .number
string: .string
cstring: .cstring
bool: .bool
table: .table

with BlobReader
_unpackMap =
b: .s8
Expand Down
44 changes: 43 additions & 1 deletion BlobWriter.moon
Expand Up @@ -3,7 +3,7 @@ ffi = require('ffi')
band, bnot, shr = bit.band, bit.bnot, bit.rshift

local _native, _byteOrder, _parseByteOrder
local _tags, _getTag, _taggedReaders, _taggedWriters, _packMap, _unpackMap
local _tags, _getTag, _taggedReaders, _taggedWriters, _packMap, _unpackMap, _arrayTypeMap

--- Writes binary data to memory.
class BlobWriter
Expand Down Expand Up @@ -214,6 +214,28 @@ class BlobWriter
_native.s32[0] = value
@vu32(_native.u32[0])

--- Writes a sequential table of values. All values must be of the same type.
--
-- @tparam string valueType Type of the values in the array
--
-- Valid types are `s8`, `u8`, `s16`, `u16`, `s32`, `u32`, `vs32`, `vu32`, `s64`, `u64`, `f32`, `f64`,
-- `number`, `string`, `bool`, `cstring`, and `table`.
--
-- Stores the array length as a `vu32` encoded value before the actual table values.
--
-- @tparam table array A sequential table of values of type `valueType`
--
-- Maximum allowed length is `2 ^ 32 - 1` values.
-- Behavior is undefined for table keys that are not sequential, or not starting at index 1.
--
-- @treturn BlobWriter self
array: (valueType, array) =>
writer = _arrayTypeMap[valueType]
assert(writer, writer or "Invalid array type <#{valueType}")
@vu32(#array)
writer(@, v) for v in *array
@

--- Writes data to the output buffer according to a format string.
--
-- @tparam string format Data format descriptor string.
Expand Down Expand Up @@ -393,6 +415,26 @@ _taggedWriters = {
=> @ -- false is stored as tag, write nothing
}

with BlobWriter
_arrayTypeMap =
s8: .s8
u8: .u8
s16: .s16
u16: .u16
s32: .s32
u32: .u32
s64: .s64
u64: .u64
vs32: .vs32
vu32: .vu32
f32: .f32
f64: .f64
number: .number
string: .string
cstring: .cstring
bool: .bool
table: .table

with BlobWriter
_packMap =
b: .s8
Expand Down
47 changes: 46 additions & 1 deletion doc/classes/BlobReader.html
Expand Up @@ -144,6 +144,10 @@ <h2><a href="#Functions">Functions</a></h2>
<td class="summary">Reads a zero-terminated string from the input data (up to 2 ^ 32 - 1 bytes).</td>
</tr>
<tr>
<td class="name" nowrap><a href="#array">array (valueType[, result])</a></td>
<td class="summary">Reads a sequential table of typed values.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#unpack">unpack (format)</a></td>
<td class="summary">Parses data into separate values according to a format string.</td>
</tr>
Expand Down Expand Up @@ -694,6 +698,47 @@ <h3>Returns:</h3>



</dd>
<dt>
<a name = "array"></a>
<strong>array (valueType[, result])</strong>
</dt>
<dd>
Reads a sequential table of typed values. </p>

<p> Expects preceding <a href="../classes/BlobReader.html#vu32">vu32</a> encoded array length information, as written by <a href="../classes/BlobWriter.html#array">BlobWriter:array</a>.


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">valueType</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#string">string</a></span>
Type of the values in the array</p>

<p> Valid types are <a href="../classes/BlobReader.html#s8">s8</a>, <a href="../classes/BlobReader.html#u8">u8</a>, <a href="../classes/BlobReader.html#s16">s16</a>, <a href="../classes/BlobReader.html#u16">u16</a>, <a href="../classes/BlobReader.html#s32">s32</a>, <a href="../classes/BlobReader.html#u32">u32</a>, <a href="../classes/BlobReader.html#vs32">vs32</a>, <a href="../classes/BlobReader.html#vu32">vu32</a>, <a href="../classes/BlobReader.html#s64">s64</a>, <a href="../classes/BlobReader.html#u64">u64</a>, <a href="../classes/BlobReader.html#f32">f32</a>, <a href="../classes/BlobReader.html#f64">f64</a>,
<a href="../classes/BlobReader.html#number">number</a>, <a href="../classes/BlobReader.html#string">string</a>, <a href="../classes/BlobReader.html#bool">bool</a>, <a href="../classes/BlobReader.html#cstring">cstring</a>, and <a href="../classes/BlobReader.html#table">table</a>.
</li>
<li><span class="parameter">result</span>
<span class="types"><a class="type" href="../classes/BlobReader.html#table">table</a></span>
Table to put the values in
(<em>optional</em>)
</li>
</ul>

<h3>Returns:</h3>
<ol>

<span class="types"><a class="type" href="../classes/BlobReader.html#table">table</a></span>
A sequential table, starting at index 1
</ol>


<h3>See also:</h3>
<ul>
<a href="../classes/BlobWriter.html#array">BlobWriter:array</a>
</ul>


</dd>
<dt>
<a name = "unpack"></a>
Expand Down Expand Up @@ -799,7 +844,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-11-04 06:33:44 </i>
<i style="float:right;">Last updated 2018-11-04 08:46:15 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
45 changes: 44 additions & 1 deletion doc/classes/BlobWriter.html
Expand Up @@ -140,6 +140,10 @@ <h2><a href="#Functions">Functions</a></h2>
<td class="summary">Writes a signed 32 bit integer value with varying length.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#array">array (valueType, array)</a></td>
<td class="summary">Writes a sequential table of values.</td>
</tr>
<tr>
<td class="name" nowrap><a href="#pack">pack (format, ...)</a></td>
<td class="summary">Writes data to the output buffer according to a format string.</td>
</tr>
Expand Down Expand Up @@ -788,6 +792,45 @@ <h3>See also:</h3>
</ul>


</dd>
<dt>
<a name = "array"></a>
<strong>array (valueType, array)</strong>
</dt>
<dd>
Writes a sequential table of values. All values must be of the same type.


<h3>Parameters:</h3>
<ul>
<li><span class="parameter">valueType</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#string">string</a></span>
Type of the values in the array</p>

<p> Valid types are <a href="../classes/BlobWriter.html#s8">s8</a>, <a href="../classes/BlobWriter.html#u8">u8</a>, <a href="../classes/BlobWriter.html#s16">s16</a>, <a href="../classes/BlobWriter.html#u16">u16</a>, <a href="../classes/BlobWriter.html#s32">s32</a>, <a href="../classes/BlobWriter.html#u32">u32</a>, <a href="../classes/BlobWriter.html#vs32">vs32</a>, <a href="../classes/BlobWriter.html#vu32">vu32</a>, <a href="../classes/BlobWriter.html#s64">s64</a>, <a href="../classes/BlobWriter.html#u64">u64</a>, <a href="../classes/BlobWriter.html#f32">f32</a>, <a href="../classes/BlobWriter.html#f64">f64</a>,
<a href="../classes/BlobWriter.html#number">number</a>, <a href="../classes/BlobWriter.html#string">string</a>, <a href="../classes/BlobWriter.html#bool">bool</a>, <a href="../classes/BlobWriter.html#cstring">cstring</a>, and <a href="../classes/BlobWriter.html#table">table</a>.</p>

<p> Stores the array length as a <a href="../classes/BlobWriter.html#vu32">vu32</a> encoded value before the actual table values.
</li>
<li><span class="parameter">array</span>
<span class="types"><a class="type" href="../classes/BlobWriter.html#table">table</a></span>
A sequential table of values of type <code>valueType</code></p>

<p> Maximum allowed length is <code>2 ^ 32 - 1</code> values.
Behavior is undefined for table keys that are not sequential, or not starting at index 1.
</li>
</ul>

<h3>Returns:</h3>
<ol>

<span class="types"><span class="type">BlobWriter</span></span>
self
</ol>




</dd>
<dt>
<a name = "pack"></a>
Expand Down Expand Up @@ -1000,7 +1043,7 @@ <h3>Returns:</h3>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-11-04 06:33:44 </i>
<i style="float:right;">Last updated 2018-11-04 08:46:15 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
2 changes: 1 addition & 1 deletion doc/index.html
Expand Up @@ -57,7 +57,7 @@ <h2>Classes</h2>
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.4.6</a></i>
<i style="float:right;">Last updated 2018-11-04 06:33:44 </i>
<i style="float:right;">Last updated 2018-11-04 08:46:15 </i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
Expand Down
35 changes: 34 additions & 1 deletion lua/BlobReader.lua
@@ -1,7 +1,7 @@
local ffi = require('ffi')
local band = bit.band
local _native, _endian, _parseByteOrder
local _tags, _getTag, _taggedReaders, _unpackMap
local _tags, _getTag, _taggedReaders, _unpackMap, _arrayTypeMap
local BlobReader
do
local _class_0
Expand Down Expand Up @@ -126,6 +126,18 @@ do
assert(len < 2 ^ 32, "String too long")
return ffi.string(ffi.cast('uint8_t*', self._data + start), len - 1)
end,
array = function(self, valueType, result)
if result == nil then
result = { }
end
local reader = _arrayTypeMap[valueType]
assert(reader, reader or "Invalid array type <" .. tostring(valueType))
local length = self:vu32()
for i = 1, length do
result[#result + 1] = reader(self)
end
return result
end,
unpack = function(self, format)
assert(type(format) == 'string', "Invalid format specifier")
local result, len = { }, nil
Expand Down Expand Up @@ -297,6 +309,27 @@ _taggedReaders = {
return false
end
}
do
_arrayTypeMap = {
s8 = BlobReader.s8,
u8 = BlobReader.u8,
s16 = BlobReader.s16,
u16 = BlobReader.u16,
s32 = BlobReader.s32,
u32 = BlobReader.u32,
s64 = BlobReader.s64,
u64 = BlobReader.u64,
vs32 = BlobReader.vs32,
vu32 = BlobReader.vu32,
f32 = BlobReader.f32,
f64 = BlobReader.f64,
number = BlobReader.number,
string = BlobReader.string,
cstring = BlobReader.cstring,
bool = BlobReader.bool,
table = BlobReader.table
}
end
do
_unpackMap = {
b = BlobReader.s8,
Expand Down
33 changes: 32 additions & 1 deletion lua/BlobWriter.lua
@@ -1,7 +1,7 @@
local ffi = require('ffi')
local band, bnot, shr = bit.band, bit.bnot, bit.rshift
local _native, _byteOrder, _parseByteOrder
local _tags, _getTag, _taggedReaders, _taggedWriters, _packMap, _unpackMap
local _tags, _getTag, _taggedReaders, _taggedWriters, _packMap, _unpackMap, _arrayTypeMap
local BlobWriter
do
local _class_0
Expand Down Expand Up @@ -115,6 +115,16 @@ do
_native.s32[0] = value
return self:vu32(_native.u32[0])
end,
array = function(self, valueType, array)
local writer = _arrayTypeMap[valueType]
assert(writer, writer or "Invalid array type <" .. tostring(valueType))
self:vu32(#array)
for _index_0 = 1, #array do
local v = array[_index_0]
writer(self, v)
end
return self
end,
pack = function(self, format, ...)
assert(type(format) == 'string', "Invalid format specifier")
local data, index, len = {
Expand Down Expand Up @@ -280,6 +290,27 @@ _taggedWriters = {
return self
end
}
do
_arrayTypeMap = {
s8 = BlobWriter.s8,
u8 = BlobWriter.u8,
s16 = BlobWriter.s16,
u16 = BlobWriter.u16,
s32 = BlobWriter.s32,
u32 = BlobWriter.u32,
s64 = BlobWriter.s64,
u64 = BlobWriter.u64,
vs32 = BlobWriter.vs32,
vu32 = BlobWriter.vu32,
f32 = BlobWriter.f32,
f64 = BlobWriter.f64,
number = BlobWriter.number,
string = BlobWriter.string,
cstring = BlobWriter.cstring,
bool = BlobWriter.bool,
table = BlobWriter.table
}
end
do
_packMap = {
b = BlobWriter.s8,
Expand Down

0 comments on commit 3a66f09

Please sign in to comment.