Skip to content

Commit

Permalink
fix: json string inside json with comment like value breaks
Browse files Browse the repository at this point in the history
  • Loading branch information
adhocore committed Oct 1, 2022
1 parent e3628de commit d0d878a
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ protected function doStrip(string $json): string
$crlf = ["\n" => '\n', "\r" => '\r'];

while (isset($json[++$this->index])) {
$oldprev = $prev ?? '';
list($prev, $char, $next) = $this->getSegments($json);

$return = $this->checkTrail($char, $return);

if ($this->inStringOrCommentEnd($prev, $char, $char . $next)) {
if ($this->inStringOrCommentEnd($prev, $char, $char . $next, $oldprev)) {
$return .= $this->inStr && isset($crlf[$char]) ? $crlf[$char] : $char;

continue;
Expand Down Expand Up @@ -115,19 +116,19 @@ protected function checkTrail(string $char, string $json): string
return $json;
}

protected function inStringOrCommentEnd(string $prev, string $char, string $next): bool
protected function inStringOrCommentEnd(string $prev, string $char, string $next, string $oldprev): bool
{
return $this->inString($char, $prev, $next) || $this->inCommentEnd($next);
return $this->inString($char, $prev, $next, $oldprev) || $this->inCommentEnd($next);
}

protected function inString(string $char, string $prev, string $next): bool
protected function inString(string $char, string $prev, string $next, string $oldprev): bool
{
if (0 === $this->comment && $char === '"' && $prev !== '\\') {
return $this->inStr = !$this->inStr;
}

if ($this->inStr && \in_array($next, ['":', '",', '"]', '"}'], true)) {
$this->inStr = false;
$this->inStr = "$oldprev$prev" !== '\\\\';
}

return $this->inStr;
Expand Down

0 comments on commit d0d878a

Please sign in to comment.