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 | 0fd2b68c2c9a7aced70ad1a230d7fcdb31460f26 (patch) | |
tree | 4fc7f31c32a0aa03abc6c185609dbe208dd4e80b /lib | |
parent | deeea77dfdf5ec297ea8189a3ec884c9aa99e05d (diff) | |
download | ruby-0fd2b68c2c9a7aced70ad1a230d7fcdb31460f26.tar.gz ruby-0fd2b68c2c9a7aced70ad1a230d7fcdb31460f26.tar.xz ruby-0fd2b68c2c9a7aced70ad1a230d7fcdb31460f26.zip |
* lib/shellwords.rb: refactored. [ruby-core:06581]
git-svn-id: http://svn.ruby-lang.org/repos/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 e87b9e656..055a6a5be 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 |