RosettaCodeData/Task/Tokenize-a-string-with-escaping/Lua/tokenize-a-string-with-escaping-1.lua

26 lines
654 B
Lua
Raw Permalink Normal View History

2025-06-11 20:16:52 -04:00
function Tokenize (str, sep, esc)
2023-07-01 11:58:00 -04:00
local strList, word, escaped, ch = {}, "", false
for pos = 1, #str do
ch = str:sub(pos, pos)
if ch == esc then
if escaped then
word = word .. ch
end
2025-06-11 20:16:52 -04:00
escaped = not escaped
2023-07-01 11:58:00 -04:00
elseif ch == sep then
if escaped then
word = word .. ch
escaped = false
else
table.insert(strList, word)
word = ""
end
else
escaped = false
word = word .. ch
end
end
table.insert(strList, word)
return strList
end