Browse Source

Merge pull request #4093 from citrin/str_endswith

[Minor] Microoptimize lua_util.str_endswith
tags/3.2
Vsevolod Stakhov 2 years ago
parent
commit
dc4d83ffa2
No account linked to committer's email address
2 changed files with 33 additions and 2 deletions
  1. 1
    1
      lualib/lua_util.lua
  2. 32
    1
      test/lua/unit/lua_util.misc.lua

+ 1
- 1
lualib/lua_util.lua View File

@@ -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

--[[[

+ 32
- 1
test/lua/unit/lua_util.misc.lua View File

@@ -27,4 +27,35 @@ context("Lua util - callback_from_string", function()
assert_false(ret)
end)
end
end)
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)

Loading…
Cancel
Save