forked from Wiladams/LAPHLibs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutils.lua
More file actions
50 lines (41 loc) · 876 Bytes
/
stringutils.lua
File metadata and controls
50 lines (41 loc) · 876 Bytes
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
local function gsplit(s,sep)
return coroutine.wrap(function()
if s == '' or sep == '' then coroutine.yield(s) return end
local lasti = 1
for v,i in s:gmatch('(.-)'..sep..'()') do
coroutine.yield(v)
lasti = i
end
coroutine.yield(s:sub(lasti))
end)
end
local function iunpack(i,s,v1)
local function pass(...)
local v1 = i(s,v1)
if v1 == nil then return ... end
return v1, pass(...)
end
return pass()
end
local function split(s,sep)
return iunpack(gsplit(s,sep))
end
local function accumulate(t,i,s,v)
for v in i,s,v do
t[#t+1] = v
end
return t
end
local function tsplit(s,sep)
return accumulate({}, gsplit(s,sep))
end
local function trim(s)
local from = s:match"^%s*()"
return from > #s and "" or s:match(".*%S", from)
end
return {
tsplit = tsplit,
split = split,
gsplit = gsplit,
trim = trim,
}