Skip to content

Instantly share code, notes, and snippets.

@denisdemaisbr
Created September 24, 2023 02:01
Show Gist options
  • Save denisdemaisbr/e16a9ee7b6eb49652da6b54d8cfb1ddc to your computer and use it in GitHub Desktop.
Save denisdemaisbr/e16a9ee7b6eb49652da6b54d8cfb1ddc to your computer and use it in GitHub Desktop.
strstr() lua implementation
-- 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