class CSVTokenizer

Copyright © 2003-2006 Kouichirou Eto, All rights reserved. This is free software with ABSOLUTELY NO WARRANTY. You can redistribute it and/or modify it under the terms of the GNU GPL 2.

Public Class Methods

csv_split(source) click to toggle source
# File vendor/qwik/lib/qwik/util-csv-tokenizer.rb, line 6
def self.csv_split(source)
  status = :IN_FIELD
  csv = []
  last = ''
  csv << last

  while !source.empty?
    case status

    when :IN_FIELD
      case source
      when /\A'/
        source = $'
        last << "'"
        status = :IN_QFIELD
      when /\A,/
        source = $'
        last = ''
        csv << last
      when /\A(\\)/
        source = $'
      when /\A([^,'\\]*)/ # anything else
        source = $'
        last << $1
      end

    when :IN_QFIELD
      case source
      when /\A'/
        source = $'
        last << "'"
        status = :IN_FIELD
      when /\A(\\)/
        source = $'
        last << $1
        status = :IN_ESCAPE
      when /\A([^'\\]*)/ # anything else
        source = $'
        last << $1
      end

    when :IN_ESCAPE
      if /\A(.)/ =~ source
        source = $'
        last << $1
      end
      status = :IN_QFIELD

    end
  end

  csv = csv.map {|a|
    a.strip
  }

  csv
end