summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit')
-rw-r--r--www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/bit.lua264
-rw-r--r--www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/hex.lua99
-rw-r--r--www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/readme.txt143
3 files changed, 506 insertions, 0 deletions
diff --git a/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/bit.lua b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/bit.lua
new file mode 100644
index 00000000..6bb3f487
--- /dev/null
+++ b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/bit.lua
@@ -0,0 +1,264 @@
+--[[---------------
+LuaBit v0.4
+-------------------
+a bitwise operation lib for lua.
+
+http://luaforge.net/projects/bit/
+
+How to use:
+-------------------
+ bit.bnot(n) -- bitwise not (~n)
+ bit.band(m, n) -- bitwise and (m & n)
+ bit.bor(m, n) -- bitwise or (m | n)
+ bit.bxor(m, n) -- bitwise xor (m ^ n)
+ bit.brshift(n, bits) -- right shift (n >> bits)
+ bit.blshift(n, bits) -- left shift (n << bits)
+ bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>)
+
+Please note that bit.brshift and bit.blshift only support number within
+32 bits.
+
+2 utility functions are provided too:
+ bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence)
+ -- high bits first
+ bit.tonumb(bit_tbl) -- convert a bit table into a number
+-------------------
+
+Under the MIT license.
+
+copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com)
+
+2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto
+--]]---------------
+
+do
+
+------------------------
+-- bit lib implementions
+
+local function check_int(n)
+ -- checking not float
+ if(n - math.floor(n) > 0) then
+ error("trying to use bitwise operation on non-integer!")
+ end
+end
+
+local function to_bits(n)
+ check_int(n)
+ if(n < 0) then
+ -- negative
+ return to_bits(bit.bnot(math.abs(n)) + 1)
+ end
+ -- to bits table
+ local tbl = {}
+ local cnt = 1
+ while (n > 0) do
+ local last = math.mod(n,2)
+ if(last == 1) then
+ tbl[cnt] = 1
+ else
+ tbl[cnt] = 0
+ end
+ n = (n-last)/2
+ cnt = cnt + 1
+ end
+
+ return tbl
+end
+
+local function tbl_to_number(tbl)
+ local n = table.getn(tbl)
+
+ local rslt = 0
+ local power = 1
+ for i = 1, n do
+ rslt = rslt + tbl[i]*power
+ power = power*2
+ end
+
+ return rslt
+end
+
+local function expand(tbl_m, tbl_n)
+ local big = {}
+ local small = {}
+ if(table.getn(tbl_m) > table.getn(tbl_n)) then
+ big = tbl_m
+ small = tbl_n
+ else
+ big = tbl_n
+ small = tbl_m
+ end
+ -- expand small
+ for i = table.getn(small) + 1, table.getn(big) do
+ small[i] = 0
+ end
+
+end
+
+local function bit_or(m, n)
+ local tbl_m = to_bits(m)
+ local tbl_n = to_bits(n)
+ expand(tbl_m, tbl_n)
+
+ local tbl = {}
+ local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
+ for i = 1, rslt do
+ if(tbl_m[i]== 0 and tbl_n[i] == 0) then
+ tbl[i] = 0
+ else
+ tbl[i] = 1
+ end
+ end
+
+ return tbl_to_number(tbl)
+end
+
+local function bit_and(m, n)
+ local tbl_m = to_bits(m)
+ local tbl_n = to_bits(n)
+ expand(tbl_m, tbl_n)
+
+ local tbl = {}
+ local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
+ for i = 1, rslt do
+ if(tbl_m[i]== 0 or tbl_n[i] == 0) then
+ tbl[i] = 0
+ else
+ tbl[i] = 1
+ end
+ end
+
+ return tbl_to_number(tbl)
+end
+
+local function bit_not(n)
+
+ local tbl = to_bits(n)
+ local size = math.max(table.getn(tbl), 32)
+ for i = 1, size do
+ if(tbl[i] == 1) then
+ tbl[i] = 0
+ else
+ tbl[i] = 1
+ end
+ end
+ return tbl_to_number(tbl)
+end
+
+local function bit_xor(m, n)
+ local tbl_m = to_bits(m)
+ local tbl_n = to_bits(n)
+ expand(tbl_m, tbl_n)
+
+ local tbl = {}
+ local rslt = math.max(table.getn(tbl_m), table.getn(tbl_n))
+ for i = 1, rslt do
+ if(tbl_m[i] ~= tbl_n[i]) then
+ tbl[i] = 1
+ else
+ tbl[i] = 0
+ end
+ end
+
+ --table.foreach(tbl, print)
+
+ return tbl_to_number(tbl)
+end
+
+local function bit_rshift(n, bits)
+ check_int(n)
+
+ local high_bit = 0
+ if(n < 0) then
+ -- negative
+ n = bit_not(math.abs(n)) + 1
+ high_bit = 2147483648 -- 0x80000000
+ end
+
+ for i=1, bits do
+ n = n/2
+ n = bit_or(math.floor(n), high_bit)
+ end
+ return math.floor(n)
+end
+
+-- logic rightshift assures zero filling shift
+local function bit_logic_rshift(n, bits)
+ check_int(n)
+ if(n < 0) then
+ -- negative
+ n = bit_not(math.abs(n)) + 1
+ end
+ for i=1, bits do
+ n = n/2
+ end
+ return math.floor(n)
+end
+
+local function bit_lshift(n, bits)
+ check_int(n)
+
+ if(n < 0) then
+ -- negative
+ n = bit_not(math.abs(n)) + 1
+ end
+
+ for i=1, bits do
+ n = n*2
+ end
+ return bit_and(n, 4294967295) -- 0xFFFFFFFF
+end
+
+local function bit_xor2(m, n)
+ local rhs = bit_or(bit_not(m), bit_not(n))
+ local lhs = bit_or(m, n)
+ local rslt = bit_and(lhs, rhs)
+ return rslt
+end
+
+--------------------
+-- bit lib interface
+
+local bit = {
+ -- bit operations
+ bnot = bit_not,
+ band = bit_and,
+ bor = bit_or,
+ bxor = bit_xor,
+ brshift = bit_rshift,
+ blshift = bit_lshift,
+ bxor2 = bit_xor2,
+ blogic_rshift = bit_logic_rshift,
+
+ -- utility func
+ tobits = to_bits,
+ tonumb = tbl_to_number,
+}
+
+return bit
+
+end
+
+--[[
+for i = 1, 100 do
+ for j = 1, 100 do
+ if(bit.bxor(i, j) ~= bit.bxor2(i, j)) then
+ error("bit.xor failed.")
+ end
+ end
+end
+--]]
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/hex.lua b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/hex.lua
new file mode 100644
index 00000000..ee6a69e3
--- /dev/null
+++ b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/hex.lua
@@ -0,0 +1,99 @@
+--[[---------------
+Hex v0.4
+-------------------
+Hex conversion lib for lua.
+
+How to use:
+ hex.to_hex(n) -- convert a number to a hex string
+ hex.to_dec(hex) -- convert a hex string(prefix with '0x' or '0X') to number
+
+Part of LuaBit(http://luaforge.net/projects/bit/).
+
+Under the MIT license.
+
+copyright(c) 2006~2007 hanzhao (abrash_han@hotmail.com)
+
+2013-02-20: Brad Jorsch: Fix to not try messing with globals, doesn't work in Scribunto
+--]]---------------
+
+local bit = require 'bit'
+
+do
+
+local function to_hex(n)
+ if(type(n) ~= "number") then
+ error("non-number type passed in.")
+ end
+
+ -- checking not float
+ if(n - math.floor(n) > 0) then
+ error("trying to apply bitwise operation on non-integer!")
+ end
+
+ if(n < 0) then
+ -- negative
+ n = bit.tobits(bit.bnot(math.abs(n)) + 1)
+ n = bit.tonumb(n)
+ end
+
+ hex_tbl = {'A', 'B', 'C', 'D', 'E', 'F'}
+ hex_str = ""
+
+ while(n ~= 0) do
+ last = math.mod(n, 16)
+ if(last < 10) then
+ hex_str = tostring(last) .. hex_str
+ else
+ hex_str = hex_tbl[last-10+1] .. hex_str
+ end
+ n = math.floor(n/16)
+ end
+ if(hex_str == "") then
+ hex_str = "0"
+ end
+ return "0x" .. hex_str
+end
+
+local function to_dec(hex)
+ if(type(hex) ~= "string") then
+ error("non-string type passed in.")
+ end
+
+ head = string.sub(hex, 1, 2)
+
+ if( head ~= "0x" and head ~= "0X") then
+ error("wrong hex format, should lead by 0x or 0X.")
+ end
+
+ v = tonumber(string.sub(hex, 3), 16)
+
+ return v;
+end
+
+--------------------
+-- hex lib interface
+local hex = {
+ to_dec = to_dec,
+ to_hex = to_hex,
+}
+
+return hex
+
+end
+
+--[[
+-- test
+d = 4341688
+h = to_hex(d)
+print(h)
+print(to_dec(h))
+
+
+for i = 1, 100000 do
+ h = hex.to_hex(i)
+ d = hex.to_dec(h)
+ if(d ~= i) then
+ error("failed " .. i .. ", " .. h)
+ end
+end
+--]]
diff --git a/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/readme.txt b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/readme.txt
new file mode 100644
index 00000000..db831723
--- /dev/null
+++ b/www/wiki/extensions/Scribunto/includes/engines/LuaCommon/lualib/luabit/readme.txt
@@ -0,0 +1,143 @@
+LuaBit
+------
+LuaBit is a bitwise operation lib completely written in Lua. It's
+written in the belief that Lua is self-contained.
+
+The supported operations are: not, and, or, xor, right shift, logic
+right shift and left shift.
+
+Several utilities are designed to leverage the power of bit operation:
+ 1. hex: a dec <-> hex number converter
+ 2. utf8: convert utf8 string to ucs2
+ 3. noki: convert nokia pc suite backuped SMS file to .txt
+
+Under the MIT license.
+
+Visit http://luaforge.net/projects/bit/ to get latest version.
+
+Status
+------
+Now LuaBit is in v0.4.
+Release date: Mar 18, 2007
+
+Content
+-------
+3 files are there for LuaBit:
+ 1) bit.lua
+ is the bitwise operation lib, all operations are implemented here.
+
+ 2) hex.lua
+ is a helper lib for ease of using hex numbers with bitwise
+ operation.
+
+ 3) noki.lua
+ a utility(based on bit and hex) to convert Nokia PC Suite backuped
+ SMS to a unicode .txt file, which is more accessible than the
+ original .nfb or .nfc file.
+
+ 4) utf8.lua
+ convert utf8 string to ucs2 string
+
+How to use
+----------
+Bit
+---
+Just require 'bit' in your project and the bit lib will be
+available:
+ bit.bnot(n) -- bitwise not (~n)
+ bit.band(m, n) -- bitwise and (m & n)
+ bit.bor(m, n) -- bitwise or (m | n)
+ bit.bxor(m, n) -- bitwise xor (m ^ n)
+ bit.brshift(n, bits) -- right shift (n >> bits)
+ bit.blshift(n, bits) -- left shift (n << bits)
+ bit.blogic_rshift(n, bits) -- logic right shift(zero fill >>>)
+
+Please note that bit.brshift and bit.blshift only support number within
+32 bits.
+
+2 utility functions are provided too:
+ bit.tobits(n) -- convert n into a bit table(which is a 1/0 sequence)
+ -- high bits first
+ bit.tonumb(bit_tbl) -- convert a bit table into a number
+
+Hex
+---
+For ease of using hex numbers, a utility hex lib is also included in
+LuaBit. You can require 'hex' to use them:
+ hex.to_hex(n) -- convert a number to a hex string
+ hex.to_dec(hex) -- convert a hex string(prefix with '0x' or '0X') to number
+
+With hex, you can write code like:
+ bit.band(258, hex.to_dec('0xFF'))
+to get the lower 8 bits of 258, that's 2.
+
+Noki
+----
+require 'noki', to save your sms to .txt file:
+ noki.save_sms('nokia.nfb', 'sms.txt')
+and you can view the output sms.txt in notepad or other editor which
+support unicode.
+
+Utf8
+----
+require 'utf8', to convert a utf8 string:
+ ucs2_string = utf8.utf_to_uni(utf8_string)
+
+History
+-------
+v0.4
+* utf8 to ucs2 converter(utf8.lua).
+* clean up for compatible with Lua5.1 and 5.0.
+* add 'How to use' section for bit.lua and hex.lua.
+
+v0.3
+* noki added as an application of bit.
+* typo correction.
+
+v0.2
+* add logic right shift(>>>) support: bit.blogic_rshift.
+* add 2 utility functions: bit.tobits and bit.tonumb.
+* update hex.to_hex(in hex.lua) to support negative number.
+
+v0.1
+LuaBit is written when I do my own game project(Fio at http://fio.edithis.info).
+When loading resources, I have to do some bit operation. And I do not
+like the embedded way of bit operation. So I decide to implement those
+ops in lua. And that's LuaBit. It's not as fast as the embedded one, but
+it works. And Lua is self-contained :-)
+
+To-Do List
+---------
+v0.1
+It'll be useful if LuaBit support those bitwise op like:
+ bit.band(258, '0xFF')
+ease to type and use. This will be supported in next release.
+
+v0.2
+I decide to delay this feature to later version for it'll mess up the
+interface of LuaBit.
+
+v0.3
+May more utility functions add to Noki - phonebook might be a nice candidate.
+
+v0.4
+There's no UCS2 -> UTF8 convertion now, this feature may add in next release
+or when the project need.
+
+Noki'll be be exluded from LuaBit in next release; I decide to let Noki grow
+into a powerful tool to support more Nokia PC Suite backup format(.nfb,
+.nfc and .nbu).
+
+Trial Noki demo at http://nokisms.googlepages.com/(in Chinese)
+
+Known issues
+------------
+LuaBit doesn't play very well with negative number. The return value of the
+bitwise operations might change to positive when applied on negative numbers
+though the bit sequence is correct. So if you want do some arithmetic with
+the result of bit operation, be careful.
+
+Feedback
+--------
+Please send your comments, bugs, patches or change request to
+hanzhao(abrash_han@hotmail.com).