Module:Pack
Documentation for this module may be created at Module:Pack/doc
p = {};
function pack( var )
local result = '';
local t = type( var );
if t == 'table' then
for k, v in pairs( var ) do
result = result .. pack( k ) .. ',' .. pack( v ) .. ',';
end
result = "T" .. pack( result );
elseif t == 'string' then
result = result .. "S" .. var:gsub( "[,|]",
{[','] = '|.', ['|'] = '||'} );
else
result = result .. "N" .. tostring(var);
end
return result;
end
function unpack( var_str )
local result;
head = var_str:sub(1,1);
var_str = var_str:sub(2,-1);
if head == 'T' then
result = {};
var_str = unpack(var_str);
for k, v in var_str:gmatch( "([^,]+),([^,]+)," ) do
k = unpack( k );
v = unpack( v );
result[k] = v;
end
elseif head == 'N' then
result = tonumber( var_str );
else
result = var_str:gsub( "|[.|]",
{['|.'] = ',', ['||'] = "|"} );
end
return result
end
function p.pack_test( frame )
local A = mw.loadData( 'Module:Citation/CS1/Configuration' );
return mw.text.nowiki( pack(A) );
end
function traverse( T )
local a;
for k, v in pairs(T) do
a = v;
if type(a) == 'table' then
traverse(a);
end
end
end
function p.unpack_test( frame )
local A = mw.loadData( 'Module:Citation/CS1/Configuration' );
local clock1, clock2
clock1 = os.clock();
for k = 1,1000 do
traverse(A);
end
clock2 = os.clock();
local C = mw.loadData( 'Module:Citation/CS1/Configuration2' );
local clock3, clock4;
clock3 = os.clock();
for k = 1,1000 do
traverse(C);
end
clock4 = os.clock();
return clock4-clock3 .. " " .. clock2-clock1;
end
return p;