-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstring_ext.lua
49 lines (45 loc) · 1.23 KB
/
string_ext.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
function string:split(sep)
local sep, fields = sep or ":", {}
local pattern = string.format("([^%s]+)", sep)
self:gsub(pattern, function(c) fields[#fields+1] = c end)
return fields
end
function string.expand (s, ...)
local args = {...}
args = #args == 1 and type(args[1]) == "table" and args[1] or args
-- return true if there was an expansion
local function DoExpand (iscode)
local was = false
local mask = iscode and "()%$(%b{})" or "()%$([%a%d_]*)"
local drepl = iscode and "\\$" or "\\\\$"
s = s:gsub(mask, function (pos, code)
if s:sub(pos-1, pos-1) == "\\" then
return "$"..code
else
was = true
local v, err
if iscode then
code = code:sub(2, -2)
else
local n = tonumber(code)
if n then v = args[n] end
end
if not v then
v, err = load("return tostring("..code..")") if not v then error(err) end
v = v()
end
if v == nil then v = "" end
v = tostring(v):gsub("%$", drepl)
return v
end
end)
if not (iscode or was) then s = s:gsub("\\%$", "$") end
return was
end
repeat DoExpand(true); until not DoExpand(false)
return s
end
_G.expand = string.expand
function string:capitalize()
return self:sub(1,1):upper() .. self:sub(2)
end