diff options
Diffstat (limited to 'tex/context/base/l-number.lua')
-rw-r--r-- | tex/context/base/l-number.lua | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/tex/context/base/l-number.lua b/tex/context/base/l-number.lua index b1ffb9ca9..a4dbe3bdf 100644 --- a/tex/context/base/l-number.lua +++ b/tex/context/base/l-number.lua @@ -8,7 +8,7 @@ if not modules then modules = { } end modules ['l-number'] = { -- this module will be replaced when we have the bit library -local tostring = tostring +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 @@ -72,11 +72,11 @@ function number.hasbit(x, p) -- typical call: if hasbit(x, bit(3)) then ... end function number.setbit(x, p) - return hasbit(x, p) and x or x + p + return (x % (p + p) >= p) and x or x + p end function number.clearbit(x, p) - return hasbit(x, p) and x - p or x + return (x % (p + p) >= p) and x - p or x end --~ function number.tobitstring(n) @@ -128,3 +128,7 @@ end --~ 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 |