about summary refs log tree commit diff stats
path: root/search.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-10-15 14:21:41 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-10-15 14:21:41 -0700
commitea4a8379fe577207509719af41491190035fc8fa (patch)
tree82b2392652ad7d79372f42aaeb8b8c4eb7058b59 /search.lua
parent6b723363b891a3a72b20718864cae780c823ebda (diff)
downloadlines.love-ea4a8379fe577207509719af41491190035fc8fa.tar.gz
rfind bugfix: handle empty pattern like string.find
Diffstat (limited to 'search.lua')
-rw-r--r--search.lua2
1 files changed, 2 insertions, 0 deletions
diff --git a/search.lua b/search.lua
index dab7e1f..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
@@ -143,6 +144,7 @@ function rfind(s, pat, i, plain)
 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')