summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2022-02-28 20:25:39 +0000
committerGitHub <noreply@github.com>2022-02-28 20:25:39 +0000
commitdc4d83ffa2d24135efff45647121bcb27449acfb (patch)
treeb1b837a54872d8898cd9d26c02a01dfb68e3a293
parent9a6ee04f0d351f82fbba65446e8318c8fe188117 (diff)
parent431b3b892f0dd29581406dcc4ed690358d5780ca (diff)
downloadrspamd-dc4d83ffa2d24135efff45647121bcb27449acfb.tar.gz
rspamd-dc4d83ffa2d24135efff45647121bcb27449acfb.zip
Merge pull request #4093 from citrin/str_endswith
[Minor] Microoptimize lua_util.str_endswith
-rw-r--r--lualib/lua_util.lua2
-rw-r--r--test/lua/unit/lua_util.misc.lua33
2 files changed, 33 insertions, 2 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua
index 308663399..7cabeed2d 100644
--- a/lualib/lua_util.lua
+++ b/lualib/lua_util.lua
@@ -113,7 +113,7 @@ end
-- @return {boolean} true if text ends with the specified suffix, false otherwise
--]]
exports.str_endswith = function(s, suffix)
- return s:sub(-suffix:len()) == suffix
+ return s:find(suffix, -suffix:len(), true) ~= nil
end
--[[[
diff --git a/test/lua/unit/lua_util.misc.lua b/test/lua/unit/lua_util.misc.lua
index b19b4d6f1..bab44a38b 100644
--- a/test/lua/unit/lua_util.misc.lua
+++ b/test/lua/unit/lua_util.misc.lua
@@ -27,4 +27,35 @@ context("Lua util - callback_from_string", function()
assert_false(ret)
end)
end
-end) \ No newline at end of file
+end)
+
+context("Lua util - str_endswith", function()
+ local ending = {
+ {'a', 'a'},
+ {'ab', 'b'},
+ {'ab', 'ab'},
+ {'abc', 'bc'},
+ {'any', ''},
+ }
+ local not_ending = {
+ {'a', 'b'},
+ {'', 'a'},
+ {'ab', 'a'},
+ {'ab', 'ba'},
+ {'ab', 'lab'},
+ {'abc', 'ab'},
+ {'abcd', 'bc'},
+ {'a', 'A'},
+ {'aB', 'b'},
+ }
+ for _, c in ipairs(ending) do
+ test(string.format('True case: str_endswith("%s", "%s")', c[1], c[2]), function()
+ assert_true(util.str_endswith(c[1], c[2]))
+ end)
+ end
+ for _, c in ipairs(not_ending) do
+ test(string.format('False case: str_endswith("%s", "%s")', c[1], c[2]), function()
+ assert_false(util.str_endswith(c[1], c[2]))
+ end)
+ end
+end)