Skip to content

Instantly share code, notes, and snippets.

@koduki
Last active June 26, 2019 09:20
Show Gist options
  • Save koduki/42683fbf5b676c8188bf5266e3d9229d to your computer and use it in GitHub Desktop.
Save koduki/42683fbf5b676c8188bf5266e3d9229d to your computer and use it in GitHub Desktop.
irb(main):132:0> str
=> "a b 'c d' e"
irb(main):133:0> scan(str)
=> ["a", "b", "c d", "e"]
def scan(str)
if str == nil
[]
else
data = if str[0] == "'"
split_quote(str)
elsif str[0] == " "
skip_space(str)
else
split_space(str)
end
data[0] + scan(data[1])
end
end
def skip_space(str)
[[], str[1..-1]]
end
def split_space(str)
head = ""
tail = nil
for i in 0..(str.size - 1) do
x = str[i]
if x == " "
tail = str[(i+1)..-1]
break
else
head += x
end
end
[[head], tail]
end
def split_quote(str)
head = ""
tail = nil
for i in 1..(str.size - 1) do
x = str[i]
if x == "'"
tail = str[(i+2)..-1]
break
else
head += x
end
end
[[head], tail]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment