Skip to content

Commit

Permalink
Do not include delimiter in sent string
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jun 1, 2017
1 parent b5d2a8d commit 7543f30
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 0 additions & 1 deletion examples/simple-parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
$generator = function (callable $printer): \Generator {
while (true) {
$buffer = yield "\n"; // Reads until a new-line character is found.
$buffer = trim($buffer);
$printer($buffer); // Use the received data.
}
};
Expand Down
12 changes: 6 additions & 6 deletions lib/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,20 @@ private function send(string $data, bool $end = false): Promise {
break; // Too few bytes in buffer.
}

$length = $this->delimiter;
$send = \substr($this->buffer, 0, $this->delimiter);
$this->buffer = \substr($this->buffer, $this->delimiter);
} elseif (\is_string($this->delimiter)) {
if (($position = \strpos($this->buffer, $this->delimiter)) === false) {
break;
}

$length = $position + \strlen($this->delimiter);
$send = \substr($this->buffer, 0, $position);
$this->buffer = \substr($this->buffer, $position + \strlen($this->delimiter));
} else {
$length = \strlen($this->buffer);
$send = $this->buffer;
$this->buffer = "";
}

$send = \substr($this->buffer, 0, $length);
$this->buffer = \substr($this->buffer, $length);

try {
$this->delimiter = $this->generator->send($send);
} catch (\Exception $exception) { // Wrap Exception instances into a StreamException.
Expand Down

0 comments on commit 7543f30

Please sign in to comment.