Skip to content

Commit

Permalink
removed rewinding rows in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeljko-Predjeskovic committed Dec 30, 2024
1 parent 6ef3352 commit 647aa3e
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 16 deletions.
9 changes: 3 additions & 6 deletions spec/std/csv/csv_parse_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,12 @@ describe CSV do
end

it "parses to hashes remaining rows" do
csv_text = "Index,Customer Id,First Name,Last Name\n1,DD37Cf93aecA6Dc,Sheryl,Baxter\n2,1Ef7b82A4CAAD10,Preston,Lozano\n3,6F94879bDAfE5a6,,Berry"
csv_text = "Index,Customer Id,First Name,Last Name\n1,DD37Cf93aecA6Dc,Sheryl,Baxter\n2,1Ef7b82A4CAAD10,Preston,Lozano"
parser = CSV::Parser.new(csv_text)
# skip header
parser.next_row
# skip rows
parser.next_row

parser.next_row

parser.parse_to_h.should eq([{"Index" => "3", "Customer Id" => "6F94879bDAfE5a6", "First Name" => "", "Last Name" => "Berry"}])
parser.parse_to_h.should eq([{"1" => "2", "DD37Cf93aecA6Dc" => "1Ef7b82A4CAAD10", "Sheryl" => "Preston", "Baxter" => "Lozano"}])
end

it "raises if single quote in the middle" do
Expand Down
3 changes: 0 additions & 3 deletions src/csv/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ abstract class CSV::Lexer
getter separator : Char
getter quote_char : Char

# :nodoc:
protected getter line_number : Int32

# :nodoc:
def initialize(@separator : Char = DEFAULT_SEPARATOR, @quote_char : Char = DEFAULT_QUOTE_CHAR)
@token = Token.new
Expand Down
8 changes: 1 addition & 7 deletions src/csv/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ class CSV::Parser

def parse_to_h : Array(Hash(String, String))
rows = [] of Hash(String, String)
row_number = @lexer.line_number

rewind
if headers = next_row
while @lexer.line_number < row_number
next_row
end
each_row do |row|
while row = next_row
if parsed_row = parse_row_to_h_internal(headers, row)
rows << parsed_row
end
Expand Down

0 comments on commit 647aa3e

Please sign in to comment.