@@ -80,6 +80,9 @@ public function parse($string)
80
80
$ diff = new Diff ($ fromMatch ['file ' ], $ toMatch ['file ' ]);
81
81
++$ i ;
82
82
} else {
83
+ if (preg_match ('/^(?:diff --git |index [\da-f\.]+|[+-]{3} [ab])/ ' , $ lines [$ i ])) {
84
+ continue ;
85
+ }
83
86
$ collected [] = $ lines [$ i ];
84
87
}
85
88
}
@@ -100,45 +103,34 @@ private function parseFileDiff(Diff $diff, array $lines)
100
103
{
101
104
$ chunks = array ();
102
105
103
- while (count ($ lines )) {
104
- while (!preg_match ('(^@@ \\s+-(?P<start> \\d+)(?:, \\s*(?P<startrange> \\d+))? \\s+ \\+(?P<end> \\d+)(?:, \\s*(?P<endrange> \\d+))? \\s+@@) ' , $ last = array_shift ($ lines ), $ match )) {
105
- if ($ last === null ) {
106
- break 2 ;
107
- }
106
+ foreach ($ lines as $ line ) {
107
+ if (preg_match ('/^@@\s+-(?P<start>\d+)(?:,\s*(?P<startrange>\d+))?\s+\+(?P<end>\d+)(?:,\s*(?P<endrange>\d+))?\s+@@/ ' , $ line , $ match )) {
108
+ $ chunk = new Chunk (
109
+ $ match ['start ' ],
110
+ isset ($ match ['startrange ' ]) ? max (1 , $ match ['startrange ' ]) : 1 ,
111
+ $ match ['end ' ],
112
+ isset ($ match ['endrange ' ]) ? max (1 , $ match ['endrange ' ]) : 1
113
+ );
114
+
115
+ $ chunks [] = $ chunk ;
116
+ $ diffLines = array ();
117
+ continue ;
108
118
}
109
119
110
- $ chunk = new Chunk (
111
- $ match ['start ' ],
112
- isset ($ match ['startrange ' ]) ? max (1 , $ match ['startrange ' ]) : 1 ,
113
- $ match ['end ' ],
114
- isset ($ match ['endrange ' ]) ? max (1 , $ match ['endrange ' ]) : 1
115
- );
116
-
117
- $ diffLines = array ();
118
- $ last = null ;
119
-
120
- while (count ($ lines ) &&
121
- (preg_match ('(^(?P<type>[+ -])?(?P<line>.*)) ' , $ last = array_shift ($ lines ), $ match ) ||
122
- (strpos ($ last , '\\ No newline at end of file ' ) === 0 ))) {
123
- if (count ($ match )) {
124
- $ type = Line::UNCHANGED ;
120
+ if (preg_match ('/^(?P<type>[+ -])?(?P<line>.*)/ ' , $ line , $ match )) {
121
+ $ type = Line::UNCHANGED ;
125
122
126
- if ($ match ['type ' ] == '+ ' ) {
127
- $ type = Line::ADDED ;
128
- } elseif ($ match ['type ' ] == '- ' ) {
129
- $ type = Line::REMOVED ;
130
- }
131
-
132
- $ diffLines [] = new Line ($ type , $ match ['line ' ]);
123
+ if ($ match ['type ' ] == '+ ' ) {
124
+ $ type = Line::ADDED ;
125
+ } elseif ($ match ['type ' ] == '- ' ) {
126
+ $ type = Line::REMOVED ;
133
127
}
134
- }
135
128
136
- $ chunk -> setLines ( $ diffLines );
129
+ $ diffLines [] = new Line ( $ type , $ match [ ' line ' ] );
137
130
138
- $ chunks [] = $ chunk ;
139
-
140
- if ($ last !== null ) {
141
- array_unshift ($ lines , $ last );
131
+ if (isset ($ chunk )) {
132
+ $ chunk ->setLines ($ diffLines );
133
+ }
142
134
}
143
135
}
144
136