Commit 0a3b89ab authored by François Perrad's avatar François Perrad
Browse files

export unpack_cursor

parent 1b9d2b4e
Revision history for lua-MessagePack
0.5.0
export unpack_cursor instead of unpackers
0.4.0 Thu Dec 8 10:30:00 2016
refactor without types_map indirection
......
......@@ -520,7 +520,7 @@ end
local unpackers -- forward declaration
local function _unpack (c)
local function unpack_cursor (c)
local s, i, j = c.s, c.i, c.j
if i > j then
c:underflow(i)
......@@ -530,6 +530,7 @@ local function _unpack (c)
c.i = i+1
return unpackers[val](c, val)
end
m.unpack_cursor = unpack_cursor
local function unpack_str (c, n)
local s, i, j = c.s, c.i, c.j
......@@ -546,7 +547,7 @@ end
local function unpack_array (c, n)
local t = {}
for i = 1, n do
t[i] = _unpack(c)
t[i] = unpack_cursor(c)
end
return t
end
......@@ -554,8 +555,8 @@ end
local function unpack_map (c, n)
local t = {}
for i = 1, n do
local k = _unpack(c)
local val = _unpack(c)
local k = unpack_cursor(c)
local val = unpack_cursor(c)
if k == nil or k ~= k then
k = m.sentinel
end
......@@ -836,7 +837,7 @@ end
function m.unpack (s)
checktype('unpack', 1, s, 'string')
local cursor = cursor_string(s)
local data = _unpack(cursor)
local data = unpack_cursor(cursor)
if cursor.i < cursor.j then
error "extra bytes"
end
......@@ -848,7 +849,7 @@ function m.unpacker (src)
local cursor = cursor_string(src)
return function ()
if cursor.i <= cursor.j then
return cursor.i, _unpack(cursor)
return cursor.i, unpack_cursor(cursor)
end
end
elseif type(src) == 'function' then
......@@ -858,7 +859,7 @@ function m.unpacker (src)
pcall(cursor.underflow, cursor, cursor.i)
end
if cursor.i <= cursor.j then
return true, _unpack(cursor)
return true, unpack_cursor(cursor)
end
end
else
......
......@@ -337,7 +337,7 @@ end
local unpackers -- forward declaration
local function _unpack (c)
local function unpack_cursor (c)
local s, i, j = c.s, c.i, c.j
if i > j then
c:underflow(i)
......@@ -347,6 +347,7 @@ local function _unpack (c)
c.i = i+1
return unpackers[val](c, val)
end
m.unpack_cursor = unpack_cursor
local function unpack_str (c, n)
local s, i, j = c.s, c.i, c.j
......@@ -363,7 +364,7 @@ end
local function unpack_array (c, n)
local t = {}
for i = 1, n do
t[i] = _unpack(c)
t[i] = unpack_cursor(c)
end
return t
end
......@@ -371,8 +372,8 @@ end
local function unpack_map (c, n)
local t = {}
for i = 1, n do
local k = _unpack(c)
local val = _unpack(c)
local k = unpack_cursor(c)
local val = unpack_cursor(c)
if k == nil or k ~= k then
k = m.sentinel
end
......@@ -587,7 +588,7 @@ end
function m.unpack (s)
checktype('unpack', 1, s, 'string')
local cursor = cursor_string(s)
local data = _unpack(cursor)
local data = unpack_cursor(cursor)
if cursor.i < cursor.j then
error "extra bytes"
end
......@@ -599,7 +600,7 @@ function m.unpacker (src)
local cursor = cursor_string(src)
return function ()
if cursor.i <= cursor.j then
return cursor.i, _unpack(cursor)
return cursor.i, unpack_cursor(cursor)
end
end
elseif type(src) == 'function' then
......@@ -609,7 +610,7 @@ function m.unpacker (src)
pcall(cursor.underflow, cursor, cursor.i)
end
if cursor.i <= cursor.j then
return true, _unpack(cursor)
return true, unpack_cursor(cursor)
end
end
else
......
......@@ -2,7 +2,7 @@
require 'Test.More'
plan(7)
plan(8)
if not require_ok 'MessagePack' then
BAIL_OUT "no lib"
......@@ -15,6 +15,7 @@ like( m._DESCRIPTION, 'MessagePack', "_DESCRIPTION" )
like( m._VERSION, '^%d%.%d%.%d$', "_VERSION" )
type_ok( m.packers, 'table', "table packers" )
type_ok( m.unpack_cursor, 'function', "function unpack_cursor" )
type_ok( m.build_ext, 'function', "function build_ext" )
if m.full64bits then
......
#! /usr/bin/lua
require 'Test.More'
plan(1)
local mp = require 'MessagePack'
local function my_cursor_string (str, i, j)
return {
s = str,
i = i or 1,
j = j or #str,
underflow = function ()
error "missing bytes"
end,
}
end
local function my_unpack (s, i, j)
local cursor = my_cursor_string(s, i, j)
local data = mp.unpack_cursor(cursor)
if cursor.i < cursor.j then
error "extra bytes"
end
return data
end
local mpac = mp.pack({1, 2, 3})
local s = 'BAR' .. mpac .. 'BAZ'
eq_array( my_unpack(s, 4, #s - 3), {1, 2, 3}, "my_unpack" )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment