diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-11-08 23:41:40 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-11-08 23:41:40 +0000 |
commit | e8201d12db87546249e23871b959317580ff81ff (patch) | |
tree | 4fc7f31c32a0aa03abc6c185609dbe208dd4e80b /lib | |
parent | 6ddd56a1308703c4ee26d942e3012531c7f80973 (diff) | |
download | ruby-e8201d12db87546249e23871b959317580ff81ff.tar.gz |
* lib/shellwords.rb: refactored. [ruby-core:06581]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9519 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/shellwords.rb | 33 |
1 files changed, 11 insertions, 22 deletions
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 |