diff --git a/search.lua b/search.lua index 37306f8..f7d4732 100644 --- a/search.lua +++ b/search.lua @@ -116,6 +116,8 @@ function find(s, pat, i, plain) return s:find(pat, i, plain) end +-- TODO: avoid the expensive reverse() operations +-- Particularly if we only care about literal matches, we don't need all of string.find function rfind(s, pat, i, plain) if s == nil then return end local rs = s:reverse() diff --git a/text.lua b/text.lua index 1833841..f868dc5 100644 --- a/text.lua +++ b/text.lua @@ -931,3 +931,27 @@ end function rtrim(s) return s:gsub('%s+$', '') end + +function starts_with(s, prefix) + if #s < #prefix then + return false + end + for i=1,#prefix do + if s:sub(i,i) ~= prefix:sub(i,i) then + return false + end + end + return true +end + +function ends_with(s, suffix) + if #s < #suffix then + return false + end + for i=0,#suffix-1 do + if s:sub(#s-i,#s-i) ~= suffix:sub(#suffix-i,#suffix-i) then + return false + end + end + return true +end