diff --git a/src/CsvReader.php b/src/CsvReader.php index 40e42ee..a71bd21 100644 --- a/src/CsvReader.php +++ b/src/CsvReader.php @@ -13,6 +13,7 @@ class CsvReader extends AbstractCsvFile implements Iterator * @deprecated use Keboola\Csv\CsvOptions::DEFAULT_ENCLOSURE */ const DEFAULT_ESCAPED_BY = CsvOptions::DEFAULT_ESCAPED_BY; + const SAMPLE_SIZE = 10000; /** * @var int @@ -124,7 +125,12 @@ protected function openCsvFile($fileName) protected function detectLineBreak() { @rewind($this->getFilePointer()); - $sample = @fread($this->getFilePointer(), 10000); + $sample = @fread($this->getFilePointer(), self::SAMPLE_SIZE); + if (substr($sample, -1) === "\r") { + // we might have hit the file in the middle of CR+LF, only getting CR + @rewind($this->getFilePointer()); + $sample = @fread($this->getFilePointer(), self::SAMPLE_SIZE+1); + } return LineBreaksHelper::detectLineBreaks($sample, $this->getEnclosure(), $this->getEscapedBy()); } diff --git a/tests/CsvReadTest.php b/tests/CsvReadTest.php index 1e11a38..8a5a0b2 100644 --- a/tests/CsvReadTest.php +++ b/tests/CsvReadTest.php @@ -33,7 +33,8 @@ public function testColumnsCount() public function testNewlineDetectionEdgecaseWithCrLf() { - $this->expectExceptionMessage('Invalid line break. Please use unix \n or win \r\n line breaks.'); + $this->expectNotToPerformAssertions(); + // this used to throw "Invalid line break. Please use unix \n or win \r\n line breaks." before the fix new CsvReader(__DIR__ . '/data/test-input-edgecase.crlf.csv'); }