Skip to content

Commit

Permalink
Merge pull request #6 from adhocore/refactor
Browse files Browse the repository at this point in the history
Refactor - split logic from main loop
  • Loading branch information
adhocore authored Jun 21, 2018
2 parents 02a1b00 + c18b0c3 commit b8e0321
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
62 changes: 38 additions & 24 deletions src/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
*/
class Comment
{
/** @var bool If current char is within a string */
protected $inStr = false;

/** @var int Lines of comments 0 = no comment, 1 = single line, 2 = multi lines */
protected $comment = 0;

/**
* Strip comments from JSON string.
*
Expand All @@ -22,40 +28,21 @@ public function strip($json)
return $json;
}

$index = -1;
$inStr = false;
$return = '';
$char = '';
$comment = 'none';
list($index, $return, $char) = [-1, '', ''];

while (isset($json[++$index])) {
list($prev, $char) = [$char, $json[$index]];

if ('none' === $comment && $char === '"' && $prev !== '\\') {
$inStr = !$inStr;
}

$charnext = $char . (isset($json[$index + 1]) ? $json[$index + 1] : '');

if (!$inStr && 'none' === $comment) {
$comment = $charnext === '//' ? 'single' : ($charnext === '/*' ? 'multi' : 'none');
}

if ($inStr || 'none' === $comment) {
if ($this->inStringOrCommentEnd($prev, $char, $charnext)) {
$return .= $char;

continue;
}

if (($comment === 'single' && $char == "\n")
|| ($comment === 'multi' && $charnext == '*/')
) {
// Cosmetic fix only!
if ($comment === 'single') {
$return = rtrim($return) . $char;
}

$comment = 'none';
$wasSingle = 1 === $this->comment;
if ($this->hasCommentEnded($char, $charnext) && $wasSingle) {
$return = rtrim($return) . $char;
}

$index += $charnext === '*/' ? 1 : 0;
Expand All @@ -64,6 +51,33 @@ public function strip($json)
return $return;
}

protected function inStringOrCommentEnd($prev, $char, $charnext)
{
if (0 === $this->comment && $char === '"' && $prev !== '\\') {
$this->inStr = !$this->inStr;
}

if (!$this->inStr && 0 === $this->comment) {
$this->comment = $charnext === '//' ? 1 : ($charnext === '/*' ? 2 : 0);
}

return $this->inStr || 0 === $this->comment;
}

protected function hasCommentEnded($char, $charnext)
{
$singleEnded = $this->comment === 1 && $char == "\n";
$multiEnded = $this->comment === 2 && $charnext == '*/';

if ($singleEnded || $multiEnded) {
$this->comment = 0;

return true;
}

return false;
}

/**
* Strip comments and decode JSON string.
*
Expand Down
3 changes: 0 additions & 3 deletions tests/CommentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@

use Ahc\Json\Comment;

/** @coversDefaultClass \Ahc\Json\Comment */
class CommentTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider theTests
* @covers \Ahc\Json\Comment::strip
*/
public function testStrip($json, $expect)
{
Expand All @@ -18,7 +16,6 @@ public function testStrip($json, $expect)

/**
* @dataProvider theTests
* @covers \Ahc\Json\Comment::decode
*/
public function testDecode($json)
{
Expand Down

0 comments on commit b8e0321

Please sign in to comment.