Last active
August 29, 2015 14:20
-
-
Save ottworks/a89e77b1b0a66eebd414 to your computer and use it in GitHub Desktop.
Command interpretation
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
input = [[ms slap "Mister Sandman" 23 im"nots"ure ""what"s going on"]] | |
input = " " .. input .. " " | |
f1 = "%s+%S" | |
f2 = "[^%s\"]%s+" | |
f3 = "\"%s+" | |
local cursor = 0 | |
local quote = false | |
local word = false | |
local out = {} | |
while cursor < #input do | |
local a, b | |
if not quote and not word then | |
a, b = input:find(f1, cursor) | |
if a then | |
cursor = a | |
local t = input:sub(b, b) | |
if t == "\"" then | |
quote = b | |
else | |
word = b | |
end | |
end | |
elseif quote then | |
a, b = input:find(f3, cursor) | |
if a then | |
cursor = a | |
table.insert(out, input:sub(quote + 1, a - 1)) | |
quote = false | |
end | |
else | |
a, b = input:find(f2, cursor) | |
if a then | |
cursor = a | |
table.insert(out, input:sub(word, a)) | |
word = false | |
end | |
end | |
cursor = cursor + 1 | |
end | |
for _, word in ipairs(out) do | |
print(word) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment