2424import java .io .Reader ;
2525import java .util .ArrayList ;
2626import java .util .List ;
27+ import java .util .Objects ;
2728import java .util .function .BiConsumer ;
2829import java .util .logging .Level ;
2930import java .util .logging .Logger ;
@@ -80,13 +81,9 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
8081
8182while (line !=null ) {
8283if (!CHUNK .validLine (line )) {
83- if (processLine (line ,DIFF_COMMAND ,INDEX ,FROM_FILE ,TO_FILE ) ==false ) {
84- throw new UnifiedDiffParserException ("parsing error at line " +line );
85- }
84+ processLine (line ,DIFF_COMMAND ,INDEX ,FROM_FILE ,TO_FILE );
8685 }else {
87- if (processLine (line ,CHUNK ) ==false ) {
88- throw new UnifiedDiffParserException ("parsing error at line " +line );
89- }
86+ processLine (line ,CHUNK );
9087 }
9188line =READER .readLine ();
9289 }
@@ -117,15 +114,15 @@ public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOExceptio
117114return parser .parse ();
118115 }
119116
120- private boolean processLine (String line ,UnifiedDiffLine ...rules )throws UnifiedDiffParserException {
117+ private void processLine (String line ,UnifiedDiffLine ...rules )throws UnifiedDiffParserException {
121118for (UnifiedDiffLine rule :rules ) {
122119if (rule .processLine (line )) {
123120LOG .info (" >>> processed rule " +rule .toString ());
124- return true ;
121+ return ;
125122 }
126123 }
127124LOG .info (" >>> no rule matched " +line );
128- return false ;
125+ throw new UnifiedDiffParserException ( "parsing error at line " + line ) ;
129126 }
130127
131128private void initFileIfNecessary () {
@@ -151,7 +148,9 @@ private void processDiff(MatchResult match, String line) {
151148private List <String >originalTxt =new ArrayList <>();
152149private List <String >revisedTxt =new ArrayList <>();
153150private int old_ln ;
151+ private int old_size ;
154152private int new_ln ;
153+ private int new_size ;
155154
156155private void finalizeChunk () {
157156if (!originalTxt .isEmpty () || !revisedTxt .isEmpty ()) {
@@ -183,8 +182,10 @@ private void processDelLine(MatchResult match, String line) {
183182
184183private void processChunk (MatchResult match ,String chunkStart ) {
185184finalizeChunk ();
186- old_ln =match .group (1 ) ==null ?1 :Integer .parseInt (match .group (1 ));
187- new_ln =match .group (3 ) ==null ?1 :Integer .parseInt (match .group (3 ));
185+ old_ln =toInteger (match ,1 ,1 );
186+ old_size =toInteger (match ,2 ,0 );
187+ new_ln =toInteger (match ,3 ,1 );
188+ new_size =toInteger (match ,4 ,0 );
188189if (old_ln ==0 ) {
189190old_ln =1 ;
190191 }
@@ -193,6 +194,10 @@ private void processChunk(MatchResult match, String chunkStart) {
193194 }
194195 }
195196
197+ private static Integer toInteger (MatchResult match ,int group ,int defValue )throws NumberFormatException {
198+ return Integer .valueOf (Objects .toString (match .group (group ) ,"" +defValue ));
199+ }
200+
196201private void processIndex (MatchResult match ,String line ) {
197202initFileIfNecessary ();
198203LOG .log (Level .INFO ,"index {0}" ,line );