Last active
June 26, 2019 09:20
-
-
Save koduki/42683fbf5b676c8188bf5266e3d9229d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
irb(main):132:0> str | |
=> "a b 'c d' e" | |
irb(main):133:0> scan(str) | |
=> ["a", "b", "c d", "e"] |
This file contains hidden or 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
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