summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2021-10-27 00:45:44 +0800
committerruki <[email protected]>2021-10-27 00:45:44 +0800
commit17b521d9059907d08390b462bcf09bd8075b84b3 (patch)
treeb80d4dc6ef07b27f5173e8e2c3bb9fd9bf5bc031
parent1115732be0fb8f1eb24b37d39b22dc187ed9062f (diff)
add table.contains
-rw-r--r--xmake/core/base/table.lua21
1 files changed, 21 insertions, 0 deletions
diff --git a/xmake/core/base/table.lua b/xmake/core/base/table.lua
index 5f2c4585a..4f21ec358 100644
--- a/xmake/core/base/table.lua
+++ b/xmake/core/base/table.lua
@@ -227,6 +227,27 @@ function table.is_dictionary(dict)
return type(dict) == "table" and dict[1] == nil
end
+-- does contain the given value in table?
+function table.contains(t, value)
+ local found = false
+ if table.is_array(t) then
+ for _, v in ipairs(t) do
+ if v == value then
+ found = true
+ break
+ end
+ end
+ else
+ for _, v in pairs(t) do
+ if v == value then
+ found = true
+ break
+ end
+ end
+ end
+ return found
+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