separate lua 5.1 functions into number-based and table-based

This commit is contained in:
kikito 2015-04-06 18:13:47 +02:00
parent 77ac109e66
commit 65d03bceef
1 changed files with 35 additions and 28 deletions

63
md5.lua
View File

@ -75,24 +75,27 @@ else
end end
end end
local bits_not, bits_or, bits_and, bits_not, bits_xor
local to_bits -- needs to be declared before bit_not local to_bits -- needs to be declared before bit_not
function bit_not(n) bits_not = function(tbl)
local tbl = to_bits(n) for i=1, math.max(#tbl, 32) do
local size = math.max(#tbl, 32) if tbl[i] == 1 then
for i = 1, size do
if(tbl[i] == 1) then
tbl[i] = 0 tbl[i] = 0
else else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl2number(tbl) return tbl
end
bit_not = function(n)
return tbl2number(bits_not(to_bits(n)))
end end
-- defined as local above -- defined as local above
to_bits = function (n) to_bits = function (n)
if(n < 0) then if n < 0 then
-- negative -- negative
return to_bits(bit_not(math.abs(n)) + 1) return to_bits(bit_not(math.abs(n)) + 1)
end end
@ -110,61 +113,66 @@ else
return tbl return tbl
end end
function bit_or(m, n) bits_or = function(tbl_m, tbl_n)
local tbl_m = to_bits(m)
local tbl_n = to_bits(n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
for i = 1, #tbl_m do for i = 1, #tbl_m do
if(tbl_m[i]== 0 and tbl_n[i] == 0) then if(tbl_m[i] == 0 and tbl_n[i] == 0) then
tbl[i] = 0 tbl[i] = 0
else else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl2number(tbl) return tbl
end end
function bit_and(m, n) bit_or = function(m, n)
local tbl_m = to_bits(m) return tbl2number(bits_or(tobits(m), tobits(n)))
local tbl_n = to_bits(n) end
bits_and = function(tbl_m, tbl_n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
for i = 1, #tbl_m do for i = 1, #tbl_m do
if(tbl_m[i]== 0 or tbl_n[i] == 0) then if tbl_m[i] == 0 or tbl_n[i] == 0 then
tbl[i] = 0 tbl[i] = 0
else else
tbl[i] = 1 tbl[i] = 1
end end
end end
return tbl2number(tbl) return tbl
end end
function bit_xor(m, n) bit_and = function(m, n)
local tbl_m = to_bits(m) return tbl2number(bits_and(tobits(m), tobits(n)))
local tbl_n = to_bits(n) end
bits_xor = function(tbl_m, tbl_n)
expand(tbl_m, tbl_n) expand(tbl_m, tbl_n)
local tbl = {} local tbl = {}
for i = 1, #tbl_m do for i = 1, #tbl_m do
if(tbl_m[i] ~= tbl_n[i]) then if tbl_m[i] ~= tbl_n[i] then
tbl[i] = 1 tbl[i] = 1
else else
tbl[i] = 0 tbl[i] = 0
end end
end end
return tbl2number(tbl) return tbl
end end
function bit_rshift(n, bits) bit_xor = function(m, n)
return tbl2number(bits_xor(tobits(m), tobits(n)))
end
bit_rshift = function(n, bits)
local high_bit = 0 local high_bit = 0
if(n < 0) then if n < 0 then
-- negative
n = bit_not(math.abs(n)) + 1 n = bit_not(math.abs(n)) + 1
high_bit = 2147483648 -- 0x80000000 high_bit = 2147483648 -- 0x80000000
end end
@ -178,9 +186,8 @@ else
return floor(n) return floor(n)
end end
function bit_lshift(n, bits) bit_lshift = function(n, bits)
if(n < 0) then if n < 0 then
-- negative
n = bit_not(math.abs(n)) + 1 n = bit_not(math.abs(n)) + 1
end end