Skip to content

Instantly share code, notes, and snippets.

@ottworks
Last active August 29, 2015 14:20
Show Gist options
  • Save ottworks/a89e77b1b0a66eebd414 to your computer and use it in GitHub Desktop.
Save ottworks/a89e77b1b0a66eebd414 to your computer and use it in GitHub Desktop.
Command interpretation
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