Created
September 24, 2023 02:01
-
-
Save denisdemaisbr/e16a9ee7b6eb49652da6b54d8cfb1ddc to your computer and use it in GitHub Desktop.
strstr() lua implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- works like c strstr() | |
-- retorn index if found pattern =) | |
function strstr(haystack, needle) | |
if (not haystack) then return nil; end | |
if (not needle) then return nil; end | |
local h, n = #haystack, #needle; | |
for i = 1, h - n + 1 do | |
if haystack:sub(i, i + n - 1) == needle then | |
return i; | |
end | |
end | |
return nil; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment