l-number.lua /size: 5720 b    last modification: 2020-07-01 14:35
1
if
not
modules
then
modules
=
{
}
end
modules
[
'
l-number
'
]
=
{
2
version
=
1
.
001
,
3
comment
=
"
companion to luat-lib.mkiv
"
,
4
author
=
"
Hans Hagen, PRAGMA-ADE, Hasselt NL
"
,
5
copyright
=
"
PRAGMA ADE / ConTeXt Development Team
"
,
6
license
=
"
see context related readme files
"
7
}
8 9
-- this module will be replaced when we have the bit library .. the number based sets
10
-- might go away
11 12
local
tostring
,
tonumber
=
tostring
,
tonumber
13
local
format
,
floor
,
match
,
rep
=
string
.
format
,
math
.
floor
,
string
.
match
,
string
.
rep
14
local
concat
,
insert
=
table
.
concat
,
table
.
insert
15
local
lpegmatch
=
lpeg
.
match
16
local
floor
=
math
.
floor
17 18
number
=
number
or
{
}
19
local
number
=
number
20 21
-- begin obsolete code --
22 23
-- if bit32 then
24
--
25
-- local btest, bor = bit32.btest, bit32.bor
26
--
27
-- function number.bit(p)
28
-- return 2 ^ (p - 1) -- 1-based indexing
29
-- end
30
--
31
-- number.hasbit = btest
32
-- number.setbit = bor
33
--
34
-- function number.setbit(x,p) -- why not bor?
35
-- return btest(x,p) and x or x + p
36
-- end
37
--
38
-- function number.clearbit(x,p)
39
-- return btest(x,p) and x - p or x
40
-- end
41
--
42
-- else
43
--
44
-- -- http://ricilake.blogspot.com/2007/10/iterating-bits-in-lua.html
45
--
46
-- function number.bit(p)
47
-- return 2 ^ (p - 1) -- 1-based indexing
48
-- end
49
--
50
-- function number.hasbit(x, p) -- typical call: if hasbit(x, bit(3)) then ...
51
-- return x % (p + p) >= p
52
-- end
53
--
54
-- function number.setbit(x, p)
55
-- return (x % (p + p) >= p) and x or x + p
56
-- end
57
--
58
-- function number.clearbit(x, p)
59
-- return (x % (p + p) >= p) and x - p or x
60
-- end
61
--
62
-- end
63 64
-- end obsolete code --
65 66
-- print(number.tobitstring(8))
67
-- print(number.tobitstring(14))
68
-- print(number.tobitstring(66))
69
-- print(number.tobitstring(0x00))
70
-- print(number.tobitstring(0xFF))
71
-- print(number.tobitstring(46260767936,4))
72 73
if
bit32
then
74 75
local
bextract
=
bit32
.
extract
76 77
local
t
=
{
78
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
79
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
80
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
81
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
"
0
"
,
82
}
83 84
function
number
.
tobitstring
(
b
,
m
,
w
)
85
if
not
w
then
86
w
=
32
87
end
88
local
n
=
w
89
for
i
=
0
,
w
-1
do
90
local
v
=
bextract
(
b
,
i
)
91
local
k
=
w
-
i
92
if
v
=
=
1
then
93
n
=
k
94
t
[
k
]
=
"
1
"
95
else
96
t
[
k
]
=
"
0
"
97
end
98
end
99
if
w
then
100
return
concat
(
t
,
"
"
,
1
,
w
)
101
elseif
m
then
102
m
=
33
-
m
*
8
103
if
m
<
1
then
104
m
=
1
105
end
106
return
concat
(
t
,
"
"
,
1
,
m
)
107
elseif
n
<
8
then
108
return
concat
(
t
)
109
elseif
n
<
16
then
110
return
concat
(
t
,
"
"
,
9
)
111
elseif
n
<
24
then
112
return
concat
(
t
,
"
"
,
17
)
113
else
114
return
concat
(
t
,
"
"
,
25
)
115
end
116
end
117 118
else
119 120
function
number
.
tobitstring
(
n
,
m
)
121
if
n
>
0
then
122
local
t
=
{
}
123
while
n
>
0
do
124
insert
(
t
,
1
,
n
%
2
>
0
and
1
or
0
)
125
n
=
floor
(
n
/
2
)
126
end
127
local
nn
=
8
-
#
t
%
8
128
if
nn
>
0
and
nn
<
8
then
129
for
i
=
1
,
nn
do
130
insert
(
t
,
1
,
0
)
131
end
132
end
133
if
m
then
134
m
=
m
*
8
-
#
t
135
if
m
>
0
then
136
insert
(
t
,
1
,
rep
(
"
0
"
,
m
)
)
137
end
138
end
139
return
concat
(
t
)
140
elseif
m
then
141
rep
(
"
00000000
"
,
m
)
142
else
143
return
"
00000000
"
144
end
145
end
146 147
end
148 149
function
number
.
valid
(
str
,
default
)
150
return
tonumber
(
str
)
or
default
or
nil
151
end
152 153
function
number
.
toevenhex
(
n
)
154
local
s
=
format
(
"
%X
"
,
n
)
155
if
#
s
%
2
=
=
0
then
156
return
s
157
else
158
return
"
0
"
.
.
s
159
end
160
end
161 162
-- -- a,b,c,d,e,f = number.toset(100101)
163
-- --
164
-- -- function number.toset(n)
165
-- -- return match(tostring(n),"(.?)(.?)(.?)(.?)(.?)(.?)(.?)(.?)")
166
-- -- end
167
-- --
168
-- -- -- the lpeg way is slower on 8 digits, but faster on 4 digits, some 7.5%
169
-- -- -- on
170
-- --
171
-- -- for i=1,1000000 do
172
-- -- local a,b,c,d,e,f,g,h = number.toset(12345678)
173
-- -- local a,b,c,d = number.toset(1234)
174
-- -- local a,b,c = number.toset(123)
175
-- -- local a,b,c = number.toset("123")
176
-- -- end
177
--
178
-- local one = lpeg.C(1-lpeg.S('')/tonumber)^1
179
--
180
-- function number.toset(n)
181
-- return lpegmatch(one,tostring(n))
182
-- end
183
--
184
-- -- function number.bits(n,zero)
185
-- -- local t, i = { }, (zero and 0) or 1
186
-- -- while n > 0 do
187
-- -- local m = n % 2
188
-- -- if m > 0 then
189
-- -- insert(t,1,i)
190
-- -- end
191
-- -- n = floor(n/2)
192
-- -- i = i + 1
193
-- -- end
194
-- -- return t
195
-- -- end
196
-- --
197
-- -- -- a bit faster
198
--
199
-- local function bits(n,i,...)
200
-- if n > 0 then
201
-- local m = n % 2
202
-- local n = floor(n/2)
203
-- if m > 0 then
204
-- return bits(n, i+1, i, ...)
205
-- else
206
-- return bits(n, i+1, ...)
207
-- end
208
-- else
209
-- return ...
210
-- end
211
-- end
212
--
213
-- function number.bits(n)
214
-- return { bits(n,1) }
215
-- end
216 217
function
number
.
bytetodecimal
(
b
)
218
local
d
=
floor
(
b
*
100
/
255
+
0
.
5
)
219
if
d
>
100
then
220
return
100
221
elseif
d
<
-100
then
222
return
-100
223
else
224
return
d
225
end
226
end
227 228
function
number
.
decimaltobyte
(
d
)
229
local
b
=
floor
(
d
*
255
/
100
+
0
.
5
)
230
if
b
>
255
then
231
return
255
232
elseif
b
<
-255
then
233
return
-255
234
else
235
return
b
236
end
237
end
238 239
function
number
.
idiv
(
i
,
d
)
240
return
floor
(
i
/
d
)
-- i//d in 5.3
241
end
242