This is an automated email from the ASF dual-hosted git repository.
monkeydluffy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git
The following commit(s) were added to refs/heads/master by this push:
new a16734809 refactor: use pl.stringx.rfind to replace the get_last_index
func (#8358)
a16734809 is described below
commit a16734809be4f917f07dd2b4d5a92a6591ddb04b
Author: Liu Wei <[email protected]>
AuthorDate: Tue May 16 17:28:13 2023 +0800
refactor: use pl.stringx.rfind to replace the get_last_index func (#8358)
---
apisix/plugins/log-rotate.lua | 21 ++++-----------------
t/core/utils.t | 32 ++++++++++++++++++++++++++++++++
2 files changed, 36 insertions(+), 17 deletions(-)
diff --git a/apisix/plugins/log-rotate.lua b/apisix/plugins/log-rotate.lua
index 7380b0d1c..61bcea163 100644
--- a/apisix/plugins/log-rotate.lua
+++ b/apisix/plugins/log-rotate.lua
@@ -32,10 +32,9 @@ local os_date = os.date
local os_remove = os.remove
local os_rename = os.rename
local str_sub = string.sub
-local str_find = string.find
local str_format = string.format
-local str_reverse = string.reverse
local ngx_sleep = require("apisix.core.utils").sleep
+local string_rfind = require("pl.stringx").rfind
local local_conf
@@ -74,18 +73,6 @@ local function file_exists(path)
end
-local function get_last_index(str, key)
- local rev = str_reverse(str)
- local _, idx = str_find(rev, key)
- local n
- if idx then
- n = #rev - idx + 1
- end
-
- return n
-end
-
-
local function get_log_path_info(file_type)
local_conf = core.config.local_conf()
local conf_path
@@ -106,7 +93,7 @@ local function get_log_path_info(file_type)
if root ~= "/" then
conf_path = prefix .. conf_path
end
- local n = get_last_index(conf_path, "/")
+ local n = string_rfind(conf_path, "/")
if n ~= nil and n ~= #conf_path then
local dir = str_sub(conf_path, 1, n)
local name = str_sub(conf_path, n + 1)
@@ -130,7 +117,7 @@ local function scan_log_folder(log_file_name)
local compression_log_type = log_file_name .. COMPRESSION_FILE_SUFFIX
for file in lfs.dir(log_dir) do
- local n = get_last_index(file, "__")
+ local n = string_rfind(file, "__")
if n ~= nil then
local log_type = file:sub(n + 2)
if log_type == log_file_name or log_type == compression_log_type
then
@@ -174,7 +161,7 @@ local function compression_file(new_file)
return
end
- local n = get_last_index(new_file, "/")
+ local n = string_rfind(new_file, "/")
local new_filepath = str_sub(new_file, 1, n)
local new_filename = str_sub(new_file, n + 1)
local com_filename = new_filename .. COMPRESSION_FILE_SUFFIX
diff --git a/t/core/utils.t b/t/core/utils.t
index 4e6b0d766..9faa545e1 100644
--- a/t/core/utils.t
+++ b/t/core/utils.t
@@ -361,3 +361,35 @@ apisix:
GET /t
--- error_log
failed to parse domain: ipv6.local
+
+
+
+=== TEST 12: get_last_index
+--- config
+ location /t {
+ content_by_lua_block {
+ local string_rfind = require("pl.stringx").rfind
+ local cases = {
+ {"you are welcome", "co"},
+ {"nice to meet you", "meet"},
+ {"chicken run", "cc"},
+ {"day day up", "day"},
+ {"happy new year", "e"},
+ {"apisix__1928", "__"}
+ }
+
+ for _, case in ipairs(cases) do
+ local res = string_rfind(case[1], case[2])
+ ngx.say("res:", res)
+ end
+ }
+ }
+--- request
+GET /t
+--- response_body
+res:12
+res:9
+res:nil
+res:5
+res:12
+res:7