summaryrefslogtreecommitdiff
path: root/xmake/core/base/table.lua
diff options
context:
space:
mode:
authorOpportunityLiu <[email protected]>2019-07-01 14:19:05 +0800
committerOpportunityLiu <[email protected]>2019-07-01 14:19:05 +0800
commitf40152174188626df4b70ca1b9044a721fd3c6e8 (patch)
tree388f5b8e3acdcf1bc9253556fe762dc8b09a5d2a /xmake/core/base/table.lua
parent19cbd06f95dddacd1247d1dffab9fa29d719d02b (diff)
utf8 support
Diffstat (limited to 'xmake/core/base/table.lua')
-rw-r--r--xmake/core/base/table.lua25
1 files changed, 25 insertions, 0 deletions
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 43e8b6db9..9af47b5a8 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -186,6 +186,31 @@ function table.is_dictionary(dict)
return type(dict) == "table" and dict[1] == nil
end
+-- read data from iterator, push them to an array
+-- usage: table.to_array(ipairs("a", "b")) -> {{1,"a",n=2},{2,"b",n=2}},2
+-- usage: table.to_array(io.lines("file")) -> {"line 1","line 2", ... , "line n"},n
+function table.to_array(iterator, state, var)
+
+ assert(iterator)
+
+ local result = {}
+ local count = 0
+ while true do
+ local data = table.pack(iterator(state, var))
+ if data[1] == nil then break end
+ var = data[1]
+
+ if data.n == 1 then
+ table.insert(result, var)
+ else
+ table.insert(result, data)
+ end
+ count = count + 1
+ end
+
+ return result, count
+end
+
-- unwrap object if be only one
function table.unwrap(object)
if type(object) == "table" then