Skip to content

Instantly share code, notes, and snippets.

@marten
Created October 1, 2013 20:29
Show Gist options
  • Save marten/6784596 to your computer and use it in GitHub Desktop.
Save marten/6784596 to your computer and use it in GitHub Desktop.
line = (<<-END).strip.chars.to_a
'"'hello world"\\", he" said, whil'e \\opening\\' "the door"
END
elements = [""]
mode = nil
while char = line.shift
puts char
case mode
when :double
case char
when '"'
mode = nil
when "\\"
line.shift # to also eat the "
elements[-1] = elements.last + '"'
else
elements[-1] = elements.last + char
end
when :single
case char
when "'"
mode = nil
else
elements[-1] = elements.last + char
end
else
case char
when '"'
puts 'switch to double'
mode = :double
when "'"
mode = :single
when " "
elements << ""
else
elements[-1] = elements.last + char
end
end
end
elements.each { |i| puts i }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment