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 660c00f
Showing 1 changed file with 49 additions and 33 deletions.
82 changes: 49 additions & 33 deletions src/Internal/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class ArrayParser
use ForbidCloning;
use ForbidSerialization;

private const WHITESPACE_CHARS = " \n\r\t\v\0";

/**
* @param string $data String representation of PostgresSQL array.
* @param \Closure(string):mixed $cast Callback to cast parsed values.
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 readonly string $delimiter,
private int $position = 0,
) {
}

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

if ($this->data === '') {
$this->position = $this->skipWhitespace($this->position);

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

if ($this->data[0] !== '{') {
if ($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 +106,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 +141,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]) && \str_contains(self::WHITESPACE_CHARS, $this->data[$position])) {
++$position;
}

return $end;
return $position;
}
}

0 comments on commit 660c00f

Please sign in to comment.