From 6b723363b891a3a72b20718864cae780c823ebda Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 15 Oct 2023 14:20:55 -0700 Subject: add some tests for rfind --- search.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/search.lua b/search.lua index c9af187..dab7e1f 100644 --- a/search.lua +++ b/search.lua @@ -141,3 +141,10 @@ function rfind(s, pat, i, plain) assert (endpos >= #pat) return endpos-#pat+1 end + +function test_rfind() + 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 -- cgit 1.4.1-2-gfad0 From ea4a8379fe577207509719af41491190035fc8fa Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 15 Oct 2023 14:21:41 -0700 Subject: rfind bugfix: handle empty pattern like string.find --- search.lua | 2 ++ 1 file changed, 2 insertions(+) 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') -- cgit 1.4.1-2-gfad0