about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-10-15 14:42:28 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-10-15 14:42:28 -0700
commit80ba5851e7332bccf7fa0db455625c365063680c (patch)
treefd2ab57c7abb9a8cfe239b1b454a036ef74e6cb3
parentb84cbf6d21fccdad85cb7963dd4689a8b497e729 (diff)
parentea4a8379fe577207509719af41491190035fc8fa (diff)
downloadtext.love-80ba5851e7332bccf7fa0db455625c365063680c.tar.gz
Merge lines.love
-rw-r--r--search.lua9
1 files changed, 9 insertions, 0 deletions
diff --git a/search.lua b/search.lua
index c9af187..fe57ac9 100644
--- a/search.lua
+++ b/search.lua
@@ -131,6 +131,7 @@ end
 -- 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
+  if #pat == 0 then return #s end
   local rs = s:reverse()
   local rpat = pat:reverse()
   if i == nil then i = #s end
@@ -141,3 +142,11 @@ function rfind(s, pat, i, plain)
   assert (endpos >= #pat)
   return endpos-#pat+1
 end
+
+function test_rfind()
+  check_eq(rfind('abc', ''), 3, 'empty pattern')
+  check_eq(rfind('abc', 'c'), 3, 'final char')
+  check_eq(rfind('acbc', 'c', 3), 2, 'previous char')
+  check_nil(rfind('abc', 'd'), 'missing char')
+  check_nil(rfind('abc', 'c', 2), 'no more char')
+end