Skip to content

Commit d0d878a

Browse files
committed
fix: json string inside json with comment like value breaks
1 parent e3628de commit d0d878a

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/Comment.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,12 @@ protected function doStrip(string $json): string
6363
$crlf = ["\n" => '\n', "\r" => '\r'];
6464

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

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

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

7374
continue;
@@ -115,19 +116,19 @@ protected function checkTrail(string $char, string $json): string
115116
return $json;
116117
}
117118

118-
protected function inStringOrCommentEnd(string $prev, string $char, string $next): bool
119+
protected function inStringOrCommentEnd(string $prev, string $char, string $next, string $oldprev): bool
119120
{
120-
return $this->inString($char, $prev, $next) || $this->inCommentEnd($next);
121+
return $this->inString($char, $prev, $next, $oldprev) || $this->inCommentEnd($next);
121122
}
122123

123-
protected function inString(string $char, string $prev, string $next): bool
124+
protected function inString(string $char, string $prev, string $next, string $oldprev): bool
124125
{
125126
if (0 === $this->comment && $char === '"' && $prev !== '\\') {
126127
return $this->inStr = !$this->inStr;
127128
}
128129

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

133134
return $this->inStr;

0 commit comments

Comments
 (0)