diff options
author | Philipp Gesang <megas.kapaneus@gmail.com> | 2012-10-19 18:35:14 +0200 |
---|---|---|
committer | Philipp Gesang <megas.kapaneus@gmail.com> | 2012-10-19 18:35:14 +0200 |
commit | e84a0c500a2104dc82b8278d7778144351aa71b3 (patch) | |
tree | 2cf9b21cca6c231445d854c9c239ee6d3b731c25 | |
parent | 9e40ecb0b88b556a55b7b5bf3b7c58b5a8368501 (diff) | |
download | lualibs-e84a0c500a2104dc82b8278d7778144351aa71b3.tar.gz |
update l-number l-boolean
-rw-r--r-- | lualibs-boolean.lua | 66 | ||||
-rw-r--r-- | lualibs-number.lua | 82 |
2 files changed, 120 insertions, 28 deletions
diff --git a/lualibs-boolean.lua b/lualibs-boolean.lua index be7ec7d..2b94de7 100644 --- a/lualibs-boolean.lua +++ b/lualibs-boolean.lua @@ -6,36 +6,60 @@ if not modules then modules = { } end modules ['l-boolean'] = { license = "see context related readme files" } -boolean = boolean or { } - local type, tonumber = type, tonumber +boolean = boolean or { } +local boolean = boolean + function boolean.tonumber(b) - if b then return 1 else return 0 end + if b then return 1 else return 0 end -- test and return or return end function toboolean(str,tolerant) - if tolerant then - local tstr = type(str) - if tstr == "string" then - return str == "true" or str == "yes" or str == "on" or str == "1" or str == "t" - elseif tstr == "number" then - return tonumber(str) ~= 0 - elseif tstr == "nil" then - return false - else - return str - end + if str == nil then + return false + elseif str == false then + return false + elseif str == true then + return true + elseif str == "true" then + return true + elseif str == "false" then + return false + elseif not tolerant then + return false + elseif str == 0 then + return false + elseif (tonumber(str) or 0) > 0 then + return true + else + return str == "yes" or str == "on" or str == "t" + end +end + +string.toboolean = toboolean + +function string.booleanstring(str) + if str == nil then + return false + elseif str == false then + return false + elseif str == true then + return true elseif str == "true" then return true elseif str == "false" then return false + elseif str == 0 then + return false + elseif (tonumber(str) or 0) > 0 then + return true else - return str + return str == "yes" or str == "on" or str == "t" end end -function string.is_boolean(str) +function string.is_boolean(str,default) if type(str) == "string" then if str == "true" or str == "yes" or str == "on" or str == "t" then return true @@ -43,13 +67,5 @@ function string.is_boolean(str) return false end end - return nil -end - -function boolean.alwaystrue() - return true -end - -function boolean.falsetrue() - return false + return default end diff --git a/lualibs-number.lua b/lualibs-number.lua index a1249f0..a4dbe3b 100644 --- a/lualibs-number.lua +++ b/lualibs-number.lua @@ -6,11 +6,15 @@ if not modules then modules = { } end modules ['l-number'] = { license = "see context related readme files" } -local tostring = tostring -local format, floor, insert, match = string.format, math.floor, table.insert, string.match +-- this module will be replaced when we have the bit library + +local tostring, tonumber = tostring, tonumber +local format, floor, match, rep = string.format, math.floor, string.match, string.rep +local concat, insert = table.concat, table.insert local lpegmatch = lpeg.match -number = number or { } +number = number or { } +local number = number -- a,b,c,d,e,f = number.toset(100101) @@ -56,3 +60,75 @@ function number.bits(n,zero) end return t end + +--~ http://ricilake.blogspot.com/2007/10/iterating-bits-in-lua.html + +function number.bit(p) + return 2 ^ (p - 1) -- 1-based indexing +end + +function number.hasbit(x, p) -- typical call: if hasbit(x, bit(3)) then ... + return x % (p + p) >= p +end + +function number.setbit(x, p) + return (x % (p + p) >= p) and x or x + p +end + +function number.clearbit(x, p) + return (x % (p + p) >= p) and x - p or x +end + +--~ function number.tobitstring(n) +--~ if n == 0 then +--~ return "0" +--~ else +--~ local t = { } +--~ while n > 0 do +--~ insert(t,1,n % 2 > 0 and 1 or 0) +--~ n = floor(n/2) +--~ end +--~ return concat(t) +--~ end +--~ end + +function number.tobitstring(n,m) + if n == 0 then + if m then + rep("00000000",m) + else + return "00000000" + end + else + local t = { } + while n > 0 do + insert(t,1,n % 2 > 0 and 1 or 0) + n = floor(n/2) + end + local nn = 8 - #t % 8 + if nn > 0 and nn < 8 then + for i=1,nn do + insert(t,1,0) + end + end + if m then + m = m * 8 - #t + if m > 0 then + insert(t,1,rep("0",m)) + end + end + return concat(t) + end +end + +--~ print(number.tobitstring(8)) +--~ print(number.tobitstring(14)) +--~ print(number.tobitstring(66)) +--~ print(number.tobitstring(0x00)) +--~ print(number.tobitstring(0xFF)) +--~ print(number.tobitstring(46260767936,8)) +--~ print(#number.tobitstring(46260767936,6)) + +function number.valid(str,default) + return tonumber(str) or default or nil +end |