diff options
-rw-r--r-- | search.lua | 9 |
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 |