about summary refs log tree commit diff stats
path: root/text.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-01-13 09:10:48 -0800
committerKartik K. Agaram <vc@akkartik.com>2023-01-13 09:10:48 -0800
commit22bf3da7de9074c6b99d8e43e3ab1772aed7b059 (patch)
tree1f6590d5b3aba69f5a63d8cf93dbd5207a8d8c37 /text.lua
parente8ec87255f94db9c6f1a0db10449f6304440a350 (diff)
downloadlines.love-22bf3da7de9074c6b99d8e43e3ab1772aed7b059.tar.gz
reduce use of rfind
Diffstat (limited to 'text.lua')
-rw-r--r--text.lua24
1 files changed, 24 insertions, 0 deletions
diff --git a/text.lua b/text.lua
index 8232335..386553f 100644
--- a/text.lua
+++ b/text.lua
@@ -1001,3 +1001,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