Skip to content

Commit

Permalink
Merge pull request #42 from keboola/webrouse-COM-350-fix-readline-speed
Browse files Browse the repository at this point in the history
Validate line breaks only once - in constructor
  • Loading branch information
michaljurecko authored Jul 28, 2020
2 parents a439656 + fcce3bf commit eb5a835
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions src/CsvReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ public function __construct(
$this->options = new CsvOptions($delimiter, $enclosure, $escapedBy);
$this->setSkipLines($skipLines);
$this->setFile($file);

$this->lineBreak = $this->detectLineBreak();
$this->validateLineBreak();

rewind($this->filePointer);
$this->header = $this->readLine();
$this->rewind();
Expand Down Expand Up @@ -124,8 +127,6 @@ protected function detectLineBreak()
*/
protected function readLine()
{
$this->validateLineBreak();

// allow empty enclosure hack
$enclosure = !$this->getEnclosure() ? chr(0) : $this->getEnclosure();
$escapedBy = !$this->getEscapedBy() ? chr(0) : $this->getEscapedBy();
Expand All @@ -138,15 +139,7 @@ protected function readLine()
*/
protected function validateLineBreak()
{
try {
$lineBreak = $this->getLineBreak();
} catch (Exception $e) {
throw new InvalidArgumentException(
"Failed to detect line break: " . $e->getMessage(),
Exception::INVALID_PARAM,
$e
);
}
$lineBreak = $this->getLineBreak();
if (in_array($lineBreak, ["\r\n", "\n"])) {
return $lineBreak;
}
Expand Down

0 comments on commit eb5a835

Please sign in to comment.