diff options
-rw-r--r-- | doc/ltn12.html | 10 | ||||
-rw-r--r-- | doc/reference.html | 3 | ||||
-rw-r--r-- | src/ltn12.lua | 10 | ||||
-rw-r--r-- | test/ltn12test.lua | 9 |
4 files changed, 31 insertions, 1 deletions
diff --git a/doc/ltn12.html b/doc/ltn12.html index 54e66fb..ee2502f 100644 --- a/doc/ltn12.html +++ b/doc/ltn12.html | |||
@@ -405,6 +405,16 @@ Creates and returns a source that produces the contents of a | |||
405 | <tt>string</tt>, chunk by chunk. | 405 | <tt>string</tt>, chunk by chunk. |
406 | </p> | 406 | </p> |
407 | 407 | ||
408 | <!-- table +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | ||
409 | |||
410 | <p class=name id="source.table"> | ||
411 | ltn12.source.<b>table(</b>table<b>)</b> | ||
412 | </p> | ||
413 | |||
414 | <p class=description> | ||
415 | Creates and returns a source that produces the numerically-indexed values of a <tt>table</tt> successively beginning at 1. The source returns nil (end-of-stream) whenever a nil value is produced by the current index, which proceeds forward regardless. | ||
416 | </p> | ||
417 | |||
408 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> | 418 | <!-- footer +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ --> |
409 | 419 | ||
410 | <div class=footer> | 420 | <div class=footer> |
diff --git a/doc/reference.html b/doc/reference.html index 287dc19..163a8a2 100644 --- a/doc/reference.html +++ b/doc/reference.html | |||
@@ -99,7 +99,8 @@ Support, Manual"> | |||
99 | <a href="ltn12.html#source.error">error</a>, | 99 | <a href="ltn12.html#source.error">error</a>, |
100 | <a href="ltn12.html#source.file">file</a>, | 100 | <a href="ltn12.html#source.file">file</a>, |
101 | <a href="ltn12.html#source.simplify">simplify</a>, | 101 | <a href="ltn12.html#source.simplify">simplify</a>, |
102 | <a href="ltn12.html#source.string">string</a>. | 102 | <a href="ltn12.html#source.string">string</a>, |
103 | <a href="ltn12.html#source.table">table</a>. | ||
103 | </blockquote> | 104 | </blockquote> |
104 | </blockquote> | 105 | </blockquote> |
105 | 106 | ||
diff --git a/src/ltn12.lua b/src/ltn12.lua index 575c5a7..afa735d 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -128,6 +128,16 @@ function source.string(s) | |||
128 | else return source.empty() end | 128 | else return source.empty() end |
129 | end | 129 | end |
130 | 130 | ||
131 | -- creates table source | ||
132 | function source.table(t) | ||
133 | base.assert('table' == type(t)) | ||
134 | local i = 0 | ||
135 | return function() | ||
136 | i = i + 1 | ||
137 | return t[i] | ||
138 | end | ||
139 | end | ||
140 | |||
131 | -- creates rewindable source | 141 | -- creates rewindable source |
132 | function source.rewind(src) | 142 | function source.rewind(src) |
133 | base.assert(src) | 143 | base.assert(src) |
diff --git a/test/ltn12test.lua b/test/ltn12test.lua index e3f85fb..e7d368d 100644 --- a/test/ltn12test.lua +++ b/test/ltn12test.lua | |||
@@ -181,6 +181,15 @@ assert(table.concat(t) == s, "mismatch") | |||
181 | print("ok") | 181 | print("ok") |
182 | 182 | ||
183 | -------------------------------- | 183 | -------------------------------- |
184 | io.write("testing source.table: ") | ||
185 | local inp = {'a','b','c','d','e'} | ||
186 | local source = ltn12.source.table(inp) | ||
187 | sink, t = ltn12.sink.table() | ||
188 | assert(ltn12.pump.all(source, sink), "returned error") | ||
189 | for i = 1, #inp do assert(t[i] == inp[i], "mismatch") end | ||
190 | print("ok") | ||
191 | |||
192 | -------------------------------- | ||
184 | io.write("testing source.chain (with split): ") | 193 | io.write("testing source.chain (with split): ") |
185 | source = ltn12.source.string(s) | 194 | source = ltn12.source.string(s) |
186 | filter = split(5) | 195 | filter = split(5) |