Skip to content

Commit fb3a842

Browse files
committed
1 parent 5ab1882 commit fb3a842

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

src/main/java/com/github/difflib/unifieddiff/UnifiedDiffReader.java

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.io.Reader;
2525
import java.util.ArrayList;
2626
import java.util.List;
27+
import java.util.Objects;
2728
import java.util.function.BiConsumer;
2829
import java.util.logging.Level;
2930
import java.util.logging.Logger;
@@ -80,13 +81,9 @@ private UnifiedDiff parse() throws IOException, UnifiedDiffParserException {
8081

8182
while (line != null) {
8283
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);
8685
} else {
87-
if (processLine(line, CHUNK) == false) {
88-
throw new UnifiedDiffParserException("parsing error at line " + line);
89-
}
86+
processLine(line, CHUNK);
9087
}
9188
line = READER.readLine();
9289
}
@@ -117,15 +114,15 @@ public static UnifiedDiff parseUnifiedDiff(InputStream stream) throws IOExceptio
117114
return parser.parse();
118115
}
119116

120-
private boolean processLine(String line, UnifiedDiffLine... rules) throws UnifiedDiffParserException {
117+
private void processLine(String line, UnifiedDiffLine... rules) throws UnifiedDiffParserException {
121118
for (UnifiedDiffLine rule : rules) {
122119
if (rule.processLine(line)) {
123120
LOG.info(" >>> processed rule " + rule.toString());
124-
return true;
121+
return;
125122
}
126123
}
127124
LOG.info(" >>> no rule matched " + line);
128-
return false;
125+
throw new UnifiedDiffParserException("parsing error at line " + line);
129126
}
130127

131128
private void initFileIfNecessary() {
@@ -151,7 +148,9 @@ private void processDiff(MatchResult match, String line) {
151148
private List<String> originalTxt = new ArrayList<>();
152149
private List<String> revisedTxt = new ArrayList<>();
153150
private int old_ln;
151+
private int old_size;
154152
private int new_ln;
153+
private int new_size;
155154

156155
private void finalizeChunk() {
157156
if (!originalTxt.isEmpty() || !revisedTxt.isEmpty()) {
@@ -183,8 +182,10 @@ private void processDelLine(MatchResult match, String line) {
183182

184183
private void processChunk(MatchResult match, String chunkStart) {
185184
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);
188189
if (old_ln == 0) {
189190
old_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+
196201
private void processIndex(MatchResult match, String line) {
197202
initFileIfNecessary();
198203
LOG.log(Level.INFO, "index {0}", line);

0 commit comments

Comments
 (0)