From 81085d368ca8aa1b5dc0d5da5c82cc0665966f91 Mon Sep 17 00:00:00 2001 From: Ibrahim ali abdelghany Date: Mon, 18 Nov 2019 17:29:31 +0200 Subject: [PATCH] Extract duplicate code into method. (#58) * Extract duplicate code into method. * fix conflicts. --- .../com/github/difflib/UnifiedDiffUtils.java | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java b/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java index d5812c3..2ed5ba8 100644 --- a/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java +++ b/java-diff-utils/src/main/java/com/github/difflib/UnifiedDiffUtils.java @@ -59,25 +59,7 @@ public final class UnifiedDiffUtils { Matcher m = UNIFIED_DIFF_CHUNK_REGEXP.matcher(line); if (m.find()) { // Process the lines in the previous chunk - if (!rawChunk.isEmpty()) { - List oldChunkLines = new ArrayList<>(); - List 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)); @@ -102,6 +84,14 @@ public final class UnifiedDiffUtils { } // Process the lines in the last chunk + processLinesInPrevChunk(rawChunk, patch, old_ln, new_ln); + + return patch; + } + + private static void processLinesInPrevChunk(List rawChunk, Patch patch, int old_ln, int new_ln) { + String tag; + String rest; if (!rawChunk.isEmpty()) { List oldChunkLines = new ArrayList<>(); List newChunkLines = new ArrayList<>(); @@ -116,14 +106,11 @@ public final class UnifiedDiffUtils { 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; } /**