Prevent an "Uninitialized string offset" error

Got triggered by testMissingEndingLineBreak.
This commit is contained in:
Christian Bläul
2018-01-21 20:55:18 +01:00
committed by Fonata
parent b004911218
commit 488b8f9d9b
2 changed files with 16 additions and 0 deletions

View File

@@ -598,6 +598,7 @@ class parseCSV {
// data does not end with a line feed or carriage return character.
$lch = $data{$strlen - 1};
if ($lch != "\n" && $lch != "\r") {
$data .= "\n";
$strlen++;
}

View File

@@ -62,6 +62,21 @@ class ParseTest extends PHPUnit\Framework\TestCase {
$this->assertEquals($expected_data, $actual_data);
}
public function testMissingEndingLineBreak() {
$this->csv->heading = false;
$this->csv->enclosure = '"';
$sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\"";
$expected_data = [86545235689, 34365587654, 13469874576];
$actual_data = $this->csv->parse_string($sInput);
$actual_column = array_map('reset', $actual_data);
$this->assertEquals($expected_data, $actual_column);
$this->assertEquals([
'a',
'b',
"c\r\nd",
], array_map('next', $actual_data));
}
public function test_sep_row_auto_detection_UTF8() {
$this->_autoparse_magazine_file(
__DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv');