csv-3.1.2/0000755000004100000410000000000013553655261012353 5ustar www-datawww-datacsv-3.1.2/README.md0000644000004100000410000000335413553655261013637 0ustar www-datawww-data# CSV [![Build Status](https://travis-ci.org/ruby/csv.svg?branch=master)](https://travis-ci.org/ruby/csv) [![Test Coverage](https://api.codeclimate.com/v1/badges/321fa39e510a0abd0369/test_coverage)](https://codeclimate.com/github/ruby/csv/test_coverage) This library provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed. ## Installation Add this line to your application's Gemfile: ```ruby gem 'csv' ``` And then execute: $ bundle Or install it yourself as: $ gem install csv ## Usage ```ruby require "csv" CSV.foreach("path/to/file.csv") do |row| # use row here... end ``` ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org). ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/csv. ### NOTE: About RuboCop We don't use RuboCop because we can manage our coding style by ourselves. We want to accept small fluctuations in our coding style because we use Ruby. Please do not submit issues and PRs that aim to introduce RuboCop in this repository. ## License The gem is available as open source under the terms of the [2-Clause BSD License](https://opensource.org/licenses/BSD-2-Clause). See LICENSE.txt for details. csv-3.1.2/csv.gemspec0000644000004100000410000000442013553655261014513 0ustar www-datawww-data######################################################### # This file has been automatically generated by gem2tgz # ######################################################### # -*- encoding: utf-8 -*- # stub: csv 3.1.2 ruby lib Gem::Specification.new do |s| s.name = "csv".freeze s.version = "3.1.2" s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= s.require_paths = ["lib".freeze] s.authors = ["James Edward Gray II".freeze, "Kouhei Sutou".freeze] s.date = "2019-10-11" s.description = "The CSV library provides a complete interface to CSV files and data. It offers tools to enable you to read and write to and from Strings or IO objects, as needed.".freeze s.email = [nil, "kou@cozmixng.org".freeze] s.files = ["LICENSE.txt".freeze, "NEWS.md".freeze, "README.md".freeze, "lib/csv.rb".freeze, "lib/csv/core_ext/array.rb".freeze, "lib/csv/core_ext/string.rb".freeze, "lib/csv/delete_suffix.rb".freeze, "lib/csv/fields_converter.rb".freeze, "lib/csv/match_p.rb".freeze, "lib/csv/parser.rb".freeze, "lib/csv/row.rb".freeze, "lib/csv/table.rb".freeze, "lib/csv/version.rb".freeze, "lib/csv/writer.rb".freeze] s.homepage = "https://github.com/ruby/csv".freeze s.licenses = ["BSD-2-Clause".freeze] s.required_ruby_version = Gem::Requirement.new(">= 2.3.0".freeze) s.rubygems_version = "2.5.2.1".freeze s.summary = "CSV Reading and Writing".freeze if s.respond_to? :specification_version then s.specification_version = 4 if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) s.add_development_dependency(%q.freeze, [">= 0"]) else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end else s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) s.add_dependency(%q.freeze, [">= 0"]) end end csv-3.1.2/lib/0000755000004100000410000000000013553655261013121 5ustar www-datawww-datacsv-3.1.2/lib/csv/0000755000004100000410000000000013553655261013714 5ustar www-datawww-datacsv-3.1.2/lib/csv/core_ext/0000755000004100000410000000000013553655261015524 5ustar www-datawww-datacsv-3.1.2/lib/csv/core_ext/array.rb0000644000004100000410000000031513553655261017166 0ustar www-datawww-dataclass Array # :nodoc: # Equivalent to CSV::generate_line(self, options) # # ["CSV", "data"].to_csv # #=> "CSV,data\n" def to_csv(**options) CSV.generate_line(self, **options) end end csv-3.1.2/lib/csv/core_ext/string.rb0000644000004100000410000000031413553655261017355 0ustar www-datawww-dataclass String # :nodoc: # Equivalent to CSV::parse_line(self, options) # # "CSV,data".parse_csv # #=> ["CSV", "data"] def parse_csv(**options) CSV.parse_line(self, **options) end end csv-3.1.2/lib/csv/fields_converter.rb0000644000004100000410000000455413553655261017606 0ustar www-datawww-data# frozen_string_literal: true class CSV # Note: Don't use this class directly. This is an internal class. class FieldsConverter include Enumerable # # A CSV::FieldsConverter is a data structure for storing the # fields converter properties to be passed as a parameter # when parsing a new file (e.g. CSV::Parser.new(@io, parser_options)) # def initialize(options={}) @converters = [] @nil_value = options[:nil_value] @empty_value = options[:empty_value] @empty_value_is_empty_string = (@empty_value == "") @accept_nil = options[:accept_nil] @builtin_converters = options[:builtin_converters] @need_static_convert = need_static_convert? end def add_converter(name=nil, &converter) if name.nil? # custom converter @converters << converter else # named converter combo = @builtin_converters[name] case combo when Array # combo converter combo.each do |sub_name| add_converter(sub_name) end else # individual named converter @converters << combo end end end def each(&block) @converters.each(&block) end def empty? @converters.empty? end def convert(fields, headers, lineno) return fields unless need_convert? fields.collect.with_index do |field, index| if field.nil? field = @nil_value elsif field.empty? field = @empty_value unless @empty_value_is_empty_string end @converters.each do |converter| break if field.nil? and @accept_nil if converter.arity == 1 # straight field converter field = converter[field] else # FieldInfo converter if headers header = headers[index] else header = nil end field = converter[field, FieldInfo.new(index, lineno, header)] end break unless field.is_a?(String) # short-circuit pipeline for speed end field # final state of each field, converted or original end end private def need_static_convert? not (@nil_value.nil? and @empty_value_is_empty_string) end def need_convert? @need_static_convert or (not @converters.empty?) end end end csv-3.1.2/lib/csv/version.rb0000644000004100000410000000015313553655261015725 0ustar www-datawww-data# frozen_string_literal: true class CSV # The version of the installed library. VERSION = "3.1.2" end csv-3.1.2/lib/csv/delete_suffix.rb0000644000004100000410000000056613553655261017076 0ustar www-datawww-data# frozen_string_literal: true # This provides String#delete_suffix? for Ruby 2.4. unless String.method_defined?(:delete_suffix) class CSV module DeleteSuffix refine String do def delete_suffix(suffix) if end_with?(suffix) self[0...-suffix.size] else self end end end end end end csv-3.1.2/lib/csv/match_p.rb0000644000004100000410000000057113553655261015657 0ustar www-datawww-data# frozen_string_literal: true # This provides String#match? and Regexp#match? for Ruby 2.3. unless String.method_defined?(:match?) class CSV module MatchP refine String do def match?(pattern) self =~ pattern end end refine Regexp do def match?(string) self =~ string end end end end end csv-3.1.2/lib/csv/writer.rb0000644000004100000410000001071713553655261015563 0ustar www-datawww-data# frozen_string_literal: true require_relative "match_p" require_relative "row" using CSV::MatchP if CSV.const_defined?(:MatchP) class CSV # Note: Don't use this class directly. This is an internal class. class Writer # # A CSV::Writer receives an output, prepares the header, format and output. # It allows us to write new rows in the object and rewind it. # attr_reader :lineno attr_reader :headers def initialize(output, options) @output = output @options = options @lineno = 0 @fields_converter = nil prepare if @options[:write_headers] and @headers self << @headers end @fields_converter = @options[:fields_converter] end # # Adds a new row # def <<(row) case row when Row row = row.fields when Hash row = @headers.collect {|header| row[header]} end @headers ||= row if @use_headers @lineno += 1 row = @fields_converter.convert(row, nil, lineno) if @fields_converter converted_row = row.collect do |field| quote(field) end line = converted_row.join(@column_separator) + @row_separator if @output_encoding line = line.encode(@output_encoding) end @output << line self end # # Winds back to the beginning # def rewind @lineno = 0 @headers = nil if @options[:headers].nil? end private def prepare @encoding = @options[:encoding] prepare_header prepare_format prepare_output end def prepare_header headers = @options[:headers] case headers when Array @headers = headers @use_headers = true when String @headers = CSV.parse_line(headers, col_sep: @options[:column_separator], row_sep: @options[:row_separator], quote_char: @options[:quote_character]) @use_headers = true when true @headers = nil @use_headers = true else @headers = nil @use_headers = false end return unless @headers converter = @options[:header_fields_converter] @headers = converter.convert(@headers, nil, 0) @headers.each do |header| header.freeze if header.is_a?(String) end end def prepare_format @column_separator = @options[:column_separator].to_s.encode(@encoding) row_separator = @options[:row_separator] if row_separator == :auto @row_separator = $INPUT_RECORD_SEPARATOR.encode(@encoding) else @row_separator = row_separator.to_s.encode(@encoding) end @quote_character = @options[:quote_character] @force_quotes = @options[:force_quotes] unless @force_quotes @quotable_pattern = Regexp.new("[\r\n".encode(@encoding) + Regexp.escape(@column_separator) + Regexp.escape(@quote_character.encode(@encoding)) + "]".encode(@encoding)) end @quote_empty = @options.fetch(:quote_empty, true) end def prepare_output @output_encoding = nil return unless @output.is_a?(StringIO) output_encoding = @output.internal_encoding || @output.external_encoding if @encoding != output_encoding if @options[:force_encoding] @output_encoding = output_encoding else compatible_encoding = Encoding.compatible?(@encoding, output_encoding) if compatible_encoding @output.set_encoding(compatible_encoding) @output.seek(0, IO::SEEK_END) end end end end def quote_field(field) field = String(field) encoded_quote_character = @quote_character.encode(field.encoding) encoded_quote_character + field.gsub(encoded_quote_character, encoded_quote_character * 2) + encoded_quote_character end def quote(field) if @force_quotes quote_field(field) else if field.nil? # represent +nil+ fields as empty unquoted fields "" else field = String(field) # Stringify fields # represent empty fields as empty quoted fields if (@quote_empty and field.empty?) or @quotable_pattern.match?(field) quote_field(field) else field # unquoted field end end end end end end csv-3.1.2/lib/csv/row.rb0000644000004100000410000002620313553655261015053 0ustar www-datawww-data# frozen_string_literal: true require "forwardable" class CSV # # A CSV::Row is part Array and part Hash. It retains an order for the fields # and allows duplicates just as an Array would, but also allows you to access # fields by name just as you could if they were in a Hash. # # All rows returned by CSV will be constructed from this class, if header row # processing is activated. # class Row # # Constructs a new CSV::Row from +headers+ and +fields+, which are expected # to be Arrays. If one Array is shorter than the other, it will be padded # with +nil+ objects. # # The optional +header_row+ parameter can be set to +true+ to indicate, via # CSV::Row.header_row?() and CSV::Row.field_row?(), that this is a header # row. Otherwise, the row assumes to be a field row. # # A CSV::Row object supports the following Array methods through delegation: # # * empty?() # * length() # * size() # def initialize(headers, fields, header_row = false) @header_row = header_row headers.each { |h| h.freeze if h.is_a? String } # handle extra headers or fields @row = if headers.size >= fields.size headers.zip(fields) else fields.zip(headers).each(&:reverse!) end end # Internal data format used to compare equality. attr_reader :row protected :row ### Array Delegation ### extend Forwardable def_delegators :@row, :empty?, :length, :size def initialize_copy(other) super @row = @row.dup end # Returns +true+ if this is a header row. def header_row? @header_row end # Returns +true+ if this is a field row. def field_row? not header_row? end # Returns the headers of this row. def headers @row.map(&:first) end # # :call-seq: # field( header ) # field( header, offset ) # field( index ) # # This method will return the field value by +header+ or +index+. If a field # is not found, +nil+ is returned. # # When provided, +offset+ ensures that a header match occurs on or later # than the +offset+ index. You can use this to find duplicate headers, # without resorting to hard-coding exact indices. # def field(header_or_index, minimum_index = 0) # locate the pair finder = (header_or_index.is_a?(Integer) || header_or_index.is_a?(Range)) ? :[] : :assoc pair = @row[minimum_index..-1].send(finder, header_or_index) # return the field if we have a pair if pair.nil? nil else header_or_index.is_a?(Range) ? pair.map(&:last) : pair.last end end alias_method :[], :field # # :call-seq: # fetch( header ) # fetch( header ) { |row| ... } # fetch( header, default ) # # This method will fetch the field value by +header+. It has the same # behavior as Hash#fetch: if there is a field with the given +header+, its # value is returned. Otherwise, if a block is given, it is yielded the # +header+ and its result is returned; if a +default+ is given as the # second argument, it is returned; otherwise a KeyError is raised. # def fetch(header, *varargs) raise ArgumentError, "Too many arguments" if varargs.length > 1 pair = @row.assoc(header) if pair pair.last else if block_given? yield header elsif varargs.empty? raise KeyError, "key not found: #{header}" else varargs.first end end end # Returns +true+ if there is a field with the given +header+. def has_key?(header) !!@row.assoc(header) end alias_method :include?, :has_key? alias_method :key?, :has_key? alias_method :member?, :has_key? alias_method :header?, :has_key? # # :call-seq: # []=( header, value ) # []=( header, offset, value ) # []=( index, value ) # # Looks up the field by the semantics described in CSV::Row.field() and # assigns the +value+. # # Assigning past the end of the row with an index will set all pairs between # to [nil, nil]. Assigning to an unused header appends the new # pair. # def []=(*args) value = args.pop if args.first.is_a? Integer if @row[args.first].nil? # extending past the end with index @row[args.first] = [nil, value] @row.map! { |pair| pair.nil? ? [nil, nil] : pair } else # normal index assignment @row[args.first][1] = value end else index = index(*args) if index.nil? # appending a field self << [args.first, value] else # normal header assignment @row[index][1] = value end end end # # :call-seq: # <<( field ) # <<( header_and_field_array ) # <<( header_and_field_hash ) # # If a two-element Array is provided, it is assumed to be a header and field # and the pair is appended. A Hash works the same way with the key being # the header and the value being the field. Anything else is assumed to be # a lone field which is appended with a +nil+ header. # # This method returns the row for chaining. # def <<(arg) if arg.is_a?(Array) and arg.size == 2 # appending a header and name @row << arg elsif arg.is_a?(Hash) # append header and name pairs arg.each { |pair| @row << pair } else # append field value @row << [nil, arg] end self # for chaining end # # A shortcut for appending multiple fields. Equivalent to: # # args.each { |arg| csv_row << arg } # # This method returns the row for chaining. # def push(*args) args.each { |arg| self << arg } self # for chaining end # # :call-seq: # delete( header ) # delete( header, offset ) # delete( index ) # # Removes a pair from the row by +header+ or +index+. The pair is # located as described in CSV::Row.field(). The deleted pair is returned, # or +nil+ if a pair could not be found. # def delete(header_or_index, minimum_index = 0) if header_or_index.is_a? Integer # by index @row.delete_at(header_or_index) elsif i = index(header_or_index, minimum_index) # by header @row.delete_at(i) else [ ] end end # # The provided +block+ is passed a header and field for each pair in the row # and expected to return +true+ or +false+, depending on whether the pair # should be deleted. # # This method returns the row for chaining. # # If no block is given, an Enumerator is returned. # def delete_if(&block) return enum_for(__method__) { size } unless block_given? @row.delete_if(&block) self # for chaining end # # This method accepts any number of arguments which can be headers, indices, # Ranges of either, or two-element Arrays containing a header and offset. # Each argument will be replaced with a field lookup as described in # CSV::Row.field(). # # If called with no arguments, all fields are returned. # def fields(*headers_and_or_indices) if headers_and_or_indices.empty? # return all fields--no arguments @row.map(&:last) else # or work like values_at() all = [] headers_and_or_indices.each do |h_or_i| if h_or_i.is_a? Range index_begin = h_or_i.begin.is_a?(Integer) ? h_or_i.begin : index(h_or_i.begin) index_end = h_or_i.end.is_a?(Integer) ? h_or_i.end : index(h_or_i.end) new_range = h_or_i.exclude_end? ? (index_begin...index_end) : (index_begin..index_end) all.concat(fields.values_at(new_range)) else all << field(*Array(h_or_i)) end end return all end end alias_method :values_at, :fields # # :call-seq: # index( header ) # index( header, offset ) # # This method will return the index of a field with the provided +header+. # The +offset+ can be used to locate duplicate header names, as described in # CSV::Row.field(). # def index(header, minimum_index = 0) # find the pair index = headers[minimum_index..-1].index(header) # return the index at the right offset, if we found one index.nil? ? nil : index + minimum_index end # # Returns +true+ if +data+ matches a field in this row, and +false+ # otherwise. # def field?(data) fields.include? data end include Enumerable # # Yields each pair of the row as header and field tuples (much like # iterating over a Hash). This method returns the row for chaining. # # If no block is given, an Enumerator is returned. # # Support for Enumerable. # def each(&block) return enum_for(__method__) { size } unless block_given? @row.each(&block) self # for chaining end alias_method :each_pair, :each # # Returns +true+ if this row contains the same headers and fields in the # same order as +other+. # def ==(other) return @row == other.row if other.is_a? CSV::Row @row == other end # # Collapses the row into a simple Hash. Be warned that this discards field # order and clobbers duplicate fields. # def to_h hash = {} each do |key, _value| hash[key] = self[key] unless hash.key?(key) end hash end alias_method :to_hash, :to_h alias_method :to_ary, :to_a # # Returns the row as a CSV String. Headers are not used. Equivalent to: # # csv_row.fields.to_csv( options ) # def to_csv(**options) fields.to_csv(**options) end alias_method :to_s, :to_csv # # Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step, # returning nil if any intermediate step is nil. # def dig(index_or_header, *indexes) value = field(index_or_header) if value.nil? nil elsif indexes.empty? value else unless value.respond_to?(:dig) raise TypeError, "#{value.class} does not have \#dig method" end value.dig(*indexes) end end # # A summary of fields, by header, in an ASCII compatible String. # def inspect str = ["#<", self.class.to_s] each do |header, field| str << " " << (header.is_a?(Symbol) ? header.to_s : header.inspect) << ":" << field.inspect end str << ">" begin str.join('') rescue # any encoding error str.map do |s| e = Encoding::Converter.asciicompat_encoding(s.encoding) e ? s.encode(e) : s.force_encoding("ASCII-8BIT") end.join('') end end end end csv-3.1.2/lib/csv/table.rb0000644000004100000410000003131613553655261015334 0ustar www-datawww-data# frozen_string_literal: true require "forwardable" class CSV # # A CSV::Table is a two-dimensional data structure for representing CSV # documents. Tables allow you to work with the data by row or column, # manipulate the data, and even convert the results back to CSV, if needed. # # All tables returned by CSV will be constructed from this class, if header # row processing is activated. # class Table # # Constructs a new CSV::Table from +array_of_rows+, which are expected # to be CSV::Row objects. All rows are assumed to have the same headers. # # The optional +headers+ parameter can be set to Array of headers. # If headers aren't set, headers are fetched from CSV::Row objects. # Otherwise, headers() method will return headers being set in # headers argument. # # A CSV::Table object supports the following Array methods through # delegation: # # * empty?() # * length() # * size() # def initialize(array_of_rows, headers: nil) @table = array_of_rows @headers = headers unless @headers if @table.empty? @headers = [] else @headers = @table.first.headers end end @mode = :col_or_row end # The current access mode for indexing and iteration. attr_reader :mode # Internal data format used to compare equality. attr_reader :table protected :table ### Array Delegation ### extend Forwardable def_delegators :@table, :empty?, :length, :size # # Returns a duplicate table object, in column mode. This is handy for # chaining in a single call without changing the table mode, but be aware # that this method can consume a fair amount of memory for bigger data sets. # # This method returns the duplicate table for chaining. Don't chain # destructive methods (like []=()) this way though, since you are working # with a duplicate. # def by_col self.class.new(@table.dup).by_col! end # # Switches the mode of this table to column mode. All calls to indexing and # iteration methods will work with columns until the mode is changed again. # # This method returns the table and is safe to chain. # def by_col! @mode = :col self end # # Returns a duplicate table object, in mixed mode. This is handy for # chaining in a single call without changing the table mode, but be aware # that this method can consume a fair amount of memory for bigger data sets. # # This method returns the duplicate table for chaining. Don't chain # destructive methods (like []=()) this way though, since you are working # with a duplicate. # def by_col_or_row self.class.new(@table.dup).by_col_or_row! end # # Switches the mode of this table to mixed mode. All calls to indexing and # iteration methods will use the default intelligent indexing system until # the mode is changed again. In mixed mode an index is assumed to be a row # reference while anything else is assumed to be column access by headers. # # This method returns the table and is safe to chain. # def by_col_or_row! @mode = :col_or_row self end # # Returns a duplicate table object, in row mode. This is handy for chaining # in a single call without changing the table mode, but be aware that this # method can consume a fair amount of memory for bigger data sets. # # This method returns the duplicate table for chaining. Don't chain # destructive methods (like []=()) this way though, since you are working # with a duplicate. # def by_row self.class.new(@table.dup).by_row! end # # Switches the mode of this table to row mode. All calls to indexing and # iteration methods will work with rows until the mode is changed again. # # This method returns the table and is safe to chain. # def by_row! @mode = :row self end # # Returns the headers for the first row of this table (assumed to match all # other rows). The headers Array passed to CSV::Table.new is returned for # empty tables. # def headers if @table.empty? @headers.dup else @table.first.headers end end # # In the default mixed mode, this method returns rows for index access and # columns for header access. You can force the index association by first # calling by_col!() or by_row!(). # # Columns are returned as an Array of values. Altering that Array has no # effect on the table. # def [](index_or_header) if @mode == :row or # by index (@mode == :col_or_row and (index_or_header.is_a?(Integer) or index_or_header.is_a?(Range))) @table[index_or_header] else # by header @table.map { |row| row[index_or_header] } end end # # In the default mixed mode, this method assigns rows for index access and # columns for header access. You can force the index association by first # calling by_col!() or by_row!(). # # Rows may be set to an Array of values (which will inherit the table's # headers()) or a CSV::Row. # # Columns may be set to a single value, which is copied to each row of the # column, or an Array of values. Arrays of values are assigned to rows top # to bottom in row major order. Excess values are ignored and if the Array # does not have a value for each row the extra rows will receive a +nil+. # # Assigning to an existing column or row clobbers the data. Assigning to # new columns creates them at the right end of the table. # def []=(index_or_header, value) if @mode == :row or # by index (@mode == :col_or_row and index_or_header.is_a? Integer) if value.is_a? Array @table[index_or_header] = Row.new(headers, value) else @table[index_or_header] = value end else # set column unless index_or_header.is_a? Integer index = @headers.index(index_or_header) || @headers.size @headers[index] = index_or_header end if value.is_a? Array # multiple values @table.each_with_index do |row, i| if row.header_row? row[index_or_header] = index_or_header else row[index_or_header] = value[i] end end else # repeated value @table.each do |row| if row.header_row? row[index_or_header] = index_or_header else row[index_or_header] = value end end end end end # # The mixed mode default is to treat a list of indices as row access, # returning the rows indicated. Anything else is considered columnar # access. For columnar access, the return set has an Array for each row # with the values indicated by the headers in each Array. You can force # column or row mode using by_col!() or by_row!(). # # You cannot mix column and row access. # def values_at(*indices_or_headers) if @mode == :row or # by indices ( @mode == :col_or_row and indices_or_headers.all? do |index| index.is_a?(Integer) or ( index.is_a?(Range) and index.first.is_a?(Integer) and index.last.is_a?(Integer) ) end ) @table.values_at(*indices_or_headers) else # by headers @table.map { |row| row.values_at(*indices_or_headers) } end end # # Adds a new row to the bottom end of this table. You can provide an Array, # which will be converted to a CSV::Row (inheriting the table's headers()), # or a CSV::Row. # # This method returns the table for chaining. # def <<(row_or_array) if row_or_array.is_a? Array # append Array @table << Row.new(headers, row_or_array) else # append Row @table << row_or_array end self # for chaining end # # A shortcut for appending multiple rows. Equivalent to: # # rows.each { |row| self << row } # # This method returns the table for chaining. # def push(*rows) rows.each { |row| self << row } self # for chaining end # # Removes and returns the indicated columns or rows. In the default mixed # mode indices refer to rows and everything else is assumed to be a column # headers. Use by_col!() or by_row!() to force the lookup. # def delete(*indexes_or_headers) if indexes_or_headers.empty? raise ArgumentError, "wrong number of arguments (given 0, expected 1+)" end deleted_values = indexes_or_headers.map do |index_or_header| if @mode == :row or # by index (@mode == :col_or_row and index_or_header.is_a? Integer) @table.delete_at(index_or_header) else # by header if index_or_header.is_a? Integer @headers.delete_at(index_or_header) else @headers.delete(index_or_header) end @table.map { |row| row.delete(index_or_header).last } end end if indexes_or_headers.size == 1 deleted_values[0] else deleted_values end end # # Removes any column or row for which the block returns +true+. In the # default mixed mode or row mode, iteration is the standard row major # walking of rows. In column mode, iteration will +yield+ two element # tuples containing the column name and an Array of values for that column. # # This method returns the table for chaining. # # If no block is given, an Enumerator is returned. # def delete_if(&block) return enum_for(__method__) { @mode == :row or @mode == :col_or_row ? size : headers.size } unless block_given? if @mode == :row or @mode == :col_or_row # by index @table.delete_if(&block) else # by header deleted = [] headers.each do |header| deleted << delete(header) if yield([header, self[header]]) end end self # for chaining end include Enumerable # # In the default mixed mode or row mode, iteration is the standard row major # walking of rows. In column mode, iteration will +yield+ two element # tuples containing the column name and an Array of values for that column. # # This method returns the table for chaining. # # If no block is given, an Enumerator is returned. # def each(&block) return enum_for(__method__) { @mode == :col ? headers.size : size } unless block_given? if @mode == :col headers.each { |header| yield([header, self[header]]) } else @table.each(&block) end self # for chaining end # Returns +true+ if all rows of this table ==() +other+'s rows. def ==(other) return @table == other.table if other.is_a? CSV::Table @table == other end # # Returns the table as an Array of Arrays. Headers will be the first row, # then all of the field rows will follow. # def to_a array = [headers] @table.each do |row| array.push(row.fields) unless row.header_row? end array end # # Returns the table as a complete CSV String. Headers will be listed first, # then all of the field rows. # # This method assumes you want the Table.headers(), unless you explicitly # pass :write_headers => false. # def to_csv(write_headers: true, **options) array = write_headers ? [headers.to_csv(**options)] : [] @table.each do |row| array.push(row.fields.to_csv(**options)) unless row.header_row? end array.join("") end alias_method :to_s, :to_csv # # Extracts the nested value specified by the sequence of +index+ or +header+ objects by calling dig at each step, # returning nil if any intermediate step is nil. # def dig(index_or_header, *index_or_headers) value = self[index_or_header] if value.nil? nil elsif index_or_headers.empty? value else unless value.respond_to?(:dig) raise TypeError, "#{value.class} does not have \#dig method" end value.dig(*index_or_headers) end end # Shows the mode and size of this table in a US-ASCII String. def inspect "#<#{self.class} mode:#{@mode} row_count:#{to_a.size}>".encode("US-ASCII") end end end csv-3.1.2/lib/csv/parser.rb0000644000004100000410000007576213553655261015556 0ustar www-datawww-data# frozen_string_literal: true require "strscan" require_relative "delete_suffix" require_relative "match_p" require_relative "row" require_relative "table" using CSV::DeleteSuffix if CSV.const_defined?(:DeleteSuffix) using CSV::MatchP if CSV.const_defined?(:MatchP) class CSV # Note: Don't use this class directly. This is an internal class. class Parser # # A CSV::Parser is m17n aware. The parser works in the Encoding of the IO # or String object being read from or written to. Your data is never transcoded # (unless you ask Ruby to transcode it for you) and will literally be parsed in # the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the # Encoding of your data. This is accomplished by transcoding the parser itself # into your Encoding. # # Raised when encoding is invalid. class InvalidEncoding < StandardError end # # CSV::Scanner receives a CSV output, scans it and return the content. # It also controls the life cycle of the object with its methods +keep_start+, # +keep_end+, +keep_back+, +keep_drop+. # # Uses StringScanner (the official strscan gem). Strscan provides lexical # scanning operations on a String. We inherit its object and take advantage # on the methods. For more information, please visit: # https://ruby-doc.org/stdlib-2.6.1/libdoc/strscan/rdoc/StringScanner.html # class Scanner < StringScanner alias_method :scan_all, :scan def initialize(*args) super @keeps = [] end def each_line(row_separator) position = pos rest.each_line(row_separator) do |line| position += line.bytesize self.pos = position yield(line) end end def keep_start @keeps.push(pos) end def keep_end start = @keeps.pop string.byteslice(start, pos - start) end def keep_back self.pos = @keeps.pop end def keep_drop @keeps.pop end end # # CSV::InputsScanner receives IO inputs, encoding and the chunk_size. # It also controls the life cycle of the object with its methods +keep_start+, # +keep_end+, +keep_back+, +keep_drop+. # # CSV::InputsScanner.scan() tries to match with pattern at the current position. # If there's a match, the scanner advances the “scan pointer” and returns the matched string. # Otherwise, the scanner returns nil. # # CSV::InputsScanner.rest() returns the “rest” of the string (i.e. everything after the scan pointer). # If there is no more data (eos? = true), it returns "". # class InputsScanner def initialize(inputs, encoding, chunk_size: 8192) @inputs = inputs.dup @encoding = encoding @chunk_size = chunk_size @last_scanner = @inputs.empty? @keeps = [] read_chunk end def each_line(row_separator) buffer = nil input = @scanner.rest position = @scanner.pos offset = 0 n_row_separator_chars = row_separator.size while true input.each_line(row_separator) do |line| @scanner.pos += line.bytesize if buffer if n_row_separator_chars == 2 and buffer.end_with?(row_separator[0]) and line.start_with?(row_separator[1]) buffer << line[0] line = line[1..-1] position += buffer.bytesize + offset @scanner.pos = position offset = 0 yield(buffer) buffer = nil next if line.empty? else buffer << line line = buffer buffer = nil end end if line.end_with?(row_separator) position += line.bytesize + offset @scanner.pos = position offset = 0 yield(line) else buffer = line end end break unless read_chunk input = @scanner.rest position = @scanner.pos offset = -buffer.bytesize if buffer end yield(buffer) if buffer end def scan(pattern) value = @scanner.scan(pattern) return value if @last_scanner if value read_chunk if @scanner.eos? return value else nil end end def scan_all(pattern) value = @scanner.scan(pattern) return value if @last_scanner return nil if value.nil? while @scanner.eos? and read_chunk and (sub_value = @scanner.scan(pattern)) value << sub_value end value end def eos? @scanner.eos? end def keep_start @keeps.push([@scanner.pos, nil]) end def keep_end start, buffer = @keeps.pop keep = @scanner.string.byteslice(start, @scanner.pos - start) if buffer buffer << keep keep = buffer end keep end def keep_back start, buffer = @keeps.pop if buffer string = @scanner.string keep = string.byteslice(start, string.bytesize - start) if keep and not keep.empty? @inputs.unshift(StringIO.new(keep)) @last_scanner = false end @scanner = StringScanner.new(buffer) else @scanner.pos = start end read_chunk if @scanner.eos? end def keep_drop @keeps.pop end def rest @scanner.rest end private def read_chunk return false if @last_scanner unless @keeps.empty? keep = @keeps.last keep_start = keep[0] string = @scanner.string keep_data = string.byteslice(keep_start, @scanner.pos - keep_start) if keep_data keep_buffer = keep[1] if keep_buffer keep_buffer << keep_data else keep[1] = keep_data.dup end end keep[0] = 0 end input = @inputs.first case input when StringIO string = input.read raise InvalidEncoding unless string.valid_encoding? @scanner = StringScanner.new(string) @inputs.shift @last_scanner = @inputs.empty? true else chunk = input.gets(nil, @chunk_size) if chunk raise InvalidEncoding unless chunk.valid_encoding? @scanner = StringScanner.new(chunk) if input.respond_to?(:eof?) and input.eof? @inputs.shift @last_scanner = @inputs.empty? end true else @scanner = StringScanner.new("".encode(@encoding)) @inputs.shift @last_scanner = @inputs.empty? if @last_scanner false else read_chunk end end end end end def initialize(input, options) @input = input @options = options @samples = [] prepare end def column_separator @column_separator end def row_separator @row_separator end def quote_character @quote_character end def field_size_limit @field_size_limit end def skip_lines @skip_lines end def unconverted_fields? @unconverted_fields end def headers @headers end def header_row? @use_headers and @headers.nil? end def return_headers? @return_headers end def skip_blanks? @skip_blanks end def liberal_parsing? @liberal_parsing end def lineno @lineno end def line last_line end def parse(&block) return to_enum(__method__) unless block_given? if @return_headers and @headers and @raw_headers headers = Row.new(@headers, @raw_headers, true) if @unconverted_fields headers = add_unconverted_fields(headers, []) end yield headers end begin @scanner ||= build_scanner if quote_character.nil? parse_no_quote(&block) elsif @need_robust_parsing parse_quotable_robust(&block) else parse_quotable_loose(&block) end rescue InvalidEncoding if @scanner ignore_broken_line lineno = @lineno else lineno = @lineno + 1 end message = "Invalid byte sequence in #{@encoding}" raise MalformedCSVError.new(message, lineno) end end def use_headers? @use_headers end private # A set of tasks to prepare the file in order to parse it def prepare prepare_variable prepare_quote_character prepare_backslash prepare_skip_lines prepare_strip prepare_separators prepare_quoted prepare_unquoted prepare_line prepare_header prepare_parser end def prepare_variable @need_robust_parsing = false @encoding = @options[:encoding] liberal_parsing = @options[:liberal_parsing] if liberal_parsing @liberal_parsing = true if liberal_parsing.is_a?(Hash) @double_quote_outside_quote = liberal_parsing[:double_quote_outside_quote] @backslash_quote = liberal_parsing[:backslash_quote] else @double_quote_outside_quote = false @backslash_quote = false end @need_robust_parsing = true else @liberal_parsing = false @backslash_quote = false end @unconverted_fields = @options[:unconverted_fields] @field_size_limit = @options[:field_size_limit] @skip_blanks = @options[:skip_blanks] @fields_converter = @options[:fields_converter] @header_fields_converter = @options[:header_fields_converter] end def prepare_quote_character @quote_character = @options[:quote_character] if @quote_character.nil? @escaped_quote_character = nil @escaped_quote = nil else @quote_character = @quote_character.to_s.encode(@encoding) if @quote_character.length != 1 message = ":quote_char has to be nil or a single character String" raise ArgumentError, message end @double_quote_character = @quote_character * 2 @escaped_quote_character = Regexp.escape(@quote_character) @escaped_quote = Regexp.new(@escaped_quote_character) end end def prepare_backslash return unless @backslash_quote @backslash_character = "\\".encode(@encoding) @escaped_backslash_character = Regexp.escape(@backslash_character) @escaped_backslash = Regexp.new(@escaped_backslash_character) if @quote_character.nil? @backslash_quote_character = nil else @backslash_quote_character = @backslash_character + @escaped_quote_character end end def prepare_skip_lines skip_lines = @options[:skip_lines] case skip_lines when String @skip_lines = skip_lines.encode(@encoding) when Regexp, nil @skip_lines = skip_lines else unless skip_lines.respond_to?(:match) message = ":skip_lines has to respond to \#match: #{skip_lines.inspect}" raise ArgumentError, message end @skip_lines = skip_lines end end def prepare_strip @strip = @options[:strip] @escaped_strip = nil @strip_value = nil if @strip.is_a?(String) case @strip.length when 0 raise ArgumentError, ":strip must not be an empty String" when 1 # ok else raise ArgumentError, ":strip doesn't support 2 or more characters yet" end @strip = @strip.encode(@encoding) @escaped_strip = Regexp.escape(@strip) if @quote_character @strip_value = Regexp.new(@escaped_strip + "+".encode(@encoding)) end @need_robust_parsing = true elsif @strip strip_values = " \t\f\v" @escaped_strip = strip_values.encode(@encoding) if @quote_character @strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding)) end @need_robust_parsing = true end end begin StringScanner.new("x").scan("x") rescue TypeError @@string_scanner_scan_accept_string = false else @@string_scanner_scan_accept_string = true end def prepare_separators column_separator = @options[:column_separator] @column_separator = column_separator.to_s.encode(@encoding) if @column_separator.size < 1 message = ":col_sep must be 1 or more characters: " message += column_separator.inspect raise ArgumentError, message end @row_separator = resolve_row_separator(@options[:row_separator]).encode(@encoding) @escaped_column_separator = Regexp.escape(@column_separator) @escaped_first_column_separator = Regexp.escape(@column_separator[0]) if @column_separator.size > 1 @column_end = Regexp.new(@escaped_column_separator) @column_ends = @column_separator.each_char.collect do |char| Regexp.new(Regexp.escape(char)) end @first_column_separators = Regexp.new(@escaped_first_column_separator + "+".encode(@encoding)) else if @@string_scanner_scan_accept_string @column_end = @column_separator else @column_end = Regexp.new(@escaped_column_separator) end @column_ends = nil @first_column_separators = nil end escaped_row_separator = Regexp.escape(@row_separator) @row_end = Regexp.new(escaped_row_separator) if @row_separator.size > 1 @row_ends = @row_separator.each_char.collect do |char| Regexp.new(Regexp.escape(char)) end else @row_ends = nil end @cr = "\r".encode(@encoding) @lf = "\n".encode(@encoding) @cr_or_lf = Regexp.new("[\r\n]".encode(@encoding)) @not_line_end = Regexp.new("[^\r\n]+".encode(@encoding)) end def prepare_quoted if @quote_character @quotes = Regexp.new(@escaped_quote_character + "+".encode(@encoding)) no_quoted_values = @escaped_quote_character.dup if @backslash_quote no_quoted_values << @escaped_backslash_character end @quoted_value = Regexp.new("[^".encode(@encoding) + no_quoted_values + "]+".encode(@encoding)) end if @escaped_strip @split_column_separator = Regexp.new(@escaped_strip + "*".encode(@encoding) + @escaped_column_separator + @escaped_strip + "*".encode(@encoding)) else if @column_separator == " ".encode(@encoding) @split_column_separator = Regexp.new(@escaped_column_separator) else @split_column_separator = @column_separator end end end def prepare_unquoted return if @quote_character.nil? no_unquoted_values = "\r\n".encode(@encoding) no_unquoted_values << @escaped_first_column_separator unless @liberal_parsing no_unquoted_values << @escaped_quote_character end if @escaped_strip no_unquoted_values << @escaped_strip end @unquoted_value = Regexp.new("[^".encode(@encoding) + no_unquoted_values + "]+".encode(@encoding)) end def resolve_row_separator(separator) if separator == :auto cr = "\r".encode(@encoding) lf = "\n".encode(@encoding) if @input.is_a?(StringIO) pos = @input.pos separator = detect_row_separator(@input.read, cr, lf) @input.seek(pos) elsif @input.respond_to?(:gets) if @input.is_a?(File) chunk_size = 32 * 1024 else chunk_size = 1024 end begin while separator == :auto # # if we run out of data, it's probably a single line # (ensure will set default value) # break unless sample = @input.gets(nil, chunk_size) # extend sample if we're unsure of the line ending if sample.end_with?(cr) sample << (@input.gets(nil, 1) || "") end @samples << sample separator = detect_row_separator(sample, cr, lf) end rescue IOError # do nothing: ensure will set default end end separator = $INPUT_RECORD_SEPARATOR if separator == :auto end separator.to_s.encode(@encoding) end def detect_row_separator(sample, cr, lf) lf_index = sample.index(lf) if lf_index cr_index = sample[0, lf_index].index(cr) else cr_index = sample.index(cr) end if cr_index and lf_index if cr_index + 1 == lf_index cr + lf elsif cr_index < lf_index cr else lf end elsif cr_index cr elsif lf_index lf else :auto end end def prepare_line @lineno = 0 @last_line = nil @scanner = nil end def last_line if @scanner @last_line ||= @scanner.keep_end else @last_line end end def prepare_header @return_headers = @options[:return_headers] headers = @options[:headers] case headers when Array @raw_headers = headers @use_headers = true when String @raw_headers = parse_headers(headers) @use_headers = true when nil, false @raw_headers = nil @use_headers = false else @raw_headers = nil @use_headers = true end if @raw_headers @headers = adjust_headers(@raw_headers) else @headers = nil end end def parse_headers(row) CSV.parse_line(row, col_sep: @column_separator, row_sep: @row_separator, quote_char: @quote_character) end def adjust_headers(headers) adjusted_headers = @header_fields_converter.convert(headers, nil, @lineno) adjusted_headers.each {|h| h.freeze if h.is_a? String} adjusted_headers end def prepare_parser @may_quoted = may_quoted? end def may_quoted? return false if @quote_character.nil? if @input.is_a?(StringIO) pos = @input.pos sample = @input.read @input.seek(pos) else return false if @samples.empty? sample = @samples.first end sample[0, 128].index(@quote_character) end SCANNER_TEST = (ENV["CSV_PARSER_SCANNER_TEST"] == "yes") if SCANNER_TEST class UnoptimizedStringIO def initialize(string) @io = StringIO.new(string) end def gets(*args) @io.gets(*args) end def each_line(*args, &block) @io.each_line(*args, &block) end def eof? @io.eof? end end def build_scanner inputs = @samples.collect do |sample| UnoptimizedStringIO.new(sample) end if @input.is_a?(StringIO) inputs << UnoptimizedStringIO.new(@input.read) else inputs << @input end chunk_size = ENV["CSV_PARSER_SCANNER_TEST_CHUNK_SIZE"] || "1" InputsScanner.new(inputs, @encoding, chunk_size: Integer(chunk_size, 10)) end else def build_scanner string = nil if @samples.empty? and @input.is_a?(StringIO) string = @input.read elsif @samples.size == 1 and @input.respond_to?(:eof?) and @input.eof? string = @samples[0] end if string unless string.valid_encoding? index = string.lines(@row_separator).index do |line| !line.valid_encoding? end if index message = "Invalid byte sequence in #{@encoding}" raise MalformedCSVError.new(message, @lineno + index + 1) end end Scanner.new(string) else inputs = @samples.collect do |sample| StringIO.new(sample) end inputs << @input InputsScanner.new(inputs, @encoding) end end end def skip_needless_lines return unless @skip_lines while true @scanner.keep_start line = @scanner.scan_all(@not_line_end) || "".encode(@encoding) line << @row_separator if parse_row_end if skip_line?(line) @lineno += 1 @scanner.keep_drop else @scanner.keep_back return end end end def skip_line?(line) case @skip_lines when String line.include?(@skip_lines) when Regexp @skip_lines.match?(line) else @skip_lines.match(line) end end def parse_no_quote(&block) @scanner.each_line(@row_separator) do |line| next if @skip_lines and skip_line?(line) original_line = line line = line.delete_suffix(@row_separator) if line.empty? next if @skip_blanks row = [] else line = strip_value(line) row = line.split(@split_column_separator, -1) n_columns = row.size i = 0 while i < n_columns row[i] = nil if row[i].empty? i += 1 end end @last_line = original_line emit_row(row, &block) end end def parse_quotable_loose(&block) @scanner.keep_start @scanner.each_line(@row_separator) do |line| if @skip_lines and skip_line?(line) @scanner.keep_drop @scanner.keep_start next end original_line = line line = line.delete_suffix(@row_separator) if line.empty? if @skip_blanks @scanner.keep_drop @scanner.keep_start next end row = [] elsif line.include?(@cr) or line.include?(@lf) @scanner.keep_back @need_robust_parsing = true return parse_quotable_robust(&block) else row = line.split(@split_column_separator, -1) n_columns = row.size i = 0 while i < n_columns column = row[i] if column.empty? row[i] = nil else n_quotes = column.count(@quote_character) if n_quotes.zero? # no quote elsif n_quotes == 2 and column.start_with?(@quote_character) and column.end_with?(@quote_character) row[i] = column[1..-2] else @scanner.keep_back @need_robust_parsing = true return parse_quotable_robust(&block) end end i += 1 end end @scanner.keep_drop @scanner.keep_start @last_line = original_line emit_row(row, &block) end @scanner.keep_drop end def parse_quotable_robust(&block) row = [] skip_needless_lines start_row while true @quoted_column_value = false @unquoted_column_value = false @scanner.scan_all(@strip_value) if @strip_value value = parse_column_value if value @scanner.scan_all(@strip_value) if @strip_value if @field_size_limit and value.size >= @field_size_limit ignore_broken_line raise MalformedCSVError.new("Field size exceeded", @lineno) end end if parse_column_end row << value elsif parse_row_end if row.empty? and value.nil? emit_row([], &block) unless @skip_blanks else row << value emit_row(row, &block) row = [] end skip_needless_lines start_row elsif @scanner.eos? break if row.empty? and value.nil? row << value emit_row(row, &block) break else if @quoted_column_value ignore_broken_line message = "Any value after quoted field isn't allowed" raise MalformedCSVError.new(message, @lineno) elsif @unquoted_column_value and (new_line = @scanner.scan(@cr_or_lf)) ignore_broken_line message = "Unquoted fields do not allow new line " + "<#{new_line.inspect}>" raise MalformedCSVError.new(message, @lineno) elsif @scanner.rest.start_with?(@quote_character) ignore_broken_line message = "Illegal quoting" raise MalformedCSVError.new(message, @lineno) elsif (new_line = @scanner.scan(@cr_or_lf)) ignore_broken_line message = "New line must be <#{@row_separator.inspect}> " + "not <#{new_line.inspect}>" raise MalformedCSVError.new(message, @lineno) else ignore_broken_line raise MalformedCSVError.new("TODO: Meaningful message", @lineno) end end end end def parse_column_value if @liberal_parsing quoted_value = parse_quoted_column_value if quoted_value unquoted_value = parse_unquoted_column_value if unquoted_value if @double_quote_outside_quote unquoted_value = unquoted_value.gsub(@quote_character * 2, @quote_character) if quoted_value.empty? # %Q{""...} case return @quote_character + unquoted_value end end @quote_character + quoted_value + @quote_character + unquoted_value else quoted_value end else parse_unquoted_column_value end elsif @may_quoted parse_quoted_column_value || parse_unquoted_column_value else parse_unquoted_column_value || parse_quoted_column_value end end def parse_unquoted_column_value value = @scanner.scan_all(@unquoted_value) return nil unless value @unquoted_column_value = true if @first_column_separators while true @scanner.keep_start is_column_end = @column_ends.all? do |column_end| @scanner.scan(column_end) end @scanner.keep_back break if is_column_end sub_separator = @scanner.scan_all(@first_column_separators) break if sub_separator.nil? value << sub_separator sub_value = @scanner.scan_all(@unquoted_value) break if sub_value.nil? value << sub_value end end value.gsub!(@backslash_quote_character, @quote_character) if @backslash_quote value end def parse_quoted_column_value quotes = @scanner.scan_all(@quotes) return nil unless quotes @quoted_column_value = true n_quotes = quotes.size if (n_quotes % 2).zero? quotes[0, (n_quotes - 2) / 2] else value = quotes[0, (n_quotes - 1) / 2] while true quoted_value = @scanner.scan_all(@quoted_value) value << quoted_value if quoted_value if @backslash_quote if @scanner.scan(@escaped_backslash) if @scanner.scan(@escaped_quote) value << @quote_character else value << @backslash_character end next end end quotes = @scanner.scan_all(@quotes) unless quotes ignore_broken_line message = "Unclosed quoted field" raise MalformedCSVError.new(message, @lineno) end n_quotes = quotes.size if n_quotes == 1 break elsif (n_quotes % 2) == 1 value << quotes[0, (n_quotes - 1) / 2] break else value << quotes[0, n_quotes / 2] end end value end end def parse_column_end return true if @scanner.scan(@column_end) return false unless @column_ends @scanner.keep_start if @column_ends.all? {|column_end| @scanner.scan(column_end)} @scanner.keep_drop true else @scanner.keep_back false end end def parse_row_end return true if @scanner.scan(@row_end) return false unless @row_ends @scanner.keep_start if @row_ends.all? {|row_end| @scanner.scan(row_end)} @scanner.keep_drop true else @scanner.keep_back false end end def strip_value(value) return value unless @strip return nil if value.nil? case @strip when String size = value.size while value.start_with?(@strip) size -= 1 value = value[1, size] end while value.end_with?(@strip) size -= 1 value = value[0, size] end else value.strip! end value end def ignore_broken_line @scanner.scan_all(@not_line_end) @scanner.scan_all(@cr_or_lf) @lineno += 1 end def start_row if @last_line @last_line = nil else @scanner.keep_drop end @scanner.keep_start end def emit_row(row, &block) @lineno += 1 raw_row = row if @use_headers if @headers.nil? @headers = adjust_headers(row) return unless @return_headers row = Row.new(@headers, row, true) else row = Row.new(@headers, @fields_converter.convert(raw_row, @headers, @lineno)) end else # convert fields, if needed... row = @fields_converter.convert(raw_row, nil, @lineno) end # inject unconverted fields and accessor, if requested... if @unconverted_fields and not row.respond_to?(:unconverted_fields) add_unconverted_fields(row, raw_row) end yield(row) end # This method injects an instance variable unconverted_fields into # +row+ and an accessor method for +row+ called unconverted_fields(). The # variable is set to the contents of +fields+. def add_unconverted_fields(row, fields) class << row attr_reader :unconverted_fields end row.instance_variable_set(:@unconverted_fields, fields) row end end end csv-3.1.2/lib/csv.rb0000644000004100000410000015453313553655261014254 0ustar www-datawww-data# encoding: US-ASCII # frozen_string_literal: true # = csv.rb -- CSV Reading and Writing # # Created by James Edward Gray II on 2005-10-31. # # See CSV for documentation. # # == Description # # Welcome to the new and improved CSV. # # This version of the CSV library began its life as FasterCSV. FasterCSV was # intended as a replacement to Ruby's then standard CSV library. It was # designed to address concerns users of that library had and it had three # primary goals: # # 1. Be significantly faster than CSV while remaining a pure Ruby library. # 2. Use a smaller and easier to maintain code base. (FasterCSV eventually # grew larger, was also but considerably richer in features. The parsing # core remains quite small.) # 3. Improve on the CSV interface. # # Obviously, the last one is subjective. I did try to defer to the original # interface whenever I didn't have a compelling reason to change it though, so # hopefully this won't be too radically different. # # We must have met our goals because FasterCSV was renamed to CSV and replaced # the original library as of Ruby 1.9. If you are migrating code from 1.8 or # earlier, you may have to change your code to comply with the new interface. # # == What's the Different From the Old CSV? # # I'm sure I'll miss something, but I'll try to mention most of the major # differences I am aware of, to help others quickly get up to speed: # # === CSV Parsing # # * This parser is m17n aware. See CSV for full details. # * This library has a stricter parser and will throw MalformedCSVErrors on # problematic data. # * This library has a less liberal idea of a line ending than CSV. What you # set as the :row_sep is law. It can auto-detect your line endings # though. # * The old library returned empty lines as [nil]. This library calls # them []. # * This library has a much faster parser. # # === Interface # # * CSV now uses Hash-style parameters to set options. # * CSV no longer has generate_row() or parse_row(). # * The old CSV's Reader and Writer classes have been dropped. # * CSV::open() is now more like Ruby's open(). # * CSV objects now support most standard IO methods. # * CSV now has a new() method used to wrap objects like String and IO for # reading and writing. # * CSV::generate() is different from the old method. # * CSV no longer supports partial reads. It works line-by-line. # * CSV no longer allows the instance methods to override the separators for # performance reasons. They must be set in the constructor. # # If you use this library and find yourself missing any functionality I have # trimmed, please {let me know}[mailto:james@grayproductions.net]. # # == Documentation # # See CSV for documentation. # # == What is CSV, really? # # CSV maintains a pretty strict definition of CSV taken directly from # {the RFC}[http://www.ietf.org/rfc/rfc4180.txt]. I relax the rules in only one # place and that is to make using this library easier. CSV will parse all valid # CSV. # # What you don't want to do is to feed CSV invalid data. Because of the way the # CSV format works, it's common for a parser to need to read until the end of # the file to be sure a field is invalid. This consumes a lot of time and memory. # # Luckily, when working with invalid CSV, Ruby's built-in methods will almost # always be superior in every way. For example, parsing non-quoted fields is as # easy as: # # data.split(",") # # == Questions and/or Comments # # Feel free to email {James Edward Gray II}[mailto:james@grayproductions.net] # with any questions. require "forwardable" require "English" require "date" require "stringio" require_relative "csv/fields_converter" require_relative "csv/match_p" require_relative "csv/parser" require_relative "csv/row" require_relative "csv/table" require_relative "csv/writer" using CSV::MatchP if CSV.const_defined?(:MatchP) # # This class provides a complete interface to CSV files and data. It offers # tools to enable you to read and write to and from Strings or IO objects, as # needed. # # The most generic interface of the library is: # # csv = CSV.new(string_or_io, **options) # # # Reading: IO object should be open for read # csv.read # => array of rows # # or # csv.each do |row| # # ... # end # # or # row = csv.shift # # # Writing: IO object should be open for write # csv << row # # There are several specialized class methods for one-statement reading or writing, # described in the Specialized Methods section. # # If a String is passed into ::new, it is internally wrapped into a StringIO object. # # +options+ can be used for specifying the particular CSV flavor (column # separators, row separators, value quoting and so on), and for data conversion, # see Data Conversion section for the description of the latter. # # == Specialized Methods # # === Reading # # # From a file: all at once # arr_of_rows = CSV.read("path/to/file.csv", **options) # # iterator-style: # CSV.foreach("path/to/file.csv", **options) do |row| # # ... # end # # # From a string # arr_of_rows = CSV.parse("CSV,data,String", **options) # # or # CSV.parse("CSV,data,String", **options) do |row| # # ... # end # # === Writing # # # To a file # CSV.open("path/to/file.csv", "wb") do |csv| # csv << ["row", "of", "CSV", "data"] # csv << ["another", "row"] # # ... # end # # # To a String # csv_string = CSV.generate do |csv| # csv << ["row", "of", "CSV", "data"] # csv << ["another", "row"] # # ... # end # # === Shortcuts # # # Core extensions for converting one line # csv_string = ["CSV", "data"].to_csv # to CSV # csv_array = "CSV,String".parse_csv # from CSV # # # CSV() method # CSV { |csv_out| csv_out << %w{my data here} } # to $stdout # CSV(csv = "") { |csv_str| csv_str << %w{my data here} } # to a String # CSV($stderr) { |csv_err| csv_err << %w{my data here} } # to $stderr # CSV($stdin) { |csv_in| csv_in.each { |row| p row } } # from $stdin # # == Data Conversion # # === CSV with headers # # CSV allows to specify column names of CSV file, whether they are in data, or # provided separately. If headers are specified, reading methods return an instance # of CSV::Table, consisting of CSV::Row. # # # Headers are part of data # data = CSV.parse(<<~ROWS, headers: true) # Name,Department,Salary # Bob,Engineering,1000 # Jane,Sales,2000 # John,Management,5000 # ROWS # # data.class #=> CSV::Table # data.first #=> # # data.first.to_h #=> {"Name"=>"Bob", "Department"=>"Engineering", "Salary"=>"1000"} # # # Headers provided by developer # data = CSV.parse('Bob,Engineering,1000', headers: %i[name department salary]) # data.first #=> # # # === Typed data reading # # CSV allows to provide a set of data _converters_ e.g. transformations to try on input # data. Converter could be a symbol from CSV::Converters constant's keys, or lambda. # # # Without any converters: # CSV.parse('Bob,2018-03-01,100') # #=> [["Bob", "2018-03-01", "100"]] # # # With built-in converters: # CSV.parse('Bob,2018-03-01,100', converters: %i[numeric date]) # #=> [["Bob", #, 100]] # # # With custom converters: # CSV.parse('Bob,2018-03-01,100', converters: [->(v) { Time.parse(v) rescue v }]) # #=> [["Bob", 2018-03-01 00:00:00 +0200, "100"]] # # == CSV and Character Encodings (M17n or Multilingualization) # # This new CSV parser is m17n savvy. The parser works in the Encoding of the IO # or String object being read from or written to. Your data is never transcoded # (unless you ask Ruby to transcode it for you) and will literally be parsed in # the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the # Encoding of your data. This is accomplished by transcoding the parser itself # into your Encoding. # # Some transcoding must take place, of course, to accomplish this multiencoding # support. For example, :col_sep, :row_sep, and # :quote_char must be transcoded to match your data. Hopefully this # makes the entire process feel transparent, since CSV's defaults should just # magically work for your data. However, you can set these values manually in # the target Encoding to avoid the translation. # # It's also important to note that while all of CSV's core parser is now # Encoding agnostic, some features are not. For example, the built-in # converters will try to transcode data to UTF-8 before making conversions. # Again, you can provide custom converters that are aware of your Encodings to # avoid this translation. It's just too hard for me to support native # conversions in all of Ruby's Encodings. # # Anyway, the practical side of this is simple: make sure IO and String objects # passed into CSV have the proper Encoding set and everything should just work. # CSV methods that allow you to open IO objects (CSV::foreach(), CSV::open(), # CSV::read(), and CSV::readlines()) do allow you to specify the Encoding. # # One minor exception comes when generating CSV into a String with an Encoding # that is not ASCII compatible. There's no existing data for CSV to use to # prepare itself and thus you will probably need to manually specify the desired # Encoding for most of those cases. It will try to guess using the fields in a # row of output though, when using CSV::generate_line() or Array#to_csv(). # # I try to point out any other Encoding issues in the documentation of methods # as they come up. # # This has been tested to the best of my ability with all non-"dummy" Encodings # Ruby ships with. However, it is brave new code and may have some bugs. # Please feel free to {report}[mailto:james@grayproductions.net] any issues you # find with it. # class CSV # The error thrown when the parser encounters illegal CSV formatting. class MalformedCSVError < RuntimeError attr_reader :line_number alias_method :lineno, :line_number def initialize(message, line_number) @line_number = line_number super("#{message} in line #{line_number}.") end end # # A FieldInfo Struct contains details about a field's position in the data # source it was read from. CSV will pass this Struct to some blocks that make # decisions based on field structure. See CSV.convert_fields() for an # example. # # index:: The zero-based index of the field in its row. # line:: The line of the data source this row is from. # header:: The header for the column, when available. # FieldInfo = Struct.new(:index, :line, :header) # A Regexp used to find and convert some common Date formats. DateMatcher = / \A(?: (\w+,?\s+)?\w+\s+\d{1,2},?\s+\d{2,4} | \d{4}-\d{2}-\d{2} )\z /x # A Regexp used to find and convert some common DateTime formats. DateTimeMatcher = / \A(?: (\w+,?\s+)?\w+\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2},?\s+\d{2,4} | \d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2} | # ISO-8601 \d{4}-\d{2}-\d{2} (?:T\d{2}:\d{2}(?::\d{2}(?:\.\d+)?(?:[+-]\d{2}(?::\d{2})|Z)?)?)? )\z /x # The encoding used by all converters. ConverterEncoding = Encoding.find("UTF-8") # # This Hash holds the built-in converters of CSV that can be accessed by name. # You can select Converters with CSV.convert() or through the +options+ Hash # passed to CSV::new(). # # :integer:: Converts any field Integer() accepts. # :float:: Converts any field Float() accepts. # :numeric:: A combination of :integer # and :float. # :date:: Converts any field Date::parse() accepts. # :date_time:: Converts any field DateTime::parse() accepts. # :all:: All built-in converters. A combination of # :date_time and :numeric. # # All built-in converters transcode field data to UTF-8 before attempting a # conversion. If your data cannot be transcoded to UTF-8 the conversion will # fail and the field will remain unchanged. # # This Hash is intentionally left unfrozen and users should feel free to add # values to it that can be accessed by all CSV objects. # # To add a combo field, the value should be an Array of names. Combo fields # can be nested with other combo fields. # Converters = { integer: lambda { |f| Integer(f.encode(ConverterEncoding)) rescue f }, float: lambda { |f| Float(f.encode(ConverterEncoding)) rescue f }, numeric: [:integer, :float], date: lambda { |f| begin e = f.encode(ConverterEncoding) e.match?(DateMatcher) ? Date.parse(e) : f rescue # encoding conversion or date parse errors f end }, date_time: lambda { |f| begin e = f.encode(ConverterEncoding) e.match?(DateTimeMatcher) ? DateTime.parse(e) : f rescue # encoding conversion or date parse errors f end }, all: [:date_time, :numeric], } # # This Hash holds the built-in header converters of CSV that can be accessed # by name. You can select HeaderConverters with CSV.header_convert() or # through the +options+ Hash passed to CSV::new(). # # :downcase:: Calls downcase() on the header String. # :symbol:: Leading/trailing spaces are dropped, string is # downcased, remaining spaces are replaced with # underscores, non-word characters are dropped, # and finally to_sym() is called. # # All built-in header converters transcode header data to UTF-8 before # attempting a conversion. If your data cannot be transcoded to UTF-8 the # conversion will fail and the header will remain unchanged. # # This Hash is intentionally left unfrozen and users should feel free to add # values to it that can be accessed by all CSV objects. # # To add a combo field, the value should be an Array of names. Combo fields # can be nested with other combo fields. # HeaderConverters = { downcase: lambda { |h| h.encode(ConverterEncoding).downcase }, symbol: lambda { |h| h.encode(ConverterEncoding).downcase.gsub(/[^\s\w]+/, "").strip. gsub(/\s+/, "_").to_sym } } # # The options used when no overrides are given by calling code. They are: # # :col_sep:: "," # :row_sep:: :auto # :quote_char:: '"' # :field_size_limit:: +nil+ # :converters:: +nil+ # :unconverted_fields:: +nil+ # :headers:: +false+ # :return_headers:: +false+ # :header_converters:: +nil+ # :skip_blanks:: +false+ # :force_quotes:: +false+ # :skip_lines:: +nil+ # :liberal_parsing:: +false+ # :quote_empty:: +true+ # DEFAULT_OPTIONS = { col_sep: ",", row_sep: :auto, quote_char: '"', field_size_limit: nil, converters: nil, unconverted_fields: nil, headers: false, return_headers: false, header_converters: nil, skip_blanks: false, force_quotes: false, skip_lines: nil, liberal_parsing: false, quote_empty: true, }.freeze class << self # # This method will return a CSV instance, just like CSV::new(), but the # instance will be cached and returned for all future calls to this method for # the same +data+ object (tested by Object#object_id()) with the same # +options+. # # If a block is given, the instance is passed to the block and the return # value becomes the return value of the block. # def instance(data = $stdout, **options) # create a _signature_ for this method call, data object and options sig = [data.object_id] + options.values_at(*DEFAULT_OPTIONS.keys.sort_by { |sym| sym.to_s }) # fetch or create the instance for this signature @@instances ||= Hash.new instance = (@@instances[sig] ||= new(data, **options)) if block_given? yield instance # run block, if given, returning result else instance # or return the instance end end # # :call-seq: # filter( **options ) { |row| ... } # filter( input, **options ) { |row| ... } # filter( input, output, **options ) { |row| ... } # # This method is a convenience for building Unix-like filters for CSV data. # Each row is yielded to the provided block which can alter it as needed. # After the block returns, the row is appended to +output+ altered or not. # # The +input+ and +output+ arguments can be anything CSV::new() accepts # (generally String or IO objects). If not given, they default to # ARGF and $stdout. # # The +options+ parameter is also filtered down to CSV::new() after some # clever key parsing. Any key beginning with :in_ or # :input_ will have that leading identifier stripped and will only # be used in the +options+ Hash for the +input+ object. Keys starting with # :out_ or :output_ affect only +output+. All other keys # are assigned to both objects. # # The :output_row_sep +option+ defaults to # $INPUT_RECORD_SEPARATOR ($/). # def filter(input=nil, output=nil, **options) # parse options for input, output, or both in_options, out_options = Hash.new, {row_sep: $INPUT_RECORD_SEPARATOR} options.each do |key, value| case key.to_s when /\Ain(?:put)?_(.+)\Z/ in_options[$1.to_sym] = value when /\Aout(?:put)?_(.+)\Z/ out_options[$1.to_sym] = value else in_options[key] = value out_options[key] = value end end # build input and output wrappers input = new(input || ARGF, **in_options) output = new(output || $stdout, **out_options) # read, yield, write input.each do |row| yield row output << row end end # # This method is intended as the primary interface for reading CSV files. You # pass a +path+ and any +options+ you wish to set for the read. Each row of # file will be passed to the provided +block+ in turn. # # The +options+ parameter can be anything CSV::new() understands. This method # also understands an additional :encoding parameter that you can use # to specify the Encoding of the data in the file to be read. You must provide # this unless your data is in Encoding::default_external(). CSV will use this # to determine how to parse the data. You may provide a second Encoding to # have the data transcoded as it is read. For example, # encoding: "UTF-32BE:UTF-8" would read UTF-32BE data from the file # but transcode it to UTF-8 before CSV parses it. # def foreach(path, mode="r", **options, &block) return to_enum(__method__, path, mode, **options) unless block_given? open(path, mode, **options) do |csv| csv.each(&block) end end # # :call-seq: # generate( str, **options ) { |csv| ... } # generate( **options ) { |csv| ... } # # This method wraps a String you provide, or an empty default String, in a # CSV object which is passed to the provided block. You can use the block to # append CSV rows to the String and when the block exits, the final String # will be returned. # # Note that a passed String *is* modified by this method. Call dup() before # passing if you need a new String. # # The +options+ parameter can be anything CSV::new() understands. This method # understands an additional :encoding parameter when not passed a # String to set the base Encoding for the output. CSV needs this hint if you # plan to output non-ASCII compatible data. # def generate(str=nil, **options) # add a default empty String, if none was given if str str = StringIO.new(str) str.seek(0, IO::SEEK_END) else encoding = options[:encoding] str = +"" str.force_encoding(encoding) if encoding end csv = new(str, **options) # wrap yield csv # yield for appending csv.string # return final String end # # This method is a shortcut for converting a single row (Array) into a CSV # String. # # The +options+ parameter can be anything CSV::new() understands. This method # understands an additional :encoding parameter to set the base # Encoding for the output. This method will try to guess your Encoding from # the first non-+nil+ field in +row+, if possible, but you may need to use # this parameter as a backup plan. # # The :row_sep +option+ defaults to $INPUT_RECORD_SEPARATOR # ($/) when calling this method. # def generate_line(row, **options) options = {row_sep: $INPUT_RECORD_SEPARATOR}.merge(options) str = +"" if options[:encoding] str.force_encoding(options[:encoding]) elsif field = row.find {|f| f.is_a?(String)} str.force_encoding(field.encoding) end (new(str, **options) << row).string end # # :call-seq: # open( filename, mode = "rb", **options ) { |faster_csv| ... } # open( filename, **options ) { |faster_csv| ... } # open( filename, mode = "rb", **options ) # open( filename, **options ) # # This method opens an IO object, and wraps that with CSV. This is intended # as the primary interface for writing a CSV file. # # You must pass a +filename+ and may optionally add a +mode+ for Ruby's # open(). You may also pass an optional Hash containing any +options+ # CSV::new() understands as the final argument. # # This method works like Ruby's open() call, in that it will pass a CSV object # to a provided block and close it when the block terminates, or it will # return the CSV object when no block is provided. (*Note*: This is different # from the Ruby 1.8 CSV library which passed rows to the block. Use # CSV::foreach() for that behavior.) # # You must provide a +mode+ with an embedded Encoding designator unless your # data is in Encoding::default_external(). CSV will check the Encoding of the # underlying IO object (set by the +mode+ you pass) to determine how to parse # the data. You may provide a second Encoding to have the data transcoded as # it is read just as you can with a normal call to IO::open(). For example, # "rb:UTF-32BE:UTF-8" would read UTF-32BE data from the file but # transcode it to UTF-8 before CSV parses it. # # An opened CSV object will delegate to many IO methods for convenience. You # may call: # # * binmode() # * binmode?() # * close() # * close_read() # * close_write() # * closed?() # * eof() # * eof?() # * external_encoding() # * fcntl() # * fileno() # * flock() # * flush() # * fsync() # * internal_encoding() # * ioctl() # * isatty() # * path() # * pid() # * pos() # * pos=() # * reopen() # * seek() # * stat() # * sync() # * sync=() # * tell() # * to_i() # * to_io() # * truncate() # * tty?() # def open(filename, mode="r", **options) # wrap a File opened with the remaining +args+ with no newline # decorator file_opts = {universal_newline: false}.merge(options) begin f = File.open(filename, mode, **file_opts) rescue ArgumentError => e raise unless /needs binmode/.match?(e.message) and mode == "r" mode = "rb" file_opts = {encoding: Encoding.default_external}.merge(file_opts) retry end begin csv = new(f, **options) rescue Exception f.close raise end # handle blocks like Ruby's open(), not like the CSV library if block_given? begin yield csv ensure csv.close end else csv end end # # :call-seq: # parse( str, **options ) { |row| ... } # parse( str, **options ) # # This method can be used to easily parse CSV out of a String. You may either # provide a +block+ which will be called with each row of the String in turn, # or just use the returned Array of Arrays (when no +block+ is given). # # You pass your +str+ to read from, and an optional +options+ containing # anything CSV::new() understands. # def parse(str, **options, &block) csv = new(str, **options) return csv.each(&block) if block_given? # slurp contents, if no block is given begin csv.read ensure csv.close end end # # This method is a shortcut for converting a single line of a CSV String into # an Array. Note that if +line+ contains multiple rows, anything beyond the # first row is ignored. # # The +options+ parameter can be anything CSV::new() understands. # def parse_line(line, **options) new(line, **options).shift end # # Use to slurp a CSV file into an Array of Arrays. Pass the +path+ to the # file and any +options+ CSV::new() understands. This method also understands # an additional :encoding parameter that you can use to specify the # Encoding of the data in the file to be read. You must provide this unless # your data is in Encoding::default_external(). CSV will use this to determine # how to parse the data. You may provide a second Encoding to have the data # transcoded as it is read. For example, # encoding: "UTF-32BE:UTF-8" would read UTF-32BE data from the file # but transcode it to UTF-8 before CSV parses it. # def read(path, **options) open(path, **options) { |csv| csv.read } end # Alias for CSV::read(). def readlines(path, **options) read(path, **options) end # # A shortcut for: # # CSV.read( path, { headers: true, # converters: :numeric, # header_converters: :symbol }.merge(options) ) # def table(path, **options) default_options = { headers: true, converters: :numeric, header_converters: :symbol, } options = default_options.merge(options) read(path, **options) end end # # This constructor will wrap either a String or IO object passed in +data+ for # reading and/or writing. In addition to the CSV instance methods, several IO # methods are delegated. (See CSV::open() for a complete list.) If you pass # a String for +data+, you can later retrieve it (after writing to it, for # example) with CSV.string(). # # Note that a wrapped String will be positioned at the beginning (for # reading). If you want it at the end (for writing), use CSV::generate(). # If you want any other positioning, pass a preset StringIO object instead. # # You may set any reading and/or writing preferences in the +options+ Hash. # Available options are: # # :col_sep:: The String placed between each field. # This String will be transcoded into # the data's Encoding before parsing. # :row_sep:: The String appended to the end of each # row. This can be set to the special # :auto setting, which requests # that CSV automatically discover this # from the data. Auto-discovery reads # ahead in the data looking for the next # "\r\n", "\n", or # "\r" sequence. A sequence # will be selected even if it occurs in # a quoted field, assuming that you # would have the same line endings # there. If none of those sequences is # found, +data+ is ARGF, # STDIN, STDOUT, or # STDERR, or the stream is only # available for output, the default # $INPUT_RECORD_SEPARATOR # ($/) is used. Obviously, # discovery takes a little time. Set # manually if speed is important. Also # note that IO objects should be opened # in binary mode on Windows if this # feature will be used as the # line-ending translation can cause # problems with resetting the document # position to where it was before the # read ahead. This String will be # transcoded into the data's Encoding # before parsing. # :quote_char:: The character used to quote fields. # This has to be a single character # String. This is useful for # application that incorrectly use # ' as the quote character # instead of the correct ". # CSV will always consider a double # sequence of this character to be an # escaped quote. This String will be # transcoded into the data's Encoding # before parsing. # :field_size_limit:: This is a maximum size CSV will read # ahead looking for the closing quote # for a field. (In truth, it reads to # the first line ending beyond this # size.) If a quote cannot be found # within the limit CSV will raise a # MalformedCSVError, assuming the data # is faulty. You can use this limit to # prevent what are effectively DoS # attacks on the parser. However, this # limit can cause a legitimate parse to # fail and thus is set to +nil+, or off, # by default. # :converters:: An Array of names from the Converters # Hash and/or lambdas that handle custom # conversion. A single converter # doesn't have to be in an Array. All # built-in converters try to transcode # fields to UTF-8 before converting. # The conversion will fail if the data # cannot be transcoded, leaving the # field unchanged. # :unconverted_fields:: If set to +true+, an # unconverted_fields() method will be # added to all returned rows (Array or # CSV::Row) that will return the fields # as they were before conversion. Note # that :headers supplied by # Array or String were not fields of the # document and thus will have an empty # Array attached. # :headers:: If set to :first_row or # +true+, the initial row of the CSV # file will be treated as a row of # headers. If set to an Array, the # contents will be used as the headers. # If set to a String, the String is run # through a call of CSV::parse_line() # with the same :col_sep, # :row_sep, and # :quote_char as this instance # to produce an Array of headers. This # setting causes CSV#shift() to return # rows as CSV::Row objects instead of # Arrays and CSV#read() to return # CSV::Table objects instead of an Array # of Arrays. # :return_headers:: When +false+, header rows are silently # swallowed. If set to +true+, header # rows are returned in a CSV::Row object # with identical headers and # fields (save that the fields do not go # through the converters). # :write_headers:: When +true+ and :headers is # set, a header row will be added to the # output. # :header_converters:: Identical in functionality to # :converters save that the # conversions are only made to header # rows. All built-in converters try to # transcode headers to UTF-8 before # converting. The conversion will fail # if the data cannot be transcoded, # leaving the header unchanged. # :skip_blanks:: When setting a +true+ value, CSV will # skip over any empty rows. Note that # this setting will not skip rows that # contain column separators, even if # the rows contain no actual data. If # you want to skip rows that contain # separators but no content, consider # using :skip_lines, or # inspecting fields.compact.empty? on # each row. # :force_quotes:: When setting a +true+ value, CSV will # quote all CSV fields it creates. # :skip_lines:: When setting an object responding to # match, every line matching # it is considered a comment and ignored # during parsing. When set to a String, # it is first converted to a Regexp. # When set to +nil+ no line is considered # a comment. If the passed object does # not respond to match, # ArgumentError is thrown. # :liberal_parsing:: When setting a +true+ value, CSV will # attempt to parse input not conformant # with RFC 4180, such as double quotes # in unquoted fields. # :nil_value:: When set an object, any values of an # empty field is replaced by the set # object, not nil. # :empty_value:: When setting an object, any values of a # blank string field is replaced by # the set object. # :quote_empty:: When setting a +true+ value, CSV will # quote empty values with double quotes. # When +false+, CSV will emit an # empty string for an empty field value. # :write_converters:: Converts values on each line with the # specified Proc object(s), # which receive a String value # and return a String or +nil+ # value. # When an array is specified, each # converter will be applied in order. # :write_nil_value:: When a String value, +nil+ # value(s) on each line will be replaced # with the specified value. # :write_empty_value:: When a String or +nil+ value, # empty value(s) on each line will be # replaced with the specified value. # :strip:: When setting a +true+ value, CSV will # strip "\t\r\n\f\v" around the values. # If you specify a string instead of # +true+, CSV will strip string. The # length of the string must be 1. # # See CSV::DEFAULT_OPTIONS for the default settings. # # Options cannot be overridden in the instance methods for performance reasons, # so be sure to set what you want here. # def initialize(data, col_sep: ",", row_sep: :auto, quote_char: '"', field_size_limit: nil, converters: nil, unconverted_fields: nil, headers: false, return_headers: false, write_headers: nil, header_converters: nil, skip_blanks: false, force_quotes: false, skip_lines: nil, liberal_parsing: false, internal_encoding: nil, external_encoding: nil, encoding: nil, nil_value: nil, empty_value: "", quote_empty: true, write_converters: nil, write_nil_value: nil, write_empty_value: "", strip: false) raise ArgumentError.new("Cannot parse nil as CSV") if data.nil? if data.is_a?(String) @io = StringIO.new(data) @io.set_encoding(encoding || data.encoding) else @io = data end @encoding = determine_encoding(encoding, internal_encoding) @base_fields_converter_options = { nil_value: nil_value, empty_value: empty_value, } @write_fields_converter_options = { nil_value: write_nil_value, empty_value: write_empty_value, } @initial_converters = converters @initial_header_converters = header_converters @initial_write_converters = write_converters @parser_options = { column_separator: col_sep, row_separator: row_sep, quote_character: quote_char, field_size_limit: field_size_limit, unconverted_fields: unconverted_fields, headers: headers, return_headers: return_headers, skip_blanks: skip_blanks, skip_lines: skip_lines, liberal_parsing: liberal_parsing, encoding: @encoding, nil_value: nil_value, empty_value: empty_value, strip: strip, } @parser = nil @parser_enumerator = nil @eof_error = nil @writer_options = { encoding: @encoding, force_encoding: (not encoding.nil?), force_quotes: force_quotes, headers: headers, write_headers: write_headers, column_separator: col_sep, row_separator: row_sep, quote_character: quote_char, quote_empty: quote_empty, } @writer = nil writer if @writer_options[:write_headers] end # # The encoded :col_sep used in parsing and writing. # See CSV::new for details. # def col_sep parser.column_separator end # # The encoded :row_sep used in parsing and writing. # See CSV::new for details. # def row_sep parser.row_separator end # # The encoded :quote_char used in parsing and writing. # See CSV::new for details. # def quote_char parser.quote_character end # # The limit for field size, if any. # See CSV::new for details. # def field_size_limit parser.field_size_limit end # # The regex marking a line as a comment. # See CSV::new for details. # def skip_lines parser.skip_lines end # # Returns the current list of converters in effect. See CSV::new for details. # Built-in converters will be returned by name, while others will be returned # as is. # def converters parser_fields_converter.map do |converter| name = Converters.rassoc(converter) name ? name.first : converter end end # # Returns +true+ if unconverted_fields() to parsed results. # See CSV::new for details. # def unconverted_fields? parser.unconverted_fields? end # # Returns +nil+ if headers will not be used, +true+ if they will but have not # yet been read, or the actual headers after they have been read. # See CSV::new for details. # def headers if @writer @writer.headers else parsed_headers = parser.headers return parsed_headers if parsed_headers raw_headers = @parser_options[:headers] raw_headers = nil if raw_headers == false raw_headers end end # # Returns +true+ if headers will be returned as a row of results. # See CSV::new for details. # def return_headers? parser.return_headers? end # # Returns +true+ if headers are written in output. # See CSV::new for details. # def write_headers? @writer_options[:write_headers] end # # Returns the current list of converters in effect for headers. See CSV::new # for details. Built-in converters will be returned by name, while others # will be returned as is. # def header_converters header_fields_converter.map do |converter| name = HeaderConverters.rassoc(converter) name ? name.first : converter end end # # Returns +true+ blank lines are skipped by the parser. See CSV::new # for details. # def skip_blanks? parser.skip_blanks? end # Returns +true+ if all output fields are quoted. See CSV::new for details. def force_quotes? @writer_options[:force_quotes] end # Returns +true+ if illegal input is handled. See CSV::new for details. def liberal_parsing? parser.liberal_parsing? end # # The Encoding CSV is parsing or writing in. This will be the Encoding you # receive parsed data in and/or the Encoding data will be written in. # attr_reader :encoding # # The line number of the last row read from this file. Fields with nested # line-end characters will not affect this count. # def lineno if @writer @writer.lineno else parser.lineno end end # # The last row read from this file. # def line parser.line end ### IO and StringIO Delegation ### extend Forwardable def_delegators :@io, :binmode, :close, :close_read, :close_write, :closed?, :external_encoding, :fcntl, :fileno, :flush, :fsync, :internal_encoding, :isatty, :pid, :pos, :pos=, :reopen, :seek, :string, :sync, :sync=, :tell, :truncate, :tty? def binmode? if @io.respond_to?(:binmode?) @io.binmode? else false end end def flock(*args) raise NotImplementedError unless @io.respond_to?(:flock) @io.flock(*args) end def ioctl(*args) raise NotImplementedError unless @io.respond_to?(:ioctl) @io.ioctl(*args) end def path @io.path if @io.respond_to?(:path) end def stat(*args) raise NotImplementedError unless @io.respond_to?(:stat) @io.stat(*args) end def to_i raise NotImplementedError unless @io.respond_to?(:to_i) @io.to_i end def to_io @io.respond_to?(:to_io) ? @io.to_io : @io end def eof? return false if @eof_error begin parser_enumerator.peek false rescue MalformedCSVError => error @eof_error = error false rescue StopIteration true end end alias_method :eof, :eof? # Rewinds the underlying IO object and resets CSV's lineno() counter. def rewind @parser = nil @parser_enumerator = nil @eof_error = nil @writer.rewind if @writer @io.rewind end ### End Delegation ### # # The primary write method for wrapped Strings and IOs, +row+ (an Array or # CSV::Row) is converted to CSV and appended to the data source. When a # CSV::Row is passed, only the row's fields() are appended to the output. # # The data source must be open for writing. # def <<(row) writer << row self end alias_method :add_row, :<< alias_method :puts, :<< # # :call-seq: # convert( name ) # convert { |field| ... } # convert { |field, field_info| ... } # # You can use this method to install a CSV::Converters built-in, or provide a # block that handles a custom conversion. # # If you provide a block that takes one argument, it will be passed the field # and is expected to return the converted value or the field itself. If your # block takes two arguments, it will also be passed a CSV::FieldInfo Struct, # containing details about the field. Again, the block should return a # converted field or the field itself. # def convert(name = nil, &converter) parser_fields_converter.add_converter(name, &converter) end # # :call-seq: # header_convert( name ) # header_convert { |field| ... } # header_convert { |field, field_info| ... } # # Identical to CSV#convert(), but for header rows. # # Note that this method must be called before header rows are read to have any # effect. # def header_convert(name = nil, &converter) header_fields_converter.add_converter(name, &converter) end include Enumerable # # Yields each row of the data source in turn. # # Support for Enumerable. # # The data source must be open for reading. # def each(&block) parser_enumerator.each(&block) end # # Slurps the remaining rows and returns an Array of Arrays. # # The data source must be open for reading. # def read rows = to_a if parser.use_headers? Table.new(rows, headers: parser.headers) else rows end end alias_method :readlines, :read # Returns +true+ if the next row read will be a header row. def header_row? parser.header_row? end # # The primary read method for wrapped Strings and IOs, a single row is pulled # from the data source, parsed and returned as an Array of fields (if header # rows are not used) or a CSV::Row (when header rows are used). # # The data source must be open for reading. # def shift if @eof_error eof_error, @eof_error = @eof_error, nil raise eof_error end begin parser_enumerator.next rescue StopIteration nil end end alias_method :gets, :shift alias_method :readline, :shift # # Returns a simplified description of the key CSV attributes in an # ASCII compatible String. # def inspect str = ["<#", self.class.to_s, " io_type:"] # show type of wrapped IO if @io == $stdout then str << "$stdout" elsif @io == $stdin then str << "$stdin" elsif @io == $stderr then str << "$stderr" else str << @io.class.to_s end # show IO.path(), if available if @io.respond_to?(:path) and (p = @io.path) str << " io_path:" << p.inspect end # show encoding str << " encoding:" << @encoding.name # show other attributes ["lineno", "col_sep", "row_sep", "quote_char"].each do |attr_name| if a = __send__(attr_name) str << " " << attr_name << ":" << a.inspect end end ["skip_blanks", "liberal_parsing"].each do |attr_name| if a = __send__("#{attr_name}?") str << " " << attr_name << ":" << a.inspect end end _headers = headers str << " headers:" << _headers.inspect if _headers str << ">" begin str.join('') rescue # any encoding error str.map do |s| e = Encoding::Converter.asciicompat_encoding(s.encoding) e ? s.encode(e) : s.force_encoding("ASCII-8BIT") end.join('') end end private def determine_encoding(encoding, internal_encoding) # honor the IO encoding if we can, otherwise default to ASCII-8BIT io_encoding = raw_encoding return io_encoding if io_encoding return Encoding.find(internal_encoding) if internal_encoding if encoding encoding, = encoding.split(":", 2) if encoding.is_a?(String) return Encoding.find(encoding) end Encoding.default_internal || Encoding.default_external end def normalize_converters(converters) converters ||= [] unless converters.is_a?(Array) converters = [converters] end converters.collect do |converter| case converter when Proc # custom code block [nil, converter] else # by name [converter, nil] end end end # # Processes +fields+ with @converters, or @header_converters # if +headers+ is passed as +true+, returning the converted field set. Any # converter that changes the field into something other than a String halts # the pipeline of conversion for that field. This is primarily an efficiency # shortcut. # def convert_fields(fields, headers = false) if headers header_fields_converter.convert(fields, nil, 0) else parser_fields_converter.convert(fields, @headers, lineno) end end # # Returns the encoding of the internal IO object. # def raw_encoding if @io.respond_to? :internal_encoding @io.internal_encoding || @io.external_encoding elsif @io.respond_to? :encoding @io.encoding else nil end end def parser_fields_converter @parser_fields_converter ||= build_parser_fields_converter end def build_parser_fields_converter specific_options = { builtin_converters: Converters, } options = @base_fields_converter_options.merge(specific_options) build_fields_converter(@initial_converters, options) end def header_fields_converter @header_fields_converter ||= build_header_fields_converter end def build_header_fields_converter specific_options = { builtin_converters: HeaderConverters, accept_nil: true, } options = @base_fields_converter_options.merge(specific_options) build_fields_converter(@initial_header_converters, options) end def writer_fields_converter @writer_fields_converter ||= build_writer_fields_converter end def build_writer_fields_converter build_fields_converter(@initial_write_converters, @write_fields_converter_options) end def build_fields_converter(initial_converters, options) fields_converter = FieldsConverter.new(options) normalize_converters(initial_converters).each do |name, converter| fields_converter.add_converter(name, &converter) end fields_converter end def parser @parser ||= Parser.new(@io, parser_options) end def parser_options @parser_options.merge(header_fields_converter: header_fields_converter, fields_converter: parser_fields_converter) end def parser_enumerator @parser_enumerator ||= parser.parse end def writer @writer ||= Writer.new(@io, writer_options) end def writer_options @writer_options.merge(header_fields_converter: header_fields_converter, fields_converter: writer_fields_converter) end end # Passes +args+ to CSV::instance. # # CSV("CSV,data").read # #=> [["CSV", "data"]] # # If a block is given, the instance is passed the block and the return value # becomes the return value of the block. # # CSV("CSV,data") { |c| # c.read.any? { |a| a.include?("data") } # } #=> true # # CSV("CSV,data") { |c| # c.read.any? { |a| a.include?("zombies") } # } #=> false # def CSV(*args, &block) CSV.instance(*args, &block) end require_relative "csv/version" require_relative "csv/core_ext/array" require_relative "csv/core_ext/string" csv-3.1.2/NEWS.md0000644000004100000410000002105413553655261013453 0ustar www-datawww-data# News ## 3.1.2 - 2019-10-12 ### Improvements * Added `:col_sep` check. [GitHub#94][Reported by Florent Beaurain] * Suppressed warnings. [GitHub#96][Patch by Nobuyoshi Nakada] * Improved documentation. [GitHub#101][GitHub#102][Patch by Vitor Oliveira] ### Fixes * Fixed a typo in documentation. [GitHub#95][Patch by Yuji Yaginuma] * Fixed a multibyte character handling bug. [GitHub#97][Patch by koshigoe] * Fixed typos in documentation. [GitHub#100][Patch by Vitor Oliveira] * Fixed a bug that seeked `StringIO` isn't accepted. [GitHub#98][Patch by MATSUMOTO Katsuyoshi] * Fixed a bug that `CSV.generate_line` doesn't work with `Encoding.default_internal`. [GitHub#105][Reported by David Rodríguez] ### Thanks * Florent Beaurain * Yuji Yaginuma * Nobuyoshi Nakada * koshigoe * Vitor Oliveira * MATSUMOTO Katsuyoshi * David Rodríguez ## 3.1.1 - 2019-04-26 ### Improvements * Added documentation for `strip` option. [GitHub#88][Patch by hayashiyoshino] * Added documentation for `write_converters`, `write_nil_value` and `write_empty_value` options. [GitHub#87][Patch by Masafumi Koba] * Added documentation for `quote_empty` option. [GitHub#89][Patch by kawa\_tech] ### Fixes * Fixed a bug that `strip; true` removes a newline. ### Thanks * hayashiyoshino * Masafumi Koba * kawa\_tech ## 3.1.0 - 2019-04-17 ### Fixes * Fixed a backward incompatibility bug that `CSV#eof?` may raises an error. [GitHub#86][Reported by krororo] ### Thanks * krororo ## 3.0.9 - 2019-04-15 ### Fixes * Fixed a test for Windows. ## 3.0.8 - 2019-04-11 ### Fixes * Fixed a bug that `strip: String` doesn't work. ## 3.0.7 - 2019-04-08 ### Improvements * Improve parse performance 1.5x by introducing loose parser. ### Fixes * Fix performance regression in 3.0.5. * Fix a bug that `CSV#line` returns wrong value when you use `quote_char: nil`. ## 3.0.6 - 2019-03-30 ### Improvements * `CSV.foreach`: Added support for `mode`. ## 3.0.5 - 2019-03-24 ### Improvements * Added `:liberal_parsing => {backslash_quote: true}` option. [GitHub#74][Patch by 284km] * Added `:write_converters` option. [GitHub#73][Patch by Danillo Souza] * Added `:write_nil_value` option. * Added `:write_empty_value` option. * Improved invalid byte line number detection. [GitHub#78][Patch by Alyssa Ross] * Added `quote_char: nil` optimization. [GitHub#79][Patch by 284km] * Improved error message. [GitHub#81][Patch by Andrés Torres] * Improved IO-like implementation for `StringIO` data. [GitHub#80][Patch by Genadi Samokovarov] * Added `:strip` option. [GitHub#58] ### Fixes * Fixed a compatibility bug that `CSV#each` doesn't care `CSV#shift`. [GitHub#76][Patch by Alyssa Ross] * Fixed a compatibility bug that `CSV#eof?` doesn't care `CSV#each` and `CSV#shift`. [GitHub#77][Reported by Chi Leung] * Fixed a compatibility bug that invalid line isn't ignored. [GitHub#82][Reported by krororo] * Fixed a bug that `:skip_lines` doesn't work with multibyte characters data. [GitHub#83][Reported by ff2248] ### Thanks * Alyssa Ross * 284km * Chi Leung * Danillo Souza * Andrés Torres * Genadi Samokovarov * krororo * ff2248 ## 3.0.4 - 2019-01-25 ### Improvements * Removed duplicated `CSV::Row#include?` implementations. [GitHub#69][Patch by Max Schwenk] * Removed duplicated `CSV::Row#header?` implementations. [GitHub#70][Patch by Max Schwenk] ### Fixes * Fixed a typo in document. [GitHub#72][Patch by Artur Beljajev] * Fixed a compatibility bug when row headers are changed. [GitHub#71][Reported by tomoyuki kosaka] ### Thanks * Max Schwenk * Artur Beljajev * tomoyuki kosaka ## 3.0.3 - 2019-01-12 ### Improvements * Migrated benchmark tool to benchmark-driver from benchmark-ips. [GitHub#57][Patch by 284km] * Added `liberal_parsing: {double_quote_outside_quote: true}` parse option. [GitHub#66][Reported by Watson] * Added `quote_empty:` write option. [GitHub#35][Reported by Dave Myron] ### Fixes * Fixed a compatibility bug that `CSV.generate` always return `ASCII-8BIT` encoding string. [GitHub#63][Patch by Watson] * Fixed a compatibility bug that `CSV.parse("", headers: true)` doesn't return `CSV::Table`. [GitHub#64][Reported by Watson][Patch by 284km] * Fixed a compatibility bug that multiple-characters column separator doesn't work. [GitHub#67][Reported by Jesse Reiss] * Fixed a compatibility bug that double `#each` parse twice. [GitHub#68][Reported by Max Schwenk] ### Thanks * Watson * 284km * Jesse Reiss * Dave Myron * Max Schwenk ## 3.0.2 - 2018-12-23 ### Improvements * Changed to use strscan in parser. [GitHub#52][Patch by 284km] * Improves CSV write performance. 3.0.2 will be about 2 times faster than 3.0.1. * Improves CSV parse performance for complex case. 3.0.2 will be about 2 times faster than 3.0.1. ### Fixes * Fixed a parse error bug for new line only input with `headers` option. [GitHub#53][Reported by Chris Beer] * Fixed some typos in document. [GitHub#54][Patch by Victor Shepelev] ### Thanks * 284km * Chris Beer * Victor Shepelev ## 3.0.1 - 2018-12-07 ### Improvements * Added a test. [GitHub#38][Patch by 284km] * `CSV::Row#dup`: Changed to duplicate internal data. [GitHub#39][Reported by André Guimarães Sakata] * Documented `:nil_value` and `:empty_value` options. [GitHub#41][Patch by OwlWorks] * Added support for separator detection for non-seekable inputs. [GitHub#45][Patch by Ilmari Karonen] * Removed needless code. [GitHub#48][Patch by Espartaco Palma] * Added support for parsing header only CSV with `headers: true`. [GitHub#47][Patch by Kazuma Shibasaka] * Added support for coverage report in CI. [GitHub#48][Patch by Espartaco Palma] * Improved auto CR row separator detection. [GitHub#51][Reported by Yuki Kurihara] ### Fixes * Fixed a typo in document. [GitHub#40][Patch by Marcus Stollsteimer] ### Thanks * 284km * André Guimarães Sakata * Marcus Stollsteimer * OwlWorks * Ilmari Karonen * Espartaco Palma * Kazuma Shibasaka * Yuki Kurihara ## 3.0.0 - 2018-06-06 ### Fixes * Fixed a bug that header isn't returned for empty row. [GitHub#37][Patch by Grace Lee] ### Thanks * Grace Lee ## 1.0.2 - 2018-05-03 ### Improvements * Split file for CSV::VERSION * Code cleanup: Split csv.rb into a more manageable structure [GitHub#19][Patch by Espartaco Palma] [GitHub#20][Patch by Steven Daniels] * Use CSV::MalformedCSVError for invalid encoding line [GitHub#26][Reported by deepj] * Support implicit Row <-> Array conversion [Bug #10013][ruby-core:63582][Reported by Dawid Janczak] * Update class docs [GitHub#32][Patch by zverok] * Add `Row#each_pair` [GitHub#33][Patch by zverok] * Improve CSV performance [GitHub#30][Patch by Watson] * Add :nil_value and :empty_value option ### Fixes * Fix a bug that "bom|utf-8" doesn't work [GitHub#23][Reported by Pavel Lobashov] * `CSV::Row#to_h`, `#to_hash`: uses the same value as `Row#[]` [Bug #14482][Reported by tomoya ishida] * Make row separator detection more robust [GitHub#25][Reported by deepj] * Fix a bug that too much separator when col_sep is `" "` [Bug #8784][ruby-core:63582][Reported by Sylvain Laperche] ### Thanks * Espartaco Palma * Steven Daniels * deepj * Dawid Janczak * zverok * Watson * Pavel Lobashov * tomoya ishida * Sylvain Laperche * Ryunosuke Sato ## 1.0.1 - 2018-02-09 ### Improvements * `CSV::Table#delete`: Added bulk delete support. You can delete multiple rows and columns at once. [GitHub#4][Patch by Vladislav] * Updated Gem description. [GitHub#11][Patch by Marcus Stollsteimer] * Code cleanup. [GitHub#12][Patch by Marcus Stollsteimer] [GitHub#14][Patch by Steven Daniels] [GitHub#18][Patch by takkanm] * `CSV::Table#dig`: Added. [GitHub#15][Patch by Tomohiro Ogoke] * `CSV::Row#dig`: Added. [GitHub#15][Patch by Tomohiro Ogoke] * Added ISO 8601 support to date time converter. [GitHub#16] ### Fixes * Fixed wrong `CSV::VERSION`. [GitHub#10][Reported by Marcus Stollsteimer] * `CSV.generate`: Fixed a regression bug that `String` argument is ignored. [GitHub#13][Patch by pavel] ### Thanks * Vladislav * Marcus Stollsteimer * Steven Daniels * takkanm * Tomohiro Ogoke * pavel csv-3.1.2/LICENSE.txt0000644000004100000410000000356213553655261014204 0ustar www-datawww-dataCopyright (C) 2005-2016 James Edward Gray II. All rights reserved. Copyright (C) 2007-2017 Yukihiro Matsumoto. All rights reserved. Copyright (C) 2017 SHIBATA Hiroshi. All rights reserved. Copyright (C) 2017 Olivier Lacan. All rights reserved. Copyright (C) 2017 Espartaco Palma. All rights reserved. Copyright (C) 2017 Marcus Stollsteimer. All rights reserved. Copyright (C) 2017 pavel. All rights reserved. Copyright (C) 2017-2018 Steven Daniels. All rights reserved. Copyright (C) 2018 Tomohiro Ogoke. All rights reserved. Copyright (C) 2018 Kouhei Sutou. All rights reserved. Copyright (C) 2018 Mitsutaka Mimura. All rights reserved. Copyright (C) 2018 Vladislav. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.