gear/strings.lua

29 lines
672 B
Lua

local strings = {}
--- Test whether a string starts with a prefix.
function strings.startswith(s, prefix)
-- optimized version of: return s:sub(1, #prefix) == prefix
for i = 1,#prefix do
if s:byte(i) ~= prefix:byte(i) then
return false
end
end
return true
end
--- Test whether a string ends with a trailing suffix.
function strings.endswith(s, trailing)
-- optimized version of: return trailing == "" or s:sub(-#trailing) == trailing
local n1,n2 = #s,#trailing
for i = 0,n2-1 do
if s:byte(n1-i) ~= trailing:byte(n2-i) then
return false
end
end
return true
end
return strings