/irc-oct-23.rb Secret
Last active
October 23, 2024 16:19
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
require "strscan" | |
string = "aaa bbb ccc/'ddd eee/fff ggg' hhh/jjj 'kkk lll'" | |
class Scanner | |
def initialize(string) | |
@scanner = StringScanner.new(string) | |
end | |
def scan | |
matches = [] | |
inside_quote = false | |
current = "" | |
until @scanner.eos? | |
ch = @scanner.getch | |
case ch | |
when "'" | |
inside_quote = !inside_quote | |
current << ch | |
when " " | |
if inside_quote | |
current << ch | |
else | |
matches << current | |
current = "" | |
end | |
else | |
current << ch | |
end | |
end | |
matches << current | |
matches | |
end | |
end | |
pp Scanner.new(string).scan | |
pp ["aaa", "bbb", "ccc/'ddd eee/fff ggg'", "hhh/jjj", "'kkk lll'"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment