1 11 package org.eclipse.compare.internal; 12 13 import org.eclipse.jface.util.Assert; 14 15 import org.eclipse.compare.rangedifferencer.IRangeComparator; 16 import org.eclipse.compare.contentmergeviewer.ITokenComparator; 17 18 24 public class TokenComparator implements ITokenComparator { 25 26 private boolean fShouldEscape= true; 27 private String fText; 28 private int fCount; 29 private int[] fStarts; 30 private int[] fLengths; 31 32 37 public TokenComparator(String text) { 38 39 Assert.isNotNull(text); 40 41 fText= text; 42 43 int length= fText.length(); 44 fStarts= new int[length]; fLengths= new int[length]; 46 fCount= 0; 47 48 char lastCategory= 0; for (int i= 0; i < length; i++) { 50 char c= fText.charAt(i); 51 52 char category= '?'; if (Character.isWhitespace(c)) 54 category= ' '; else if (Character.isDigit(c)) 56 category= '0'; else if (Character.isLetter(c)) 58 category= 'a'; 60 if (category != lastCategory) { 61 fStarts[fCount++]= i; 63 lastCategory= category; 64 } 65 fLengths[fCount-1]++; 66 } 67 } 68 69 75 public TokenComparator(String text, boolean shouldEscape) { 76 this(text); 77 fShouldEscape= shouldEscape; 78 } 79 80 85 public int getRangeCount() { 86 return fCount; 87 } 88 89 92 public int getTokenStart(int index) { 93 if (index < fCount) 94 return fStarts[index]; 95 return fText.length(); 96 } 97 98 101 public int getTokenLength(int index) { 102 if (index < fCount) 103 return fLengths[index]; 104 return 0; 105 } 106 107 116 public boolean rangesEqual(int thisIndex, IRangeComparator other, int otherIndex) { 117 if (other != null && getClass() == other.getClass()) { 118 TokenComparator tc= (TokenComparator) other; 119 int thisLen= getTokenLength(thisIndex); 120 int otherLen= tc.getTokenLength(otherIndex); 121 if (thisLen == otherLen) 122 return fText.regionMatches(false, getTokenStart(thisIndex), tc.fText, tc.getTokenStart(otherIndex), thisLen); 123 } 124 return false; 125 } 126 127 131 public boolean skipRangeComparison(int length, int max, IRangeComparator other) { 132 133 if (!fShouldEscape) 134 return false; 135 136 if (getRangeCount() < 50 || other.getRangeCount() < 50) 137 return false; 138 139 if (max < 100) 140 return false; 141 142 if (length < 100) 143 return false; 144 145 if (max > 800) 146 return true; 147 148 if (length < max / 4) 149 return false; 150 151 return true; 152 } 153 154 } 176 | Popular Tags |