24
24
import java .io .Reader ;
25
25
import java .util .ArrayList ;
26
26
import java .util .List ;
27
+ import java .util .Objects ;
27
28
import java .util .function .BiConsumer ;
28
29
import java .util .logging .Level ;
29
30
import java .util .logging .Logger ;
@@ -80,13 +81,9 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
80
81
81
82
while (line !=null ) {
82
83
if (!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 );
86
85
}else {
87
- if (processLine (line ,CHUNK ) ==false ) {
88
- throw new UnifiedDiffParserException ("parsing error at line " +line );
89
- }
86
+ processLine (line ,CHUNK );
90
87
}
91
88
line =READER .readLine ();
92
89
}
@@ -117,15 +114,15 @@ public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOExceptio
117
114
return parser .parse ();
118
115
}
119
116
120
- private boolean processLine (String line ,UnifiedDiffLine ...rules )throws UnifiedDiffParserException {
117
+ private void processLine (String line ,UnifiedDiffLine ...rules )throws UnifiedDiffParserException {
121
118
for (UnifiedDiffLine rule :rules ) {
122
119
if (rule .processLine (line )) {
123
120
LOG .info (" >>> processed rule " +rule .toString ());
124
- return true ;
121
+ return ;
125
122
}
126
123
}
127
124
LOG .info (" >>> no rule matched " +line );
128
- return false ;
125
+ throw new UnifiedDiffParserException ( "parsing error at line " + line ) ;
129
126
}
130
127
131
128
private void initFileIfNecessary () {
@@ -151,7 +148,9 @@ private void processDiff(MatchResult match, String line) {
151
148
private List <String >originalTxt =new ArrayList <>();
152
149
private List <String >revisedTxt =new ArrayList <>();
153
150
private int old_ln ;
151
+ private int old_size ;
154
152
private int new_ln ;
153
+ private int new_size ;
155
154
156
155
private void finalizeChunk () {
157
156
if (!originalTxt .isEmpty () || !revisedTxt .isEmpty ()) {
@@ -183,8 +182,10 @@ private void processDelLine(MatchResult match, String line) {
183
182
184
183
private void processChunk (MatchResult match ,String chunkStart ) {
185
184
finalizeChunk ();
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 );
188
189
if (old_ln ==0 ) {
189
190
old_ln =1 ;
190
191
}
@@ -193,6 +194,10 @@ private void processChunk(MatchResult match, String chunkStart) {
193
194
}
194
195
}
195
196
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
+
196
201
private void processIndex (MatchResult match ,String line ) {
197
202
initFileIfNecessary ();
198
203
LOG .log (Level .INFO ,"index {0}" ,line );