Skip to content

Commit

Permalink
Reduce string manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Nov 20, 2024
1 parent e793c96 commit 316ae46
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
80 changes: 46 additions & 34 deletions src/Internal/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
*/
final class ArrayParser
{
private const WHITESPACE = [" ", "\n", "\r", "\t", "\v", "\0"];

use ForbidCloning;
use ForbidSerialization;

Expand All @@ -25,12 +27,10 @@ final class ArrayParser
*/
public static function parse(string $data, \Closure $cast, string $delimiter = ','): array
{
$data = \trim($data);

$parser = new self($data, $cast, $delimiter);
$result = $parser->parseToArray();

if ($parser->data !== '') {
if (isset($parser->data[$parser->position])) {
throw new PostgresParseException("Data left in buffer after parsing");
}

Expand All @@ -43,9 +43,10 @@ public static function parse(string $data, \Closure $cast, string $delimiter = '
* @param string $delimiter Delimiter used to separate values.
*/
private function __construct(
private string $data,
private readonly string $data,
private readonly \Closure $cast,
private readonly string $delimiter = ',',
private int $position = 0,
) {
}

Expand All @@ -58,36 +59,35 @@ private function parseToArray(): array
{
$result = [];

if ($this->data === '') {
throw new PostgresParseException("Unexpected end of data");
}
$this->position = $this->skipWhitespace($this->position);

if ($this->data[0] !== '{') {
if (!isset($this->data[$this->position]) || $this->data[$this->position] !== '{') {
throw new PostgresParseException("Missing opening bracket");
}

$this->data = \ltrim(\substr($this->data, 1));
$this->position = $this->skipWhitespace($this->position + 1);

do {
if ($this->data === '') {
if (!isset($this->data[$this->position])) {
throw new PostgresParseException("Unexpected end of data");
}

if ($this->data[0] === '}') { // Empty array
$this->data = \ltrim(\substr($this->data, 1));
if ($this->data[$this->position] === '}') { // Empty array
$this->position = $this->skipWhitespace($this->position + 1);
break;
}

if ($this->data[0] === '{') { // Array
$parser = new self($this->data, $this->cast, $this->delimiter);
if ($this->data[$this->position] === '{') { // Array
$parser = new self($this->data, $this->cast, $this->delimiter, $this->position);
$result[] = $parser->parseToArray();
$this->data = $parser->data;
$end = $this->trim(0);
$this->position = $parser->position;
$delimiter = $this->moveToNextDelimiter($this->position);
continue;
}

if ($this->data[0] === '"') { // Quoted value
for ($position = 1; isset($this->data[$position]); ++$position) {
if ($this->data[$this->position] === '"') { // Quoted value
++$this->position;
for ($position = $this->position; isset($this->data[$position]); ++$position) {
if ($this->data[$position] === '\\') {
++$position; // Skip next character
continue;
Expand All @@ -102,27 +102,30 @@ private function parseToArray(): array
throw new PostgresParseException("Could not find matching quote in quoted value");
}

$yield = \stripslashes(\substr($this->data, 1, $position - 1));
$entry = \stripslashes(\substr($this->data, $this->position, $position - $this->position));

$end = $this->trim($position + 1);
$delimiter = $this->moveToNextDelimiter($position + 1);
} else { // Unquoted value
$position = 0;
while (isset($this->data[$position]) && $this->data[$position] !== $this->delimiter && $this->data[$position] !== '}') {
$position = $this->position;
while (isset($this->data[$position])
&& $this->data[$position] !== $this->delimiter
&& $this->data[$position] !== '}'
) {
++$position;
}

$yield = \trim(\substr($this->data, 0, $position));
$entry = \trim(\substr($this->data, $this->position, $position - $this->position));

$end = $this->trim($position);
$delimiter = $this->moveToNextDelimiter($position);

if (\strcasecmp($yield, "NULL") === 0) { // Literal NULL is always unquoted.
if (\strcasecmp($entry, "NULL") === 0) { // Literal NULL is always unquoted.
$result[] = null;
continue;
}
}

$result[] = ($this->cast)($yield);
} while ($end !== '}');
$result[] = ($this->cast)($entry);
} while ($delimiter !== '}');

return $result;
}
Expand All @@ -134,22 +137,31 @@ private function parseToArray(): array
*
* @throws PostgresParseException
*/
private function trim(int $position): string
private function moveToNextDelimiter(int $position): string
{
$this->data = \ltrim(\substr($this->data, $position));
$position = $this->skipWhitespace($position);

if ($this->data === '') {
if (!isset($this->data[$position])) {
throw new PostgresParseException("Unexpected end of data");
}

$end = $this->data[0];
$delimiter = $this->data[$position];

if ($end !== $this->delimiter && $end !== '}') {
if ($delimiter !== $this->delimiter && $delimiter !== '}') {
throw new PostgresParseException("Invalid delimiter");
}

$this->data = \ltrim(\substr($this->data, 1));
$this->position = $this->skipWhitespace($position + 1);

return $delimiter;
}

private function skipWhitespace(int $position): int
{
while (isset($this->data[$position]) && \in_array($this->data[$position], self::WHITESPACE, true)) {
++$position;
}

return $end;
return $position;
}
}
2 changes: 1 addition & 1 deletion test/ArrayParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function testMalformedNestedArray(): void
public function testEmptyString(): void
{
$this->expectException(PostgresParseException::class);
$this->expectExceptionMessage('Unexpected end of data');
$this->expectExceptionMessage('Missing opening bracket');

$string = ' ';
ArrayParser::parse($string, $this->getDefaultCastFunction());
Expand Down

0 comments on commit 316ae46

Please sign in to comment.