1 11 package org.eclipse.compare.internal; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 17 public class TextLineLCS extends LCS { 18 19 private final TextLine[] lines1; 20 private final TextLine[] lines2; 21 private TextLine[][] lcs; 22 23 public TextLineLCS(TextLine[] lines1, TextLine[] lines2) { 24 this.lines1 = lines1; 25 this.lines2 = lines2; 26 } 27 28 public TextLine[][] getResult() { 29 int length = getLength(); 30 if (length == 0) 31 return new TextLine[2][0]; 32 TextLine[][] result = new TextLine[2][]; 33 34 result[0] = compactAndShiftLCS(lcs[0], length, lines1); 36 result[1] = compactAndShiftLCS(lcs[1], length, lines2); 37 38 return result; 39 } 40 41 protected int getLength2() { 42 return lines2.length; 43 } 44 45 protected int getLength1() { 46 return lines1.length; 47 } 48 49 protected boolean isRangeEqual(int i1, int i2) { 50 return lines1[i1].sameText(lines2[i2]); 51 } 52 53 protected void setLcs(int sl1, int sl2) { 54 lcs[0][sl1] = lines1[sl1]; 55 lcs[1][sl1] = lines2[sl2]; 56 } 57 58 protected void initializeLcs(int length) { 59 lcs = new TextLine[2][length]; 60 } 61 62 79 private TextLine[] compactAndShiftLCS(TextLine[] lcsSide, int len, 80 TextLine[] original) { 81 TextLine[] result = new TextLine[len]; 82 83 if (len == 0) { 84 return result; 85 } 86 87 int j = 0; 88 89 while (lcsSide[j] == null) { 90 j++; 91 } 92 93 result[0] = lcsSide[j]; 94 j++; 95 96 for (int i = 1; i < len; i++) { 97 while (lcsSide[j] == null) { 98 j++; 99 } 100 101 if (original[result[i - 1].lineNumber() + 1].sameText(lcsSide[j])) { 102 result[i] = original[result[i - 1].lineNumber() + 1]; 103 } else { 104 result[i] = lcsSide[j]; 105 } 106 j++; 107 } 108 109 return result; 110 } 111 112 123 public static TextLine[] getTextLines(String text) { 124 List lines = new ArrayList (); 125 int begin = 0; 126 int end = getEOL(text, 0); 127 int lineNum = 0; 128 while (end != -1) { 129 lines.add(new TextLine(lineNum++, text.substring(begin, end))); 130 begin = end + 1; 131 end = getEOL(text, begin); 132 if (end == begin && text.charAt(begin - 1) == '\r' 133 && text.charAt(begin) == '\n') { 134 begin = end + 1; 136 end = getEOL(text, begin); 137 } 138 } 139 140 144 lines.add(new TextLine(lineNum, text.substring(begin))); 145 TextLine[] aLines = new TextLine[lines.size()]; 146 lines.toArray(aLines); 147 return aLines; 148 } 149 150 159 private static int getEOL(String text, int start) { 160 int max = text.length(); 161 for (int i = start; i < max; i++) { 162 char c = text.charAt(i); 163 if (c == '\n' || c == '\r') { 164 return i; 165 } 166 } 167 return -1; 168 } 169 170 171 public static class TextLine { 172 private int number; 174 private String text; 176 public TextLine(int number, String text) { 177 this.number = number; 178 this.text = text; 179 } 180 181 188 public boolean sameText(TextLine l) { 189 return text.hashCode() == l.text.hashCode() && l.text.equals(text); 192 } 193 194 199 public int lineNumber() { 200 return number; 201 } 202 203 public String toString() { 204 return "" + number + " " + text + "\n"; } 206 } 207 } 208 | Popular Tags |