Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/alt/ruby27/share/ruby/csv
File: parser.rb
# frozen_string_literal: true
[0] Fix | Delete
[1] Fix | Delete
require "strscan"
[2] Fix | Delete
[3] Fix | Delete
require_relative "delete_suffix"
[4] Fix | Delete
require_relative "match_p"
[5] Fix | Delete
require_relative "row"
[6] Fix | Delete
require_relative "table"
[7] Fix | Delete
[8] Fix | Delete
using CSV::DeleteSuffix if CSV.const_defined?(:DeleteSuffix)
[9] Fix | Delete
using CSV::MatchP if CSV.const_defined?(:MatchP)
[10] Fix | Delete
[11] Fix | Delete
class CSV
[12] Fix | Delete
# Note: Don't use this class directly. This is an internal class.
[13] Fix | Delete
class Parser
[14] Fix | Delete
#
[15] Fix | Delete
# A CSV::Parser is m17n aware. The parser works in the Encoding of the IO
[16] Fix | Delete
# or String object being read from or written to. Your data is never transcoded
[17] Fix | Delete
# (unless you ask Ruby to transcode it for you) and will literally be parsed in
[18] Fix | Delete
# the Encoding it is in. Thus CSV will return Arrays or Rows of Strings in the
[19] Fix | Delete
# Encoding of your data. This is accomplished by transcoding the parser itself
[20] Fix | Delete
# into your Encoding.
[21] Fix | Delete
#
[22] Fix | Delete
[23] Fix | Delete
# Raised when encoding is invalid.
[24] Fix | Delete
class InvalidEncoding < StandardError
[25] Fix | Delete
end
[26] Fix | Delete
[27] Fix | Delete
#
[28] Fix | Delete
# CSV::Scanner receives a CSV output, scans it and return the content.
[29] Fix | Delete
# It also controls the life cycle of the object with its methods +keep_start+,
[30] Fix | Delete
# +keep_end+, +keep_back+, +keep_drop+.
[31] Fix | Delete
#
[32] Fix | Delete
# Uses StringScanner (the official strscan gem). Strscan provides lexical
[33] Fix | Delete
# scanning operations on a String. We inherit its object and take advantage
[34] Fix | Delete
# on the methods. For more information, please visit:
[35] Fix | Delete
# https://ruby-doc.org/stdlib-2.6.1/libdoc/strscan/rdoc/StringScanner.html
[36] Fix | Delete
#
[37] Fix | Delete
class Scanner < StringScanner
[38] Fix | Delete
alias_method :scan_all, :scan
[39] Fix | Delete
[40] Fix | Delete
def initialize(*args)
[41] Fix | Delete
super
[42] Fix | Delete
@keeps = []
[43] Fix | Delete
end
[44] Fix | Delete
[45] Fix | Delete
def each_line(row_separator)
[46] Fix | Delete
position = pos
[47] Fix | Delete
rest.each_line(row_separator) do |line|
[48] Fix | Delete
position += line.bytesize
[49] Fix | Delete
self.pos = position
[50] Fix | Delete
yield(line)
[51] Fix | Delete
end
[52] Fix | Delete
end
[53] Fix | Delete
[54] Fix | Delete
def keep_start
[55] Fix | Delete
@keeps.push(pos)
[56] Fix | Delete
end
[57] Fix | Delete
[58] Fix | Delete
def keep_end
[59] Fix | Delete
start = @keeps.pop
[60] Fix | Delete
string.byteslice(start, pos - start)
[61] Fix | Delete
end
[62] Fix | Delete
[63] Fix | Delete
def keep_back
[64] Fix | Delete
self.pos = @keeps.pop
[65] Fix | Delete
end
[66] Fix | Delete
[67] Fix | Delete
def keep_drop
[68] Fix | Delete
@keeps.pop
[69] Fix | Delete
end
[70] Fix | Delete
end
[71] Fix | Delete
[72] Fix | Delete
#
[73] Fix | Delete
# CSV::InputsScanner receives IO inputs, encoding and the chunk_size.
[74] Fix | Delete
# It also controls the life cycle of the object with its methods +keep_start+,
[75] Fix | Delete
# +keep_end+, +keep_back+, +keep_drop+.
[76] Fix | Delete
#
[77] Fix | Delete
# CSV::InputsScanner.scan() tries to match with pattern at the current position.
[78] Fix | Delete
# If there's a match, the scanner advances the “scan pointer” and returns the matched string.
[79] Fix | Delete
# Otherwise, the scanner returns nil.
[80] Fix | Delete
#
[81] Fix | Delete
# CSV::InputsScanner.rest() returns the “rest” of the string (i.e. everything after the scan pointer).
[82] Fix | Delete
# If there is no more data (eos? = true), it returns "".
[83] Fix | Delete
#
[84] Fix | Delete
class InputsScanner
[85] Fix | Delete
def initialize(inputs, encoding, chunk_size: 8192)
[86] Fix | Delete
@inputs = inputs.dup
[87] Fix | Delete
@encoding = encoding
[88] Fix | Delete
@chunk_size = chunk_size
[89] Fix | Delete
@last_scanner = @inputs.empty?
[90] Fix | Delete
@keeps = []
[91] Fix | Delete
read_chunk
[92] Fix | Delete
end
[93] Fix | Delete
[94] Fix | Delete
def each_line(row_separator)
[95] Fix | Delete
buffer = nil
[96] Fix | Delete
input = @scanner.rest
[97] Fix | Delete
position = @scanner.pos
[98] Fix | Delete
offset = 0
[99] Fix | Delete
n_row_separator_chars = row_separator.size
[100] Fix | Delete
while true
[101] Fix | Delete
input.each_line(row_separator) do |line|
[102] Fix | Delete
@scanner.pos += line.bytesize
[103] Fix | Delete
if buffer
[104] Fix | Delete
if n_row_separator_chars == 2 and
[105] Fix | Delete
buffer.end_with?(row_separator[0]) and
[106] Fix | Delete
line.start_with?(row_separator[1])
[107] Fix | Delete
buffer << line[0]
[108] Fix | Delete
line = line[1..-1]
[109] Fix | Delete
position += buffer.bytesize + offset
[110] Fix | Delete
@scanner.pos = position
[111] Fix | Delete
offset = 0
[112] Fix | Delete
yield(buffer)
[113] Fix | Delete
buffer = nil
[114] Fix | Delete
next if line.empty?
[115] Fix | Delete
else
[116] Fix | Delete
buffer << line
[117] Fix | Delete
line = buffer
[118] Fix | Delete
buffer = nil
[119] Fix | Delete
end
[120] Fix | Delete
end
[121] Fix | Delete
if line.end_with?(row_separator)
[122] Fix | Delete
position += line.bytesize + offset
[123] Fix | Delete
@scanner.pos = position
[124] Fix | Delete
offset = 0
[125] Fix | Delete
yield(line)
[126] Fix | Delete
else
[127] Fix | Delete
buffer = line
[128] Fix | Delete
end
[129] Fix | Delete
end
[130] Fix | Delete
break unless read_chunk
[131] Fix | Delete
input = @scanner.rest
[132] Fix | Delete
position = @scanner.pos
[133] Fix | Delete
offset = -buffer.bytesize if buffer
[134] Fix | Delete
end
[135] Fix | Delete
yield(buffer) if buffer
[136] Fix | Delete
end
[137] Fix | Delete
[138] Fix | Delete
def scan(pattern)
[139] Fix | Delete
value = @scanner.scan(pattern)
[140] Fix | Delete
return value if @last_scanner
[141] Fix | Delete
[142] Fix | Delete
if value
[143] Fix | Delete
read_chunk if @scanner.eos?
[144] Fix | Delete
return value
[145] Fix | Delete
else
[146] Fix | Delete
nil
[147] Fix | Delete
end
[148] Fix | Delete
end
[149] Fix | Delete
[150] Fix | Delete
def scan_all(pattern)
[151] Fix | Delete
value = @scanner.scan(pattern)
[152] Fix | Delete
return value if @last_scanner
[153] Fix | Delete
[154] Fix | Delete
return nil if value.nil?
[155] Fix | Delete
while @scanner.eos? and read_chunk and (sub_value = @scanner.scan(pattern))
[156] Fix | Delete
value << sub_value
[157] Fix | Delete
end
[158] Fix | Delete
value
[159] Fix | Delete
end
[160] Fix | Delete
[161] Fix | Delete
def eos?
[162] Fix | Delete
@scanner.eos?
[163] Fix | Delete
end
[164] Fix | Delete
[165] Fix | Delete
def keep_start
[166] Fix | Delete
@keeps.push([@scanner.pos, nil])
[167] Fix | Delete
end
[168] Fix | Delete
[169] Fix | Delete
def keep_end
[170] Fix | Delete
start, buffer = @keeps.pop
[171] Fix | Delete
keep = @scanner.string.byteslice(start, @scanner.pos - start)
[172] Fix | Delete
if buffer
[173] Fix | Delete
buffer << keep
[174] Fix | Delete
keep = buffer
[175] Fix | Delete
end
[176] Fix | Delete
keep
[177] Fix | Delete
end
[178] Fix | Delete
[179] Fix | Delete
def keep_back
[180] Fix | Delete
start, buffer = @keeps.pop
[181] Fix | Delete
if buffer
[182] Fix | Delete
string = @scanner.string
[183] Fix | Delete
keep = string.byteslice(start, string.bytesize - start)
[184] Fix | Delete
if keep and not keep.empty?
[185] Fix | Delete
@inputs.unshift(StringIO.new(keep))
[186] Fix | Delete
@last_scanner = false
[187] Fix | Delete
end
[188] Fix | Delete
@scanner = StringScanner.new(buffer)
[189] Fix | Delete
else
[190] Fix | Delete
@scanner.pos = start
[191] Fix | Delete
end
[192] Fix | Delete
read_chunk if @scanner.eos?
[193] Fix | Delete
end
[194] Fix | Delete
[195] Fix | Delete
def keep_drop
[196] Fix | Delete
@keeps.pop
[197] Fix | Delete
end
[198] Fix | Delete
[199] Fix | Delete
def rest
[200] Fix | Delete
@scanner.rest
[201] Fix | Delete
end
[202] Fix | Delete
[203] Fix | Delete
private
[204] Fix | Delete
def read_chunk
[205] Fix | Delete
return false if @last_scanner
[206] Fix | Delete
[207] Fix | Delete
unless @keeps.empty?
[208] Fix | Delete
keep = @keeps.last
[209] Fix | Delete
keep_start = keep[0]
[210] Fix | Delete
string = @scanner.string
[211] Fix | Delete
keep_data = string.byteslice(keep_start, @scanner.pos - keep_start)
[212] Fix | Delete
if keep_data
[213] Fix | Delete
keep_buffer = keep[1]
[214] Fix | Delete
if keep_buffer
[215] Fix | Delete
keep_buffer << keep_data
[216] Fix | Delete
else
[217] Fix | Delete
keep[1] = keep_data.dup
[218] Fix | Delete
end
[219] Fix | Delete
end
[220] Fix | Delete
keep[0] = 0
[221] Fix | Delete
end
[222] Fix | Delete
[223] Fix | Delete
input = @inputs.first
[224] Fix | Delete
case input
[225] Fix | Delete
when StringIO
[226] Fix | Delete
string = input.read
[227] Fix | Delete
raise InvalidEncoding unless string.valid_encoding?
[228] Fix | Delete
@scanner = StringScanner.new(string)
[229] Fix | Delete
@inputs.shift
[230] Fix | Delete
@last_scanner = @inputs.empty?
[231] Fix | Delete
true
[232] Fix | Delete
else
[233] Fix | Delete
chunk = input.gets(nil, @chunk_size)
[234] Fix | Delete
if chunk
[235] Fix | Delete
raise InvalidEncoding unless chunk.valid_encoding?
[236] Fix | Delete
@scanner = StringScanner.new(chunk)
[237] Fix | Delete
if input.respond_to?(:eof?) and input.eof?
[238] Fix | Delete
@inputs.shift
[239] Fix | Delete
@last_scanner = @inputs.empty?
[240] Fix | Delete
end
[241] Fix | Delete
true
[242] Fix | Delete
else
[243] Fix | Delete
@scanner = StringScanner.new("".encode(@encoding))
[244] Fix | Delete
@inputs.shift
[245] Fix | Delete
@last_scanner = @inputs.empty?
[246] Fix | Delete
if @last_scanner
[247] Fix | Delete
false
[248] Fix | Delete
else
[249] Fix | Delete
read_chunk
[250] Fix | Delete
end
[251] Fix | Delete
end
[252] Fix | Delete
end
[253] Fix | Delete
end
[254] Fix | Delete
end
[255] Fix | Delete
[256] Fix | Delete
def initialize(input, options)
[257] Fix | Delete
@input = input
[258] Fix | Delete
@options = options
[259] Fix | Delete
@samples = []
[260] Fix | Delete
[261] Fix | Delete
prepare
[262] Fix | Delete
end
[263] Fix | Delete
[264] Fix | Delete
def column_separator
[265] Fix | Delete
@column_separator
[266] Fix | Delete
end
[267] Fix | Delete
[268] Fix | Delete
def row_separator
[269] Fix | Delete
@row_separator
[270] Fix | Delete
end
[271] Fix | Delete
[272] Fix | Delete
def quote_character
[273] Fix | Delete
@quote_character
[274] Fix | Delete
end
[275] Fix | Delete
[276] Fix | Delete
def field_size_limit
[277] Fix | Delete
@field_size_limit
[278] Fix | Delete
end
[279] Fix | Delete
[280] Fix | Delete
def skip_lines
[281] Fix | Delete
@skip_lines
[282] Fix | Delete
end
[283] Fix | Delete
[284] Fix | Delete
def unconverted_fields?
[285] Fix | Delete
@unconverted_fields
[286] Fix | Delete
end
[287] Fix | Delete
[288] Fix | Delete
def headers
[289] Fix | Delete
@headers
[290] Fix | Delete
end
[291] Fix | Delete
[292] Fix | Delete
def header_row?
[293] Fix | Delete
@use_headers and @headers.nil?
[294] Fix | Delete
end
[295] Fix | Delete
[296] Fix | Delete
def return_headers?
[297] Fix | Delete
@return_headers
[298] Fix | Delete
end
[299] Fix | Delete
[300] Fix | Delete
def skip_blanks?
[301] Fix | Delete
@skip_blanks
[302] Fix | Delete
end
[303] Fix | Delete
[304] Fix | Delete
def liberal_parsing?
[305] Fix | Delete
@liberal_parsing
[306] Fix | Delete
end
[307] Fix | Delete
[308] Fix | Delete
def lineno
[309] Fix | Delete
@lineno
[310] Fix | Delete
end
[311] Fix | Delete
[312] Fix | Delete
def line
[313] Fix | Delete
last_line
[314] Fix | Delete
end
[315] Fix | Delete
[316] Fix | Delete
def parse(&block)
[317] Fix | Delete
return to_enum(__method__) unless block_given?
[318] Fix | Delete
[319] Fix | Delete
if @return_headers and @headers and @raw_headers
[320] Fix | Delete
headers = Row.new(@headers, @raw_headers, true)
[321] Fix | Delete
if @unconverted_fields
[322] Fix | Delete
headers = add_unconverted_fields(headers, [])
[323] Fix | Delete
end
[324] Fix | Delete
yield headers
[325] Fix | Delete
end
[326] Fix | Delete
[327] Fix | Delete
begin
[328] Fix | Delete
@scanner ||= build_scanner
[329] Fix | Delete
if quote_character.nil?
[330] Fix | Delete
parse_no_quote(&block)
[331] Fix | Delete
elsif @need_robust_parsing
[332] Fix | Delete
parse_quotable_robust(&block)
[333] Fix | Delete
else
[334] Fix | Delete
parse_quotable_loose(&block)
[335] Fix | Delete
end
[336] Fix | Delete
rescue InvalidEncoding
[337] Fix | Delete
if @scanner
[338] Fix | Delete
ignore_broken_line
[339] Fix | Delete
lineno = @lineno
[340] Fix | Delete
else
[341] Fix | Delete
lineno = @lineno + 1
[342] Fix | Delete
end
[343] Fix | Delete
message = "Invalid byte sequence in #{@encoding}"
[344] Fix | Delete
raise MalformedCSVError.new(message, lineno)
[345] Fix | Delete
end
[346] Fix | Delete
end
[347] Fix | Delete
[348] Fix | Delete
def use_headers?
[349] Fix | Delete
@use_headers
[350] Fix | Delete
end
[351] Fix | Delete
[352] Fix | Delete
private
[353] Fix | Delete
# A set of tasks to prepare the file in order to parse it
[354] Fix | Delete
def prepare
[355] Fix | Delete
prepare_variable
[356] Fix | Delete
prepare_quote_character
[357] Fix | Delete
prepare_backslash
[358] Fix | Delete
prepare_skip_lines
[359] Fix | Delete
prepare_strip
[360] Fix | Delete
prepare_separators
[361] Fix | Delete
prepare_quoted
[362] Fix | Delete
prepare_unquoted
[363] Fix | Delete
prepare_line
[364] Fix | Delete
prepare_header
[365] Fix | Delete
prepare_parser
[366] Fix | Delete
end
[367] Fix | Delete
[368] Fix | Delete
def prepare_variable
[369] Fix | Delete
@need_robust_parsing = false
[370] Fix | Delete
@encoding = @options[:encoding]
[371] Fix | Delete
liberal_parsing = @options[:liberal_parsing]
[372] Fix | Delete
if liberal_parsing
[373] Fix | Delete
@liberal_parsing = true
[374] Fix | Delete
if liberal_parsing.is_a?(Hash)
[375] Fix | Delete
@double_quote_outside_quote =
[376] Fix | Delete
liberal_parsing[:double_quote_outside_quote]
[377] Fix | Delete
@backslash_quote = liberal_parsing[:backslash_quote]
[378] Fix | Delete
else
[379] Fix | Delete
@double_quote_outside_quote = false
[380] Fix | Delete
@backslash_quote = false
[381] Fix | Delete
end
[382] Fix | Delete
@need_robust_parsing = true
[383] Fix | Delete
else
[384] Fix | Delete
@liberal_parsing = false
[385] Fix | Delete
@backslash_quote = false
[386] Fix | Delete
end
[387] Fix | Delete
@unconverted_fields = @options[:unconverted_fields]
[388] Fix | Delete
@field_size_limit = @options[:field_size_limit]
[389] Fix | Delete
@skip_blanks = @options[:skip_blanks]
[390] Fix | Delete
@fields_converter = @options[:fields_converter]
[391] Fix | Delete
@header_fields_converter = @options[:header_fields_converter]
[392] Fix | Delete
end
[393] Fix | Delete
[394] Fix | Delete
def prepare_quote_character
[395] Fix | Delete
@quote_character = @options[:quote_character]
[396] Fix | Delete
if @quote_character.nil?
[397] Fix | Delete
@escaped_quote_character = nil
[398] Fix | Delete
@escaped_quote = nil
[399] Fix | Delete
else
[400] Fix | Delete
@quote_character = @quote_character.to_s.encode(@encoding)
[401] Fix | Delete
if @quote_character.length != 1
[402] Fix | Delete
message = ":quote_char has to be nil or a single character String"
[403] Fix | Delete
raise ArgumentError, message
[404] Fix | Delete
end
[405] Fix | Delete
@double_quote_character = @quote_character * 2
[406] Fix | Delete
@escaped_quote_character = Regexp.escape(@quote_character)
[407] Fix | Delete
@escaped_quote = Regexp.new(@escaped_quote_character)
[408] Fix | Delete
end
[409] Fix | Delete
end
[410] Fix | Delete
[411] Fix | Delete
def prepare_backslash
[412] Fix | Delete
return unless @backslash_quote
[413] Fix | Delete
[414] Fix | Delete
@backslash_character = "\\".encode(@encoding)
[415] Fix | Delete
[416] Fix | Delete
@escaped_backslash_character = Regexp.escape(@backslash_character)
[417] Fix | Delete
@escaped_backslash = Regexp.new(@escaped_backslash_character)
[418] Fix | Delete
if @quote_character.nil?
[419] Fix | Delete
@backslash_quote_character = nil
[420] Fix | Delete
else
[421] Fix | Delete
@backslash_quote_character =
[422] Fix | Delete
@backslash_character + @escaped_quote_character
[423] Fix | Delete
end
[424] Fix | Delete
end
[425] Fix | Delete
[426] Fix | Delete
def prepare_skip_lines
[427] Fix | Delete
skip_lines = @options[:skip_lines]
[428] Fix | Delete
case skip_lines
[429] Fix | Delete
when String
[430] Fix | Delete
@skip_lines = skip_lines.encode(@encoding)
[431] Fix | Delete
when Regexp, nil
[432] Fix | Delete
@skip_lines = skip_lines
[433] Fix | Delete
else
[434] Fix | Delete
unless skip_lines.respond_to?(:match)
[435] Fix | Delete
message =
[436] Fix | Delete
":skip_lines has to respond to \#match: #{skip_lines.inspect}"
[437] Fix | Delete
raise ArgumentError, message
[438] Fix | Delete
end
[439] Fix | Delete
@skip_lines = skip_lines
[440] Fix | Delete
end
[441] Fix | Delete
end
[442] Fix | Delete
[443] Fix | Delete
def prepare_strip
[444] Fix | Delete
@strip = @options[:strip]
[445] Fix | Delete
@escaped_strip = nil
[446] Fix | Delete
@strip_value = nil
[447] Fix | Delete
if @strip.is_a?(String)
[448] Fix | Delete
case @strip.length
[449] Fix | Delete
when 0
[450] Fix | Delete
raise ArgumentError, ":strip must not be an empty String"
[451] Fix | Delete
when 1
[452] Fix | Delete
# ok
[453] Fix | Delete
else
[454] Fix | Delete
raise ArgumentError, ":strip doesn't support 2 or more characters yet"
[455] Fix | Delete
end
[456] Fix | Delete
@strip = @strip.encode(@encoding)
[457] Fix | Delete
@escaped_strip = Regexp.escape(@strip)
[458] Fix | Delete
if @quote_character
[459] Fix | Delete
@strip_value = Regexp.new(@escaped_strip +
[460] Fix | Delete
"+".encode(@encoding))
[461] Fix | Delete
end
[462] Fix | Delete
@need_robust_parsing = true
[463] Fix | Delete
elsif @strip
[464] Fix | Delete
strip_values = " \t\f\v"
[465] Fix | Delete
@escaped_strip = strip_values.encode(@encoding)
[466] Fix | Delete
if @quote_character
[467] Fix | Delete
@strip_value = Regexp.new("[#{strip_values}]+".encode(@encoding))
[468] Fix | Delete
end
[469] Fix | Delete
@need_robust_parsing = true
[470] Fix | Delete
end
[471] Fix | Delete
end
[472] Fix | Delete
[473] Fix | Delete
begin
[474] Fix | Delete
StringScanner.new("x").scan("x")
[475] Fix | Delete
rescue TypeError
[476] Fix | Delete
@@string_scanner_scan_accept_string = false
[477] Fix | Delete
else
[478] Fix | Delete
@@string_scanner_scan_accept_string = true
[479] Fix | Delete
end
[480] Fix | Delete
[481] Fix | Delete
def prepare_separators
[482] Fix | Delete
column_separator = @options[:column_separator]
[483] Fix | Delete
@column_separator = column_separator.to_s.encode(@encoding)
[484] Fix | Delete
if @column_separator.size < 1
[485] Fix | Delete
message = ":col_sep must be 1 or more characters: "
[486] Fix | Delete
message += column_separator.inspect
[487] Fix | Delete
raise ArgumentError, message
[488] Fix | Delete
end
[489] Fix | Delete
@row_separator =
[490] Fix | Delete
resolve_row_separator(@options[:row_separator]).encode(@encoding)
[491] Fix | Delete
[492] Fix | Delete
@escaped_column_separator = Regexp.escape(@column_separator)
[493] Fix | Delete
@escaped_first_column_separator = Regexp.escape(@column_separator[0])
[494] Fix | Delete
if @column_separator.size > 1
[495] Fix | Delete
@column_end = Regexp.new(@escaped_column_separator)
[496] Fix | Delete
@column_ends = @column_separator.each_char.collect do |char|
[497] Fix | Delete
Regexp.new(Regexp.escape(char))
[498] Fix | Delete
end
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function