Skip to content

Extract duplicate code into method. #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 18, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,7 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
Matcher m = UNIFIED_DIFF_CHUNK_REGEXP.matcher(line);
if (m.find()) {
// Process the lines in the previous chunk
if (!rawChunk.isEmpty()) {
List<String> oldChunkLines = new ArrayList<>();
List<String> newChunkLines = new ArrayList<>();

for (String[] raw_line : rawChunk) {
tag = raw_line[0];
rest = raw_line[1];
if (" ".equals(tag) || "-".equals(tag)) {
oldChunkLines.add(rest);
}
if (" ".equals(tag) || "+".equals(tag)) {
newChunkLines.add(rest);
}
}
patch.addDelta(new ChangeDelta<>(new Chunk<>(
old_ln - 1, oldChunkLines), new Chunk<>(
new_ln - 1, newChunkLines)));
rawChunk.clear();
}
processLinesInPrevChunk(rawChunk, patch, old_ln, new_ln);
// Parse the @@ header
old_ln = m.group(1) == null ? 1 : Integer.parseInt(m.group(1));
new_ln = m.group(3) == null ? 1 : Integer.parseInt(m.group(3));
Expand All @@ -102,6 +84,14 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
}

// Process the lines in the last chunk
processLinesInPrevChunk(rawChunk, patch, old_ln, new_ln);

return patch;
}

private static void processLinesInPrevChunk(List<String[]> rawChunk, Patch<String> patch, int old_ln, int new_ln) {
String tag;
String rest;
if (!rawChunk.isEmpty()) {
List<String> oldChunkLines = new ArrayList<>();
List<String> newChunkLines = new ArrayList<>();
Expand All @@ -116,14 +106,11 @@ public static Patch<String> parseUnifiedDiff(List<String> diff) {
newChunkLines.add(rest);
}
}

patch.addDelta(new ChangeDelta<>(new Chunk<>(
old_ln - 1, oldChunkLines), new Chunk<>(new_ln - 1,
newChunkLines)));
old_ln - 1, oldChunkLines), new Chunk<>(
new_ln - 1, newChunkLines)));
rawChunk.clear();
}

return patch;
}

/**
Expand Down