From 07df24f7d015521dc73598f93c27bf5a721e7598 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Mon, 22 Mar 2021 20:21:15 +0100 Subject: [PATCH 1/4] Add utility to count table keys See #3293. Signed-off-by: Lucas Schwiderski --- lib/gears/table.lua | 15 +++++++++++++++ spec/gears/table_spec.lua | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/lib/gears/table.lua b/lib/gears/table.lua index 118971c2..6572fdd3 100644 --- a/lib/gears/table.lua +++ b/lib/gears/table.lua @@ -165,6 +165,21 @@ function gtable.keys(t) return keys end +--- Get the number of keys in a table, both integer and string indicies. +-- +-- This is functionally equivalent, but faster than `#gears.table.keys(t)`. +-- +-- @tparam table t The table for which to count the keys. +-- @treturn number The number of keys in the table. +-- @staticfct gears.table.count_keys +function gtable.count_keys(t) + local count = 0 + for _, _ in pairs(t) do + count = count + 1 + end + return count +end + --- Filter a table's keys for certain content type. -- -- @tparam table t The table to retrieve the keys for. diff --git a/spec/gears/table_spec.lua b/spec/gears/table_spec.lua index dbe836a8..1c33055d 100644 --- a/spec/gears/table_spec.lua +++ b/spec/gears/table_spec.lua @@ -7,6 +7,34 @@ describe("gears.table", function() assert.is.same(gtable.keys(t), { 1, 2, "a" }) end) + describe("gears.table.count_keys", function() + it("counts keys in an empty table", function() + local t = {} + assert.is.same(gtable.count_keys(t), 0) + end) + + it("counts keys in a sparse array", function() + local t = { 1, nil, 3 } + assert.is.same(gtable.count_keys(t), 2) + end) + + it("counts keys in a regular array", function() + local t = { 1, 2, 3 } + assert.is.same(gtable.count_keys(t), 3) + end) + + it("counts keys in a hash table", function() + local t = { a = 1, b = "2", c = true } + assert.is.same(gtable.count_keys(t), 3) + end) + + it("counts keys in a mixed table", function() + local t = { 1, a = 2, nil, 4 } + assert.is.same(gtable.count_keys(t), 3) + end) + + end) + it("table.keys_filter", function() local t = { "a", 1, function() end, false} assert.is.same(gtable.keys_filter(t, "number", "function"), { 2, 3 }) From 058190a3c0cc2097d349ea5adb0ed15461789ad1 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Mon, 22 Mar 2021 20:22:12 +0100 Subject: [PATCH 2/4] Add missing modelines Signed-off-by: Lucas Schwiderski --- lib/gears/table.lua | 2 ++ spec/gears/table_spec.lua | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/gears/table.lua b/lib/gears/table.lua index 6572fdd3..7d4f00bd 100644 --- a/lib/gears/table.lua +++ b/lib/gears/table.lua @@ -415,3 +415,5 @@ function gtable.map(f, tbl) end return gtable + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/spec/gears/table_spec.lua b/spec/gears/table_spec.lua index 1c33055d..f46f83b0 100644 --- a/spec/gears/table_spec.lua +++ b/spec/gears/table_spec.lua @@ -132,3 +132,5 @@ describe("gears.table", function() end) end) end) + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 From afced71a9aec841c4b0b757a3cdd716e77ba09ea Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Mon, 22 Mar 2021 20:55:40 +0100 Subject: [PATCH 3/4] Add example for gears.table.count_keys Signed-off-by: Lucas Schwiderski --- lib/gears/table.lua | 2 ++ tests/examples/text/gears/table/count_keys.lua | 6 ++++++ 2 files changed, 8 insertions(+) create mode 100644 tests/examples/text/gears/table/count_keys.lua diff --git a/lib/gears/table.lua b/lib/gears/table.lua index 7d4f00bd..e1744f28 100644 --- a/lib/gears/table.lua +++ b/lib/gears/table.lua @@ -169,6 +169,8 @@ end -- -- This is functionally equivalent, but faster than `#gears.table.keys(t)`. -- +-- @DOC_text_gears_table_count_keys_EXAMPLE@ +-- -- @tparam table t The table for which to count the keys. -- @treturn number The number of keys in the table. -- @staticfct gears.table.count_keys diff --git a/tests/examples/text/gears/table/count_keys.lua b/tests/examples/text/gears/table/count_keys.lua new file mode 100644 index 00000000..543db2b1 --- /dev/null +++ b/tests/examples/text/gears/table/count_keys.lua @@ -0,0 +1,6 @@ +--DOC_GEN_OUTPUT --DOC_HIDE +local gears = require("gears") --DOC_HIDE + +local tab = { 1, nil, "a", "b", foo = "bar" } +local count = gears.table.count_keys(tab) +print("The table has " .. count .. " keys") From e7d55567a6f0d44b3e8b5c4b6b11bf74f2149355 Mon Sep 17 00:00:00 2001 From: Lucas Schwiderski Date: Sat, 27 Mar 2021 14:08:47 +0100 Subject: [PATCH 4/4] Remove redundant assignment Signed-off-by: Lucas Schwiderski --- lib/gears/table.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/gears/table.lua b/lib/gears/table.lua index e1744f28..301f6848 100644 --- a/lib/gears/table.lua +++ b/lib/gears/table.lua @@ -176,7 +176,7 @@ end -- @staticfct gears.table.count_keys function gtable.count_keys(t) local count = 0 - for _, _ in pairs(t) do + for _ in pairs(t) do count = count + 1 end return count