diff options
Diffstat (limited to '')
-rw-r--r-- | src/ltn12.lua | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index ed3449b..dac932b 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -34,8 +34,23 @@ end | |||
34 | local function chain2(f1, f2) | 34 | local function chain2(f1, f2) |
35 | if type(f1) ~= 'function' then error('invalid filter', 2) end | 35 | if type(f1) ~= 'function' then error('invalid filter', 2) end |
36 | if type(f2) ~= 'function' then error('invalid filter', 2) end | 36 | if type(f2) ~= 'function' then error('invalid filter', 2) end |
37 | local co = coroutine.create(function(chunk) | ||
38 | while true do | ||
39 | local filtered1 = f1(chunk) | ||
40 | local filtered2 = f2(filtered1) | ||
41 | local done2 = filtered1 and "" | ||
42 | while true do | ||
43 | if filtered2 == "" or filtered2 == nil then break end | ||
44 | coroutine.yield(filtered2) | ||
45 | filtered2 = f2(done2) | ||
46 | end | ||
47 | if filtered1 == "" then chunk = coroutine.yield(filtered1) | ||
48 | elseif filtered1 == nil then return nil | ||
49 | else chunk = chunk and "" end | ||
50 | end | ||
51 | end) | ||
37 | return function(chunk) | 52 | return function(chunk) |
38 | return f2(f1(chunk)) | 53 | return shift(coroutine.resume(co, chunk)) |
39 | end | 54 | end |
40 | end | 55 | end |
41 | 56 | ||