From dde520fe155de030a0892a312b3f4d1e19398f95 Mon Sep 17 00:00:00 2001 From: --get-all Date: Tue, 12 Apr 2022 14:57:58 +0800 Subject: [PATCH 1/2] add table meta:foreach,foreachi,getn --- locale/en-us/meta.lua | 6 ++++++ locale/zh-cn/meta.lua | 6 ++++++ meta/template/table.lua | 27 +++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/locale/en-us/meta.lua b/locale/en-us/meta.lua index 858cde13d..839dc27a6 100644 --- a/locale/en-us/meta.lua +++ b/locale/en-us/meta.lua @@ -677,6 +677,12 @@ Returns the elements from the given list. This function is equivalent to ``` By default, `i` is `1` and `j` is `#list`. ]] +table.foreach = +'Executes the given f over all elements of table. For each element, f is called with the index and respective value as arguments. If f returns a non-nil value, then the loop is broken, and this value is returned as the final value of foreach.' +table.foreachi = +'Executes the given f over the numerical indices of table. For each index, f is called with the index and respective value as arguments. Indices are visited in sequential order, from 1 to n, where n is the size of the table. If f returns a non-nil value, then the loop is broken and this value is returned as the result of foreachi.' +table.getn = +'Returns the number of elements in the table. This function is equivalent to `#list`.' utf8 = '' diff --git a/locale/zh-cn/meta.lua b/locale/zh-cn/meta.lua index 8e7c37e88..61ba6c9b0 100644 --- a/locale/zh-cn/meta.lua +++ b/locale/zh-cn/meta.lua @@ -655,6 +655,12 @@ table.unpack = ``` i 默认为 1 ,j 默认为 #list。 ]] +table.foreach = +'遍历表中的每一个元素,并以key和value执行回调函数。如果回调函数返回一个非nil值则循环终止,并且返回这个值。该函数等同pair(list),比pair(list)更慢。不推荐使用' +table.foreachi = +'遍历数组中的每一个元素,并以索引号index和value执行回调函数。如果回调函数返回一个非nil值则循环终止,并且返回这个值。该函数等同ipair(list),比ipair(list)更慢。不推荐使用' +table.getn = +'返回表的长度。该函数等价于#list。' utf8 = '' diff --git a/meta/template/table.lua b/meta/template/table.lua index 3ec92ce8a..21c8b619a 100644 --- a/meta/template/table.lua +++ b/meta/template/table.lua @@ -64,4 +64,31 @@ function table.sort(list, comp) end ---@nodiscard function table.unpack(list, i, j) end +---@version <5.1, JIT +---#DES 'table.foreach' +---@generic T +---@param list any +---@param callback fun(key: string, value: any):T|nil +---@return T|nil +---@deprecated +function table.foreach(list, callback) end + +---@version <5.1, JIT +---#DES 'table.foreachi' +---@generic T +---@param list any +---@param callback fun(key: string, value: any):T|nil +---@return T|nil +---@deprecated +function table.foreachi(list, callback) end + +---@version <5.1, JIT +---#DES 'table.getn' +---@generic T +---@param list T[] +---@return integer +---@nodiscard +---@deprecated +function table.getn(list) end + return table From 9abbb86c26474d613cc0bc63c918abdc876c4da0 Mon Sep 17 00:00:00 2001 From: --get-all Date: Tue, 26 Apr 2022 12:09:57 +0800 Subject: [PATCH 2/2] fix ngx doc error --- meta/3rd/OpenResty/library/ngx.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/meta/3rd/OpenResty/library/ngx.lua b/meta/3rd/OpenResty/library/ngx.lua index f8f5eabfe..bacafa674 100644 --- a/meta/3rd/OpenResty/library/ngx.lua +++ b/meta/3rd/OpenResty/library/ngx.lua @@ -2134,7 +2134,7 @@ function ngx.req.get_post_args(max_args) end --- --- Removing the `max_args` cap is strongly discouraged. --- ----@param max_args number +---@param max_args? number ---@return table args ---@return string|'"truncated"' error function ngx.req.get_uri_args(max_args) end @@ -2438,8 +2438,8 @@ function ngx.req.clear_header(header_name) end --- --- The `__index` metamethod will not be added when the `raw` argument is set to `true`. --- ----@param max_headers number ----@param raw boolean +---@param max_headers? number +---@param raw? boolean ---@return table headers ---@return string|'"truncated"' error function ngx.req.get_headers(max_headers, raw) end @@ -4211,8 +4211,8 @@ ngx.resp = {} --- --- Note that a maximum of 100 response headers are parsed by default (including those with the same name) and that additional response headers are silently discarded to guard against potential denial of service attacks. When the limit is exceeded, it will return a second value which is the string `"truncated"`. --- ----@param max_headers number ----@param raw boolean +---@param max_headers? number +---@param raw? boolean ---@return table ---@return string|'"truncated"' error function ngx.resp.get_headers(max_headers, raw) end