Skip to content

Commit

Permalink
Solved "Machine did not halt" exception for chunks ending with '\r\n' (
Browse files Browse the repository at this point in the history
…#16)

Co-authored-by: nvolodin <[email protected]>
  • Loading branch information
klimp-drupal and nvolodin authored Jan 16, 2021
1 parent 40d781a commit 6f1a4aa
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Parser/StateMachine.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct()

$this->addEndState(self::STATE_NEW_FIELD);
$this->addEndState(self::STATE_RECORD_END);
$this->addEndState(self::STATE_REDUNDANT_RECORD_END);

$this->addNewFieldTransitions();
$this->addNoCaptureTransitions();
Expand Down
63 changes: 63 additions & 0 deletions test/CsvParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ public function testMultiEnd()
$values = ['G', 'B'];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());

$parser = $this->parse("a,b\r\n");
$record = $parser->getRecord();
$values = ['a', 'b'];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());
}

public function testSerialization()
Expand All @@ -372,4 +378,61 @@ public function testSerialization()
$record2 = $parser2->getRecord();
$this->assertEquals($record2[0], 'e');
}

public function testEndingWithNewLineString()
{
$parser = $this->parse(",,\n,,\n");
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());
}

public function testEndingWithWindowsNewLineString()
{
$parser = $this->parse(",,\r\n,,\r\n");
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());
}

public function testEndingWithMultipleNewLinesString()
{
$parser = $this->parse(",,\n,,\n\n\n");
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());
}

public function testEndingWithMultipleWindowsNewLinesString()
{
$parser = $this->parse(",,\r\n,,\r\n\r\n\r\n");
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$record = $parser->getRecord();
$values = ['', '', ''];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());
}

public function testEmptyWindowsNewLineString()
{
$parser = $this->parse("\r\n");
$record = $parser->getRecord();
$values = [''];
$this->assertNumberOfFieldsAndValues($record, $values);
$this->assertNull($parser->getRecord());
}
}

0 comments on commit 6f1a4aa

Please sign in to comment.