Created
August 5, 2016 23:53
-
-
Save thelastpenguin/ff63a54c9828d281ece5cc2d2f834949 to your computer and use it in GitHub Desktop.
Simple quoteed string parser written in lua
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
local quotes = { | |
['\''] = true, | |
['\"'] = true | |
} | |
local function parseString(line) | |
local function skipWhiteSpace(index) | |
return string.find(line, '%S', index) | |
end | |
local function findNextSpace(index) | |
return string.find(line, '%s', index) | |
end | |
local function findClosingQuote(index, type) | |
return string.find(line, type, index) | |
end | |
local parts = {} | |
local index = 1 | |
while index ~= nil do | |
index = skipWhiteSpace(index) | |
if not index then break end | |
local cur = string.sub(line, index, index) | |
if quotes[cur] then | |
local closer = findClosingQuote(index + 1, cur) | |
local quotedString = string.sub(line, index + 1, closer and closer - 1 or nil) | |
table.insert(parts, quotedString) | |
if not closer then break end | |
index = closer | |
else | |
local nextSpace = findNextSpace(index) | |
local word = string.sub(line, index, nextSpace and nextSpace - 1 or nil) | |
table.insert(parts, word) | |
if not nextSpace then break end | |
index = nextSpace | |
end | |
end | |
return parts | |
end | |
for k,v in pairs(parseString("hello world 'this is a test'")) do | |
print(v) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment