-
Notifications
You must be signed in to change notification settings - Fork 11
/
core.lua
32 lines (27 loc) · 999 Bytes
/
core.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
Codex = {}
CodexHistory = {}
function Codex:strsplit(delimiter, data)
if not data then return nil end
local delimiter, fields = delimiter or ":", {}
local pattern = string.format("([^%s]+)", delimiter)
string.gsub(data, pattern, function(c) fields[table.getn(fields) + 1] = c end)
return unpack(fields)
end
local sanitizeCache = {}
function Codex:SanitizePattern(pattern)
if not sanitizeCache[pattern] then
local result = pattern
-- escape magic characters
result = gsub(result, "([%+%-%*%(%)%?%[%]%^])", "%%%1")
-- remove capture indexes
result = gsub(result, "%d%$","")
-- catch all characters
result = gsub(result, "(%%%a)","%(%1+%)")
-- convert all %s to .+
result = gsub(result, "%%s%+",".+")
-- set priority to number over strings
result = gsub(result, "%(.%+%)%(%%d%+%)","%(.-%)%(%%d%+%)")
sanitizeCache[pattern] = result
end
return sanitizeCache[pattern]
end