From e8201d12db87546249e23871b959317580ff81ff Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 8 Nov 2005 23:41:40 +0000 Subject: * lib/shellwords.rb: refactored. [ruby-core:06581] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9519 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/shellwords.rb | 33 +++++++++++---------------------- 1 file changed, 11 insertions(+), 22 deletions(-) (limited to 'lib/shellwords.rb') diff --git a/lib/shellwords.rb b/lib/shellwords.rb index e87b9e656b..055a6a5be2 100644 --- a/lib/shellwords.rb +++ b/lib/shellwords.rb @@ -28,31 +28,20 @@ module Shellwords def shellwords(line) line = String.new(line) rescue raise(ArgumentError, "Argument must be a string") - line.lstrip! words = [] - until line.empty? - field = '' - loop do - if line.sub!(/\A"(([^"\\]|\\.)*)"/, '') then - snippet = $1.gsub(/\\(.)/, '\1') - elsif line =~ /\A"/ then - raise ArgumentError, "Unmatched double quote: #{line}" - elsif line.sub!(/\A'([^']*)'/, '') then - snippet = $1 - elsif line =~ /\A'/ then - raise ArgumentError, "Unmatched single quote: #{line}" - elsif line.sub!(/\A\\(.)?/, '') then - snippet = $1 || '\\' - elsif line.sub!(/\A([^\s\\'"]+)/, '') then - snippet = $1 - else - line.lstrip! - break - end - field.concat(snippet) + field = '' + last = 0 + sep = nil + line.scan(/\G\s*(?:([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?))(\s+|\z)?/m) do + last = $~.end(0) + sep = $~.begin(5) + field << ($1 || $2 || ($3 || $4).gsub(/\\(?=.)/, '')) + if sep + words << field + field = '' end - words.push(field) end + raise ArgumentError, "Unmatched double quote: #{line}" if line[last] words end -- cgit v1.2.3