1515 */
1616package com .github .difflib .unifieddiff ;
1717
18+ import com .github .difflib .patch .ChangeDelta ;
19+ import com .github .difflib .patch .Chunk ;
1820import java .io .BufferedReader ;
1921import java .io .IOException ;
2022import java .io .InputStream ;
3537 */
3638public final class UnifiedDiffParser {
3739
38- private static final String UNIFIED_DIFF_CHUNK_REGEXP = "^@@\\ s+-(?:(\\ d+)(?:,(\\ d+))?)\\ s+\\ +(?:(\\ d+)(?:,(\\ d+))?)\\ s+@@$" ;
40+ static final Pattern UNIFIED_DIFF_CHUNK_REGEXP = Pattern . compile ( "^@@\\ s+-(?:(\\ d+)(?:,(\\ d+))?)\\ s+\\ +(?:(\\ d+)(?:,(\\ d+))?)\\ s+@@" ) ;
3941
4042 private final UnifiedDiffReader READER ;
4143 private final UnifiedDiff data = new UnifiedDiff ();
@@ -62,6 +64,7 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
6264 String headerTxt = "" ;
6365 while (READER .ready ()) {
6466 String line = READER .readLine ();
67+ LOG .log (Level .INFO , "parsing line {0}" , line );
6568 if (processLine (header , line ) == false ) {
6669 if (header ) {
6770 headerTxt += line + "\n " ;
@@ -118,25 +121,54 @@ public void processDiff(MatchResult match, String line) {
118121 actualFile .setDiffCommand (line );
119122 }
120123
121- public void processChunk (MatchResult match , String chunkStart ) {
124+ public void processChunk (MatchResult _match , String chunkStart ) {
125+ MatchResult match = _match ;
122126 try {
123- List <String > originalTxt = new ArrayList <>();
124- List <String > revisedTxt = new ArrayList <>();
125127
126- int old_ln = match .group (1 ) == null ? 1 : Integer .parseInt (match .group (1 ));
127- int new_ln = match .group (3 ) == null ? 1 : Integer .parseInt (match .group (3 ));
128+ while (true ) {
128129
129- while ( this . READER . ready ()) {
130- String line = READER . readLine ();
130+ List < String > originalTxt = new ArrayList <>();
131+ List < String > revisedTxt = new ArrayList <> ();
131132
132- if (line .startsWith (" " ) || line .startsWith ("+" )) {
133- revisedTxt .add (line .substring (1 ));
133+ int old_ln = match .group (1 ) == null ? 1 : Integer .parseInt (match .group (1 ));
134+ int new_ln = match .group (3 ) == null ? 1 : Integer .parseInt (match .group (3 ));
135+ if (old_ln == 0 ) {
136+ old_ln = 1 ;
134137 }
135- if (line . startsWith ( " " ) || line . startsWith ( "-" ) ) {
136- originalTxt . add ( line . substring ( 1 )) ;
138+ if (new_ln == 0 ) {
139+ new_ln = 1 ;
137140 }
138- if (line .equals ("" )) {
141+
142+ while (this .READER .ready ()) {
143+ String line = READER .readLine ();
144+ LOG .log (Level .INFO , "processing chunk line {0}" , line );
145+
146+ if (line .startsWith (" " ) || line .startsWith ("+" )) {
147+ revisedTxt .add (line .substring (1 ));
148+ }
149+ if (line .startsWith (" " ) || line .startsWith ("-" )) {
150+ originalTxt .add (line .substring (1 ));
151+ }
152+ if (line .equals ("" ) || line .startsWith ("@@" ) || line .startsWith ("--" )) {
153+ break ;
154+ }
155+ }
156+
157+ actualFile .getPatch ().addDelta (new ChangeDelta <>(new Chunk <>(
158+ old_ln - 1 , originalTxt ), new Chunk <>(
159+ new_ln - 1 , revisedTxt )));
160+
161+ if (READER .lastLine ().equals ("" )
162+ || READER .lastLine ().startsWith ("--" )
163+ || !READER .lastLine ().startsWith ("@@" )) {
139164 break ;
165+ } else {
166+ Matcher m = UNIFIED_DIFF_CHUNK_REGEXP .matcher (READER .lastLine ());
167+ if (m .find ()) {
168+ match = m .toMatchResult ();
169+ } else {
170+ break ;
171+ }
140172 }
141173 }
142174
@@ -182,6 +214,12 @@ public UnifiedDiffLine(boolean stopsHeaderParsing, String pattern, BiConsumer<Ma
182214 this .stopsHeaderParsing = stopsHeaderParsing ;
183215 }
184216
217+ public UnifiedDiffLine (boolean stopsHeaderParsing , Pattern pattern , BiConsumer <MatchResult , String > command ) {
218+ this .pattern = pattern ;
219+ this .command = command ;
220+ this .stopsHeaderParsing = stopsHeaderParsing ;
221+ }
222+
185223 public boolean processLine (String line ) throws UnifiedDiffParserException {
186224 Matcher m = pattern .matcher (line );
187225 if (m .find ()) {
0 commit comments