Changeset View
Changeset View
Standalone View
Standalone View
src/filesystem/linesofalarge/LinesOfALarge.php
| Show All 36 Lines | abstract class LinesOfALarge extends Phobject implements Iterator { | ||||
| private $pos; | private $pos; | ||||
| private $buf; | private $buf; | ||||
| private $num; | private $num; | ||||
| private $line; | private $line; | ||||
| private $valid; | private $valid; | ||||
| private $eof; | private $eof; | ||||
| private $delimiter = "\n"; | private $delimiter = "\n"; | ||||
| /* -( Configuration )------------------------------------------------------ */ | /* -( Configuration )------------------------------------------------------ */ | ||||
| /** | /** | ||||
| * Change the "line" delimiter character, which defaults to "\n". This is | * Change the "line" delimiter character, which defaults to "\n". This is | ||||
| * used to determine where each line ends. | * used to determine where each line ends. | ||||
| * | * | ||||
| * @param string A one-byte delimiter character. | * If you pass `null`, data will be read from source as it becomes available, | ||||
| * without looking for delimiters. You can use this to stream a large file or | |||||
| * the output of a command which returns a large amount of data. | |||||
| * | |||||
| * @param string|null A one-byte delimiter character. | |||||
| * @return this | * @return this | ||||
| * @task config | * @task config | ||||
| */ | */ | ||||
| final public function setDelimiter($character) { | final public function setDelimiter($character) { | ||||
| if (strlen($character) !== 1) { | if (($character !== null) && (strlen($character) !== 1)) { | ||||
| throw new Exception( | throw new Exception( | ||||
| pht('Delimiter character MUST be one byte in length.')); | pht('Delimiter character must be one byte in length or null.')); | ||||
| } | } | ||||
| $this->delimiter = $character; | $this->delimiter = $character; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| /* -( Internals )---------------------------------------------------------- */ | /* -( Internals )---------------------------------------------------------- */ | ||||
| ▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | final public function next() { | ||||
| // read in reasonably-sized chunks of many lines), at the cost of some | // read in reasonably-sized chunks of many lines), at the cost of some | ||||
| // complexity in buffer management. | // complexity in buffer management. | ||||
| // We do this in a loop to avoid recursion when consuming more bytes, in | // We do this in a loop to avoid recursion when consuming more bytes, in | ||||
| // case the size of a line is very large compared to the chunk size we | // case the size of a line is very large compared to the chunk size we | ||||
| // read. | // read. | ||||
| while (true) { | while (true) { | ||||
| if (strlen($this->buf)) { | if (strlen($this->buf)) { | ||||
| // If we don't have a delimiter, return the entire buffer. | |||||
| if ($this->delimiter === null) { | |||||
| $this->num++; | |||||
| $this->line = substr($this->buf, $this->pos); | |||||
| $this->buf = ''; | |||||
| $this->pos = 0; | |||||
| return; | |||||
| } | |||||
| // If we already have some data buffered, try to get the next line from | // If we already have some data buffered, try to get the next line from | ||||
| // the buffer. Search through the buffer for a delimiter. This should be | // the buffer. Search through the buffer for a delimiter. This should be | ||||
| // the common case. | // the common case. | ||||
| $endl = strpos($this->buf, $this->delimiter, $this->pos); | $endl = strpos($this->buf, $this->delimiter, $this->pos); | ||||
| if ($endl !== false) { | if ($endl !== false) { | ||||
| // We found a delimiter, so return the line it delimits. We leave | // We found a delimiter, so return the line it delimits. We leave | ||||
| // the buffer as-is so we don't need to reallocate it, in case it is | // the buffer as-is so we don't need to reallocate it, in case it is | ||||
| ▲ Show 20 Lines • Show All 53 Lines • Show Last 20 Lines | |||||