Class: CSV::Parser::InputsScanner

Inherits:
Object
  • Object
show all
Defined in:
lib/csv/parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(inputs, encoding, chunk_size: 8192) ⇒ InputsScanner

Returns a new instance of InputsScanner.



43
44
45
46
47
48
49
50
# File 'lib/csv/parser.rb', line 43

def initialize(inputs, encoding, chunk_size: 8192)
  @inputs = inputs.dup
  @encoding = encoding
  @chunk_size = chunk_size
  @last_scanner = @inputs.empty?
  @keeps = []
  read_chunk
end

Instance Method Details

#each_line(row_separator) {|buffer| ... } ⇒ Object

Yields:

  • (buffer)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/csv/parser.rb', line 52

def each_line(row_separator)
  buffer = nil
  input = @scanner.rest
  @scanner.terminate
  while input
    input.each_line(row_separator) do |line|
      if buffer
        buffer << line
        line = buffer
        buffer = nil
      end
      if line.end_with?(row_separator)
        yield(line)
      else
        buffer = line
      end
    end
    input = @inputs.shift
  end
  yield(buffer) if buffer
end

#eos?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/csv/parser.rb', line 97

def eos?
  @scanner.eos?
end

#keep_backObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/csv/parser.rb', line 115

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
end

#keep_dropObject



130
131
132
# File 'lib/csv/parser.rb', line 130

def keep_drop
  @keeps.pop
end

#keep_endObject



105
106
107
108
109
110
111
112
113
# File 'lib/csv/parser.rb', line 105

def keep_end
  start, buffer = @keeps.pop
  keep = @scanner.string[start, @scanner.pos - start]
  if buffer
    buffer << keep
    keep = buffer
  end
  keep
end

#keep_startObject



101
102
103
# File 'lib/csv/parser.rb', line 101

def keep_start
  @keeps.push([@scanner.pos, nil])
end

#restObject



134
135
136
# File 'lib/csv/parser.rb', line 134

def rest
  @scanner.rest
end

#scan(pattern) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/csv/parser.rb', line 74

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

#scan_all(pattern) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/csv/parser.rb', line 86

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