summaryrefslogtreecommitdiff
path: root/lualibs-number.lua
blob: a4dbe3bdf14e926ef3f149cc16e8aef7b84bfac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
if not modules then modules = { } end modules ['l-number'] = {
    version   = 1.001,
    comment   = "companion to luat-lib.mkiv",
    author    = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
    copyright = "PRAGMA ADE / ConTeXt Development Team",
    license   = "see context related readme files"
}

-- 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 { }
local number = number

-- a,b,c,d,e,f = number.toset(100101)

function number.toset(n)
    return match(tostring(n),"(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)")
end

function number.toevenhex(n)
    local s = format("%X",n)
    if #s % 2 == 0 then
        return s
    else
        return "0" .. s
    end
end

-- the lpeg way is slower on 8 digits, but faster on 4 digits, some 7.5%
-- on
--
-- for i=1,1000000 do
--     local a,b,c,d,e,f,g,h = number.toset(12345678)
--     local a,b,c,d         = number.toset(1234)
--     local a,b,c           = number.toset(123)
-- end
--
-- of course dedicated "(.)(.)(.)(.)" matches are even faster

local one = lpeg.C(1-lpeg.S(''))^1

function number.toset(n)
    return lpegmatch(one,tostring(n))
end

function number.bits(n,zero)
    local t, i = { }, (zero and 0) or 1
    while n > 0 do
        local m = n % 2
        if m > 0 then
            insert(t,1,i)
        end
        n = floor(n/2)
        i = i + 1
    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