aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/shellwords.rb14
1 files changed, 5 insertions, 9 deletions
diff --git a/lib/shellwords.rb b/lib/shellwords.rb
index 055a6a5be2..1d97cd62f2 100644
--- a/lib/shellwords.rb
+++ b/lib/shellwords.rb
@@ -26,22 +26,18 @@ module Shellwords
# See the +Shellwords+ module documentation for an example.
#
def shellwords(line)
- line = String.new(line) rescue
- raise(ArgumentError, "Argument must be a string")
words = []
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(/\\(?=.)/, ''))
+ word = sq = dq = esc = garbage = sep = nil
+ line.scan(/\G\s*(?>([^\s\\\'\"]+)|'([^\']*)'|"((?:[^\"\\]|\\.)*)"|(\\.?)|(\S))(\s|\z)?/m) do
+ |word, sq, dq, esc, garbage, sep|
+ raise ArgumentError, "Unmatched double quote: #{line.inspect}" if garbage
+ field << (word || sq || (dq || esc).gsub(/\\(?=.)/, ''))
if sep
words << field
field = ''
end
end
- raise ArgumentError, "Unmatched double quote: #{line}" if line[last]
words
end