From 98586feb4df7ab999f104ad63dee8d36b34537af Mon Sep 17 00:00:00 2001 From: PrinsFrank <25006490+PrinsFrank@users.noreply.github.com> Date: Fri, 21 Aug 2026 21:01:12 +0200 Subject: [PATCH] Remove usage of infiniteBuffer --- .../DictionaryParseContext/NestingContext.php | 43 +++++++++-------- src/Document/Dictionary/DictionaryParser.php | 23 ++++----- .../Generic/Parsing/InfiniteBuffer.php | 48 ------------------- 3 files changed, 32 insertions(+), 82 deletions(-) delete mode 100644 src/Document/Generic/Parsing/InfiniteBuffer.php diff --git a/src/Document/Dictionary/DictionaryParseContext/NestingContext.php b/src/Document/Dictionary/DictionaryParseContext/NestingContext.php index 87651761..b0a745e4 100644 --- a/src/Document/Dictionary/DictionaryParseContext/NestingContext.php +++ b/src/Document/Dictionary/DictionaryParseContext/NestingContext.php @@ -3,8 +3,6 @@ namespace PrinsFrank\PdfParser\Document\Dictionary\DictionaryParseContext; -use PrinsFrank\PdfParser\Document\Generic\Parsing\InfiniteBuffer; - /** @internal */ class NestingContext { private string $currentLevel; @@ -12,10 +10,10 @@ class NestingContext { /** @var array */ private array $nestingContext = []; - /** @var array */ + /** @var array */ private array $keyBuffer = []; - /** @var array */ + /** @var array */ private array $valueBuffer = []; public function __construct() { @@ -24,12 +22,16 @@ public function __construct() { public function incrementNesting(): self { $this->currentLevel = (string) ($this->keyBuffer[$this->currentLevel] ?? (int) $this->currentLevel + 1); + $this->keyBuffer[$this->currentLevel] = ''; + $this->valueBuffer[$this->currentLevel] = ''; return $this; } public function decrementNesting(): self { array_pop($this->nestingContext); + unset($this->keyBuffer[$this->currentLevel]); + unset($this->valueBuffer[$this->currentLevel]); $this->currentLevel = (string) array_key_last($this->nestingContext); return $this; @@ -45,44 +47,47 @@ public function getContext(): DictionaryParseContext { return $this->nestingContext[$this->currentLevel] ?? DictionaryParseContext::ROOT; } - public function getKeyBuffer(): InfiniteBuffer { - return $this->keyBuffer[$this->currentLevel] ??= new InfiniteBuffer(); + public function addToKeyBuffer(string $char): void { + $this->keyBuffer[$this->currentLevel] .= $char; } - public function addToKeyBuffer(string $char): self { - $this->getKeyBuffer()->addChar($char); + public function removeCharFromKeyBuffer(): void { + $this->keyBuffer[$this->currentLevel] = substr($this->keyBuffer[$this->currentLevel], 0, -1); + } - return $this; + public function getKeyBuffer(): string { + return $this->keyBuffer[$this->currentLevel] ?? ''; } - public function getValueBuffer(): InfiniteBuffer { - return $this->valueBuffer[$this->currentLevel] ??= new InfiniteBuffer(); + public function addToValueBuffer(string $char): void { + $this->valueBuffer[$this->currentLevel] .= $char; } - public function addToValueBuffer(string $char): self { - $this->getValueBuffer()->addChar($char); + public function removeCharFromValueBuffer(): void { + $this->valueBuffer[$this->currentLevel] = substr($this->valueBuffer[$this->currentLevel], 0, -1); + } - return $this; + public function getValueBuffer(): string { + return $this->valueBuffer[$this->currentLevel] ?? ''; } /** @return list */ public function getKeysFromRoot(): array { $keysFromRoot = []; foreach ($this->keyBuffer as $keyBuffer) { - $keyBufferString = (string) $keyBuffer; - if ($keyBufferString === '') { + if ($keyBuffer === '') { continue; } - $keysFromRoot[] = $keyBufferString; + $keysFromRoot[] = $keyBuffer; } return $keysFromRoot; } public function flush(): self { - ($this->valueBuffer[$this->currentLevel] ?? null)?->flush(); - ($this->keyBuffer[$this->currentLevel] ?? null)?->flush(); + $this->valueBuffer[$this->currentLevel] = ''; + $this->keyBuffer[$this->currentLevel] = ''; return $this; } diff --git a/src/Document/Dictionary/DictionaryParser.php b/src/Document/Dictionary/DictionaryParser.php index 5e77e601..c9ce24da 100644 --- a/src/Document/Dictionary/DictionaryParser.php +++ b/src/Document/Dictionary/DictionaryParser.php @@ -23,8 +23,6 @@ class DictionaryParser { public static function parse(?EncryptionContext $encryptionContext, Stream $stream, int $startPos, int $nrOfBytes): Dictionary { $dictionaryArray = []; $nestingContext = (new NestingContext())->setContext(DictionaryParseContext::ROOT); - $keyBuffer = $nestingContext->getKeyBuffer(); - $valueBuffer = $nestingContext->getValueBuffer(); $arrayNestingLevel = 0; $previousChar = $secondToLastChar = $currentContext = $contextBeforeComment = $previousIndexLevelDecrease = $previousIndexLevelIncrease = null; foreach ($stream->chars($startPos, $nrOfBytes) as $index => $char) { @@ -34,14 +32,12 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre && $currentContext !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS && $previousIndexLevelIncrease !== $index - 1) { if ($currentContext === DictionaryParseContext::KEY) { - $keyBuffer->removeChar(); + $nestingContext->removeCharFromKeyBuffer(); } $previousIndexLevelIncrease = $index; $nestingContext->setContext(DictionaryParseContext::DICTIONARY)->incrementNesting()->setContext(DictionaryParseContext::DICTIONARY); $currentContext = DictionaryParseContext::DICTIONARY; - $keyBuffer = $nestingContext->getKeyBuffer(); - $valueBuffer = $nestingContext->getValueBuffer(); } elseif ($char === DelimiterCharacter::LESS_THAN_SIGN->value && $currentContext === DictionaryParseContext::KEY) { $nestingContext->setContext(DictionaryParseContext::VALUE); $currentContext = DictionaryParseContext::VALUE; @@ -50,13 +46,11 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre && $secondToLastChar !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value && $currentContext !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS && $previousIndexLevelDecrease !== $index - 1) { - $valueBuffer->removeChar(); + $nestingContext->removeCharFromValueBuffer(); self::flush($dictionaryArray, $nestingContext); $previousIndexLevelDecrease = $index; $nestingContext->decrementNesting()->flush(); $currentContext = $nestingContext->getContext(); - $keyBuffer = $nestingContext->getKeyBuffer(); - $valueBuffer = $nestingContext->getValueBuffer(); } elseif ($char === DelimiterCharacter::SOLIDUS->value && $previousChar !== LiteralStringEscapeCharacter::REVERSE_SOLIDUS->value && $currentContext !== DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS) { @@ -118,11 +112,11 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre $secondToLastChar = $previousChar; $previousChar = $char; if ($currentContext === DictionaryParseContext::KEY) { - $keyBuffer->addChar($char); + $nestingContext->addToKeyBuffer($char); } elseif ($currentContext === DictionaryParseContext::VALUE_IN_PARENTHESES || $currentContext === DictionaryParseContext::VALUE_IN_SQUARE_BRACKETS || $currentContext === DictionaryParseContext::VALUE) { - $valueBuffer->addChar($char); + $nestingContext->addToValueBuffer($char); } } @@ -131,14 +125,13 @@ public static function parse(?EncryptionContext $encryptionContext, Stream $stre /** @param array $dictionaryArray */ private static function flush(array &$dictionaryArray, NestingContext $nestingContext): void { - if ($nestingContext->getValueBuffer()->isEmpty() || $nestingContext->getKeyBuffer()->isEmpty()) { + if (($valueBuffer = $nestingContext->getValueBuffer()) === '' || ($keyBuffer = $nestingContext->getKeyBuffer()) === '') { return; } $dictionaryArrayPointer = &$dictionaryArray; - $keys = $nestingContext->getKeysFromRoot(); - foreach ($keys as $index => $key) { - if ($key === (string) $nestingContext->getKeyBuffer() && $index === array_key_last($keys)) { + foreach (($keys = $nestingContext->getKeysFromRoot()) as $index => $key) { + if ($key === $keyBuffer && $index === array_key_last($keys)) { break; } @@ -147,7 +140,7 @@ private static function flush(array &$dictionaryArray, NestingContext $nestingCo } /** @phpstan-ignore offsetAccess.nonOffsetAccessible */ - $dictionaryArrayPointer[(string) $nestingContext->getKeyBuffer()] = trim((string) $nestingContext->getValueBuffer()); + $dictionaryArrayPointer[$keyBuffer] = trim($valueBuffer); $nestingContext->flush(); } } diff --git a/src/Document/Generic/Parsing/InfiniteBuffer.php b/src/Document/Generic/Parsing/InfiniteBuffer.php deleted file mode 100644 index 97418ef9..00000000 --- a/src/Document/Generic/Parsing/InfiniteBuffer.php +++ /dev/null @@ -1,48 +0,0 @@ -buffer .= $char; - - return $this; - } - - public function flush(): self { - return $this->setValue(''); - } - - #[Override] - public function __toString(): string { - return $this->buffer; - } - - public function getLength(): int { - return strlen($this->buffer); - } - - public function isEmpty(): bool { - return $this->getLength() === 0; - } - - public function setValue(string $buffer): self { - $this->buffer = $buffer; - - return $this; - } - - public function removeChar(): self { - if ($this->buffer !== '') { - $this->buffer = substr($this->buffer, 0, -1); - } - - return $this; - } -}