forked from blitmap/lua-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string-gmatch.lua
executable file
·57 lines (41 loc) · 928 Bytes
/
string-gmatch.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
50
51
52
53
54
55
56
57
#!/usr/bin/env lua
local cwrap = coroutine.wrap
local cyield = coroutine.yield
local orig_sgmatch = string.gmatch
string.gmatch =
function (s, m, i, j)
local f = orig_sgmatch(s, m)
if i == nil then
return f
end
if j == nil then
-- we assume not-a-number means `i' is callable
if type(i) ~= 'number' then
while true do
local ret = { f() }
if not next(ret) then
break
end
i(unpack(ret))
end
return -- Ash used an Escape Rope!
end
j = math.huge
end
for _ = 1, i - 1 do
f()
end
return
cwrap(
function ()
for x = 1, j - i do
cyield(f())
end
end
)
end
string.gmatch('blahness', '.', print)
print(string.rep('-', 20))
for m in string.gmatch('abcdefghijklmnopqrstuvwxyz', '.', 1) do print(m) end
print(string.rep('-', 20))
for m in string.gmatch('abcdefghijklmnopqrstuvwxyz', '.', 10, 20) do print(m:upper()) end