-
Notifications
You must be signed in to change notification settings - Fork 0
/
listwords.lua
70 lines (61 loc) · 1.49 KB
/
listwords.lua
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
local prefix = 'bitdicts/'
local r = false
local bitwidth = 1
while arg[1] and arg[1]:sub(1, 1) == '-' do
if arg[1] == '-r' then
r = true
table.remove(arg, 1)
-- TODO: allow -x5 and -x6 for base 32 and base64
-- perhaps also -x3 for octal, and x2 for completion?
-- x1 discards all nonbinary characters
elseif arg[1] == '-x' then
bitwidth = 4
table.remove(arg, 1)
else
io.stderr:write(string.format('Unrecognized option %q\n', arg[1]))
os.exit(1)
end
end
local hexbits = {}
for i = 0, 16 do
hexbits[string.format('%x',i)] =
(i > 7 and '1' or '0') ..
(i % 8 > 3 and '1' or '0') ..
(i % 4 > 1 and '1' or '0') ..
(i % 2 > 0 and '1' or '0')
end
local bits = arg[1]:gsub('%X','')
-- TODO: skip this under -x6 (the only base with X as a digit)
if bits:match('^0[xX]') then
bits = bits:sub(3)
bitwidth = 4
end
if bitwidth == 4 then
bits = bits:lower():gsub('.', hexbits)
end
bits = bits:gsub('[^01]','')
local tips = {}
local function least(t)
local lk, lv = next(t)
local ck, cv = next(t, lk)
while ck do
if cv < lv then lk, lv = ck, cv end
ck, cv = next(t, ck)
end
return lk
end
local function readtip(file)
local tip = file:read()
tips[file] = tip
if not tip then file:close() end
end
for i = 1, #bits do
local file = io.open(prefix .. bits:sub(r and i or 1, r and -1 or i))
if file then readtip(file) end
end
local topfile = least(tips)
while topfile do
print(tips[topfile])
readtip(topfile)
topfile = least(tips)
end