forked from graphdat/plugin-redis
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.lua
More file actions
65 lines (57 loc) · 1.27 KB
/
tools.lua
File metadata and controls
65 lines (57 loc) · 1.27 KB
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
--
-- Module.
--
local tools = {}
-- Requires.
local string = require('string')
--
-- Limit a given number x between two boundaries.
-- Either min or max can be nil, to fence on one side only.
--
tools.fence = function(x, min, max)
return (min and x < min and min) or (max and x > max and max) or x
end
--
-- Encode data in Base64 format.
--
tools.base64 = function(data)
local _lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
return ((data:gsub('.', function(x)
local r, b = '', x:byte()
for i = 8, 1, -1 do
r = r .. (b % 2 ^ i - b % 2 ^ (i - 1) > 0 and '1' or '0')
end
return r
end) .. '0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
if #x < 6 then
return ''
end
local c = 0
for i = 1, 6 do
c = c + (x:sub(i, i) == '1' and 2 ^ (6 - i) or 0)
end
return _lookup:sub(c + 1, c + 1)
end) .. ({
'',
'==',
'='
})[#data % 3 + 1])
end
--
-- Split a string into a table
--
tools.split = function (inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={} ; local i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
end
return t
end
--
-- Export.
--
return tools