aboutsummaryrefslogtreecommitdiff
path: root/src/ltn12.lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/ltn12.lua')
-rw-r--r--src/ltn12.lua58
1 files changed, 29 insertions, 29 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua
index 6c5ea28..9917ce8 100644
--- a/src/ltn12.lua
+++ b/src/ltn12.lua
@@ -31,40 +31,40 @@ function filter.cycle(low, ctx, extra)
31 end 31 end
32end 32end
33 33
34-- chains a bunch of filters together 34local function chain2(f1, f2)
35-- by Wim Couwenberg 35 local ff1, ff2 = "", ""
36function filter.chain(...)
37 local current = 1
38 local bottom = 1
39 local top = table.getn(arg)
40 local retry = ""
41 return function(chunk) 36 return function(chunk)
42 if chunk ~= retry then 37 local rf1 = chunk and ""
43 current = bottom 38 local rf2 = ff1 and ""
44 retry = chunk and retry 39 -- if f2 still has pending data, get it and return it
40 if ff2 ~= rf2 then
41 ff2 = f2(rf2)
42 if ff2 ~= "" then return ff2 end
43 end
44 -- here we know f2 needs more data
45 -- we try to get it from f1
46 ff1 = f1(chunk)
47 while 1 do
48 -- if f1 can't produce data, we need more data from the user
49 if ff1 == "" then return "" end
50 -- otherwise we pass new data to f2 until it produces something
51 -- or f1 runs out of data too
52 ff2 = f2(ff1)
53 if ff2 ~= "" then return ff2 end
54 ff1 = f1(rf1)
45 end 55 end
46 repeat
47 if current == bottom then
48 chunk = arg[current](chunk)
49 if chunk == "" or bottom == top then return chunk
50 elseif chunk then current = current + 1
51 else
52 bottom = bottom + 1
53 current = bottom
54 end
55 else
56 chunk = arg[current](chunk or "")
57 if chunk == "" then
58 chunk = retry
59 current = current - 1
60 elseif current == top then return chunk
61 else current = current + 1
62 end
63 end
64 until false
65 end 56 end
66end 57end
67 58
59-- chains a bunch of filters together
60function filter.chain(...)
61 local f = arg[1]
62 for i = 2, table.getn(arg) do
63 f = chain2(f, arg[i])
64 end
65 return f
66end
67
68----------------------------------------------------------------------------- 68-----------------------------------------------------------------------------
69-- Source stuff 69-- Source stuff
70----------------------------------------------------------------------------- 70-----------------------------------------------------------------------------