diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/ltn12.lua | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index 9917ce8..ed39ec8 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
| @@ -31,6 +31,7 @@ function filter.cycle(low, ctx, extra) | |||
| 31 | end | 31 | end |
| 32 | end | 32 | end |
| 33 | 33 | ||
| 34 | --[[ | ||
| 34 | local function chain2(f1, f2) | 35 | local function chain2(f1, f2) |
| 35 | local ff1, ff2 = "", "" | 36 | local ff1, ff2 = "", "" |
| 36 | return function(chunk) | 37 | return function(chunk) |
| @@ -55,6 +56,29 @@ local function chain2(f1, f2) | |||
| 55 | end | 56 | end |
| 56 | end | 57 | end |
| 57 | end | 58 | end |
| 59 | ]] | ||
| 60 | |||
| 61 | local function chain2(f1, f2) | ||
| 62 | local co = coroutine.create(function(chunk) | ||
| 63 | while true do | ||
| 64 | local filtered1 = f1(chunk) | ||
| 65 | local filtered2 = f2(filtered1) | ||
| 66 | local done2 = filtered1 and "" | ||
| 67 | while true do | ||
| 68 | if filtered2 == "" or filtered2 == nil then break end | ||
| 69 | coroutine.yield(filtered2) | ||
| 70 | filtered2 = f2(done2) | ||
| 71 | end | ||
| 72 | if filtered1 == "" then chunk = coroutine.yield(filtered1) | ||
| 73 | elseif filtered1 == nil then return nil | ||
| 74 | else chunk = chunk and "" end | ||
| 75 | end | ||
| 76 | end) | ||
| 77 | return function(chunk) | ||
| 78 | local _, res = coroutine.resume(co, chunk) | ||
| 79 | return res | ||
| 80 | end | ||
| 81 | end | ||
| 58 | 82 | ||
| 59 | -- chains a bunch of filters together | 83 | -- chains a bunch of filters together |
| 60 | function filter.chain(...) | 84 | function filter.chain(...) |
