aboutsummaryrefslogtreecommitdiff
path: root/src/ltn12.lua
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2005-10-07 04:40:59 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2005-10-07 04:40:59 +0000
commitf4dadea763c1959a27dead24df3ee6c54c209842 (patch)
treec13b294a8ca5438d59b60e3f5a25a4f7c1fc9a1b /src/ltn12.lua
parent562d8cceb704a96a7b2f9acc4bc229ab9f5c6541 (diff)
downloadluasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.gz
luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.tar.bz2
luasocket-f4dadea763c1959a27dead24df3ee6c54c209842.zip
Before compiling on Windows.
Diffstat (limited to 'src/ltn12.lua')
-rw-r--r--src/ltn12.lua52
1 files changed, 34 insertions, 18 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua
index 733422d..2c16253 100644
--- a/src/ltn12.lua
+++ b/src/ltn12.lua
@@ -134,33 +134,49 @@ function source.rewind(src)
134 end 134 end
135end 135end
136 136
137-- chains a source with a filter
138function source.chain(src, f) 137function source.chain(src, f)
139 base.assert(src and f) 138 base.assert(src and f)
140 local last_in, last_out = "", "" 139 local last_in, last_out = "", ""
140 local state = "feeding"
141 local err
141 return function() 142 return function()
142 if last_out == "" then 143 if not last_out then
143 while true do 144 base.error('source is empty!', 2)
144 local err 145 end
146 while true do
147 if state == "feeding" then
145 last_in, err = src() 148 last_in, err = src()
146 if err then return nil, err end 149 if err then return nil, err end
147 last_out = f(last_in) 150 last_out = f(last_in)
148 if last_out ~= "" then return last_out end 151 if not last_out then
149 if not last_in then 152 if last_in then
150 base.error('filter returned inappropriate ""') 153 base.error('filter returned inappropriate nil')
154 else
155 return nil
156 end
157 elseif last_out ~= "" then
158 state = "eating"
159 if last_in then last_in = "" end
160 return last_out
161 end
162 else
163 last_out = f(last_in)
164 if last_out == "" then
165 if last_in == "" then
166 state = "feeding"
167 else
168 base.error('filter returned ""')
169 end
170 elseif not last_out then
171 if last_in then
172 base.error('filter returned inappropriate nil')
173 else
174 return nil
175 end
176 else
177 return last_out
151 end 178 end
152 end 179 end
153 elseif last_out then
154 last_out = f(last_in and "")
155 if last_in and not last_out then
156 base.error('filter returned inappropriate nil')
157 end
158 if last_out == "" and not last_in then
159 base.error(base.tostring(f) .. ' returned inappropriate ""')
160 end
161 return last_out
162 else
163 base.error("source is empty", 2)
164 end 180 end
165 end 181 end
166end 182end