From 453cae7362ab31be5b1b50a15e5aa5fd2c5fd654 Mon Sep 17 00:00:00 2001 From: Jason Perkins Date: Wed, 20 Feb 2013 09:45:35 -0500 Subject: [PATCH] Add table.foreachi(), phase out ipairs() in API so nil values can be included in lists --- src/base/api.lua | 26 ++++++++++---------- src/base/table.lua | 59 +++++++++++++++++++++++++++++++++------------- 2 files changed, 56 insertions(+), 29 deletions(-) diff --git a/src/base/api.lua b/src/base/api.lua index f36bf0fe..030a68e7 100644 --- a/src/base/api.lua +++ b/src/base/api.lua @@ -140,9 +140,9 @@ function recurse(value) if type(value) == "table" then - for _, v in ipairs(value) do + table.foreachi(value, function(v) recurse(v) - end + end) else remover(removes, value) end @@ -182,7 +182,9 @@ if type(field.allowed) == "function" then return field.allowed(value) else - for _,v in ipairs(field.allowed) do + local n = #field.allowed + for i = 1, n do + local v = field.allowed[i] if value:lower() == v:lower() then return v end @@ -341,10 +343,10 @@ function api.setfile(target, name, field, value) if value:find("*") then local values = os.matchfiles(value) - for _, value in ipairs(values) do - api.setfile(target, name, field, value) + table.foreachi(values, function(v) + api.setfile(target, name, field, v) name = name + 1 - end + end) else target[name] = path.getabsolute(value) end @@ -353,10 +355,10 @@ function api.setdirectory(target, name, field, value) if value:find("*") then local values = os.matchdirs(value) - for _, value in ipairs(values) do - api.setdirectory(target, name, field, value) + table.foreachi(values, function(v) + api.setdirectory(target, name, field, values[i]) name = name + 1 - end + end) else target[name] = path.getabsolute(value) end @@ -411,9 +413,9 @@ local result = {} function recurse(value) if type(value) == "table" then - for _, v in ipairs(value) do - recurse(v) - end + table.foreachi(value, function (value) + recurse(value) + end) else setter(result, #result + 1, field, value) end diff --git a/src/base/table.lua b/src/base/table.lua index 849a787b..9bea0932 100644 --- a/src/base/table.lua +++ b/src/base/table.lua @@ -3,7 +3,7 @@ -- Additions to Lua's built-in table functions. -- Copyright (c) 2002-2008 Jason Perkins and the Premake project -- - + -- -- Returns true if the table contains the specified value. @@ -17,7 +17,7 @@ end return false end - + -- -- Make a copy of the indexed elements of the table. @@ -39,23 +39,23 @@ function table.deepcopy(object) -- keep track of already seen objects to avoid loops local seen = {} - + local function copy(object) if type(object) ~= "table" then return object elseif seen[object] then return seen[object] end - + local clone = {} seen[object] = clone for key, value in pairs(object) do clone[key] = copy(value) end - + return clone end - + return copy(object) end @@ -72,8 +72,8 @@ end return result end - - + + -- -- Flattens a hierarchy of tables into a single array containing all @@ -81,23 +81,48 @@ -- function table.flatten(arr) - local result = { } - + local result = {} + local function flatten(arr) - for _, v in ipairs(arr) do + local n = #arr + for i = 1, n do + local v = arr[i] if type(v) == "table" then flatten(v) - else + elseif v then table.insert(result, v) end end end - + flatten(arr) return result end +-- +-- Walk the elements of an array and call the specified function +-- for each non-nil element. This works around a "feature" of the +-- ipairs() function that stops iteration at the first nil. +-- +-- @param arr +-- The array to iterate. +-- @param func +-- The function to call. The value (not the index) will be passed +-- as the only argument. +-- + + function table.foreachi(arr, func) + local n = #arr + for i = 1, n do + local v = arr[i] + if v then + func(v) + end + end + end + + -- -- Merge two lists into an array of objects, containing pairs -- of values, one from each list. @@ -136,7 +161,7 @@ -- -- Inserts a value of array of values into a table. If the value is --- itself a table, its contents are enumerated and added instead. So +-- itself a table, its contents are enumerated and added instead. So -- these inputs give these outputs: -- -- "x" -> { "x" } @@ -229,7 +254,7 @@ if tbl[i] == obj then return i end - end + end end @@ -253,5 +278,5 @@ end return result end - - + +