Created
January 28, 2013 16:02
-
-
Save cutthroat/4656721 to your computer and use it in GitHub Desktop.
Split strings in Lua, the Python way
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
function string.split(s, pattern, init, plain) | |
local function f(s, init) | |
local n = #s + 1 | |
if init > n then return nil end | |
local i, j = s:find(pattern, init, plain) | |
if i == nil then | |
return n + 1, init, n - 1 | |
else | |
return j + 1, init, i - 1 | |
end | |
end | |
return f, s, init or 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment