From: Anton Yuzhaninov Date: Sat, 26 Feb 2022 13:56:36 +0000 (+0000) Subject: [Minor] Microoptimize lua_util.str_endswith X-Git-Tag: 3.2~37^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F4093%2Fhead;p=thirdparty%2Frspamd.git [Minor] Microoptimize lua_util.str_endswith Use find to check string suffix instead of sub (which involves string interning of a returned string). Benchmarks with LuaJIT 2.1.0 shows that an option with find is significantly faster. While here added unit test for this function. --- diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 308663399b..7cabeed2d2 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 b19b4d6f18..bab44a38b6 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)