Merge text.love

This commit is contained in:
Kartik K. Agaram 2023-01-13 09:48:20 -08:00
commit e80f48506d
2 changed files with 26 additions and 0 deletions

View File

@ -116,6 +116,8 @@ function find(s, pat, i, plain)
return s:find(pat, i, plain) return s:find(pat, i, plain)
end 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) function rfind(s, pat, i, plain)
if s == nil then return end if s == nil then return end
local rs = s:reverse() local rs = s:reverse()

View File

@ -931,3 +931,27 @@ end
function rtrim(s) function rtrim(s)
return s:gsub('%s+$', '') return s:gsub('%s+$', '')
end 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