Hi,

At Mon, 7 Nov 2005 16:20:47 +0900,
Kev Jackson wrote in [ruby-core:06581]:
> +        case line

You'd intend just `case' without `line' here.

This is my version.

  def shellwords2(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(/\\(?=.)/, ''))
      if sep
        words << field
        field = ''
      end
    end
    raise ArgumentError, "Unmatched double quote: #{line}" if line[last]
    words
  end

It seems nearly 50% faster than current version.

  $ ruby -Ilib -rshellwords -rbenchmark -e 'n=50;s="abc "*500;Benchmark.bm{|x|x.report {n.times{Shellwords.shellwords(s)}};x.report {n.times{Shellwords.shellwords2(s)}}}'
        user     system      total        real
    0.594000   0.015000   0.609000 (  0.599000)
    0.390000   0.000000   0.390000 (  0.394000)

-- 
Nobu Nakada