summaryrefslogtreecommitdiff
path: root/reading.lua
blob: 6c910e20762a1558a1e9daac0c99fd59b52b4c3f (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
local function trigram_to_value (a, b, c)
	return a * 25 + b * 5 + c
end


local function next_trigram (message, index)
	index = (index or 0) + 1
	local offset = (index - 1) * 3
	local a = message[offset + 1]
	if not a then
		return nil
	end
	return index, a, message[offset + 2], message[offset + 3]
end


local function next_value (message, index)
	local a, b, c
	index, a, b, c = next_trigram(message, index)
	if not index then
		return nil
	end
	return index, trigram_to_value(a, b, c)
end


local reading = {
	trigram_to_value = trigram_to_value,
	next_trigram = next_trigram,
	next_value = next_value,
}


function reading.trigrams (message, start)
	return next_trigram, message, start or 0
end


function reading.values (message, start)
	return next_value, message, start or 0
end


function reading.tvalues (message)
	local values = {}
	for _, value in reading.values(message) do
		table.insert(values, value)
	end
	return values
end


return reading