9
9
*/
10
10
class Comment
11
11
{
12
+ /** @var int The current index being scanned */
13
+ protected $ index = -1 ;
14
+
12
15
/** @var bool If current char is within a string */
13
16
protected $ inStr = false ;
14
17
@@ -28,35 +31,49 @@ public function strip($json)
28
31
return $ json ;
29
32
}
30
33
31
- list ( $ index , $ return , $ char ) = [- 1 , '' , '' ] ;
34
+ $ this -> reset () ;
32
35
33
- while (isset ($ json [++$ index ])) {
34
- list ($ prev , $ char ) = [$ char , $ json [$ index ]];
36
+ return $ this ->doStrip ($ json );
37
+ }
38
+
39
+ protected function reset ()
40
+ {
41
+ $ this ->index = -1 ;
42
+ $ this ->inStr = false ;
43
+ $ this ->comment = 0 ;
44
+ }
35
45
36
- $ charnext = $ char . (isset ($ json [$ index + 1 ]) ? $ json [$ index + 1 ] : '' );
37
- if ($ this ->inStringOrCommentEnd ($ prev , $ char , $ charnext )) {
46
+ protected function doStrip ($ json )
47
+ {
48
+ $ return = '' ;
49
+
50
+ while (isset ($ json [++$ this ->index ])) {
51
+ list ($ prev , $ char , $ next ) = $ this ->getSegments ($ json );
52
+
53
+ if ($ this ->inStringOrCommentEnd ($ prev , $ char , $ char . $ next )) {
38
54
$ return .= $ char ;
39
55
40
56
continue ;
41
57
}
42
58
43
59
$ wasSingle = 1 === $ this ->comment ;
44
- if ($ this ->hasCommentEnded ($ char , $ charnext ) && $ wasSingle ) {
60
+ if ($ this ->hasCommentEnded ($ char , $ char . $ next ) && $ wasSingle ) {
45
61
$ return = \rtrim ($ return ) . $ char ;
46
62
}
47
63
48
- $ index += $ charnext === '*/ ' ? 1 : 0 ;
64
+ $ this -> index += $ char . $ next === '*/ ' ? 1 : 0 ;
49
65
}
50
66
51
- $ this ->reset ();
52
-
53
67
return $ return ;
54
68
}
55
69
56
- protected function reset ( )
70
+ protected function getSegments ( $ json )
57
71
{
58
- $ this ->comment = 0 ;
59
- $ this ->inStr = false ;
72
+ return [
73
+ isset ($ json [$ this ->index - 1 ]) ? $ json [$ this ->index - 1 ] : '' ,
74
+ $ json [$ this ->index ],
75
+ isset ($ json [$ this ->index + 1 ]) ? $ json [$ this ->index + 1 ] : '' ,
76
+ ];
60
77
}
61
78
62
79
protected function inStringOrCommentEnd ($ prev , $ char , $ charnext )
0 commit comments