-
Notifications
You must be signed in to change notification settings - Fork 0
/
strings.lua
47 lines (40 loc) · 1.28 KB
/
strings.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
local types = import("types.lua")
----------------------
-- String Utilities --
----------------------
-- Converts some value to a string. Additional arguments are forwarded if the value happens to be a function. If the
-- value is a function and it returns something other than a string and err is not nil, err is called with the return
-- value as an argument.
local function evaluate(value, err, ...)
if DEBUG then types.force({"function?"}, err) end
local value_type = type(value)
if value_type == "string" then return value
elseif value_type == "function" then
value = value(...)
if type(value) ~= "string" then
if err then err(value) end
return evaluate(value)
end
return value
else return tostring(value) end
end
-- Splits a string and returns a table. Based on https://gist.github.com/jaredallard/ddb152179831dd23b230.
local function split(str, delimiter)
local result = {}
local from = 1
local delim_from, delim_to = string.find(str, delimiter, from)
while delim_from do
table.insert( result, string.sub(str, from , delim_from-1))
from = delim_to + 1
delim_from, delim_to = string.find(str, delimiter, from)
end
table.insert(result, string.sub(str, from))
return result
end
-------------
-- Exports --
-------------
return {
evaluate = evaluate,
split = split
}