| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
package org.crosswire.common.diff; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | 0 | public final class Commonality { |
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 0 | private Commonality() { |
| 36 | 0 | } |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
public static int prefix(final String source, final String target) { |
| 48 | 0 | int pointermin = 0; |
| 49 | 0 | int pointermax = Math.min(source.length(), target.length()); |
| 50 | 0 | int pointermid = pointermax; |
| 51 | 0 | while (pointermin < pointermid) { |
| 52 | 0 | if (source.regionMatches(0, target, 0, pointermid)) { |
| 53 | 0 | pointermin = pointermid; |
| 54 | |
} else { |
| 55 | 0 | pointermax = pointermid; |
| 56 | |
} |
| 57 | 0 | pointermid = (pointermax - pointermin) / 2 + pointermin; |
| 58 | |
} |
| 59 | |
|
| 60 | 0 | return pointermid; |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public static int suffix(final String source, final String target) { |
| 73 | 0 | int pointermin = 0; |
| 74 | 0 | int pointermax = Math.min(source.length(), target.length()); |
| 75 | 0 | int pointermid = pointermax; |
| 76 | 0 | while (pointermin < pointermid) { |
| 77 | 0 | if (source.regionMatches(source.length() - pointermid, target, target.length() - pointermid, pointermid)) { |
| 78 | 0 | pointermin = pointermid; |
| 79 | |
} else { |
| 80 | 0 | pointermax = pointermid; |
| 81 | |
} |
| 82 | 0 | pointermid = (pointermax - pointermin) / 2 + pointermin; |
| 83 | |
} |
| 84 | |
|
| 85 | 0 | return pointermid; |
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public static CommonMiddle halfMatch(final String source, final String target) { |
| 99 | 0 | int sourceLength = source.length(); |
| 100 | 0 | int targetLength = target.length(); |
| 101 | 0 | String longText = sourceLength > targetLength ? source : target; |
| 102 | 0 | String shortText = sourceLength > targetLength ? target : source; |
| 103 | 0 | int longTextLength = Math.max(sourceLength, targetLength); |
| 104 | 0 | if (longTextLength < 10 || shortText.length() < 1) { |
| 105 | 0 | return null; |
| 106 | |
} |
| 107 | |
|
| 108 | |
|
| 109 | 0 | CommonMiddle hm1 = halfMatch(longText, shortText, ceil(longTextLength, 4)); |
| 110 | |
|
| 111 | 0 | CommonMiddle hm2 = halfMatch(longText, shortText, ceil(longTextLength, 2)); |
| 112 | 0 | CommonMiddle hm = null; |
| 113 | 0 | if (hm1 == null && hm2 == null) { |
| 114 | 0 | return null; |
| 115 | 0 | } else if (hm2 == null) { |
| 116 | 0 | hm = hm1; |
| 117 | 0 | } else if (hm1 == null) { |
| 118 | 0 | hm = hm2; |
| 119 | |
} else { |
| 120 | |
|
| 121 | 0 | hm = hm1.getCommonality().length() > hm2.getCommonality().length() ? hm1 : hm2; |
| 122 | |
} |
| 123 | |
|
| 124 | |
|
| 125 | 0 | if (sourceLength > targetLength) { |
| 126 | 0 | return hm; |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | 0 | if (hm != null) { |
| 131 | 0 | return new CommonMiddle(hm.getTargetPrefix(), hm.getTargetSuffix(), hm.getSourcePrefix(), hm.getSourceSuffix(), hm.getCommonality()); |
| 132 | |
} |
| 133 | 0 | return null; |
| 134 | |
} |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
private static CommonMiddle halfMatch(final String longText, final String shortText, final int startIndex) { |
| 151 | |
|
| 152 | 0 | String seed = longText.substring(startIndex, startIndex + (longText.length() / 4)); |
| 153 | 0 | int j = -1; |
| 154 | 0 | String common = ""; |
| 155 | 0 | String longTextPrefix = ""; |
| 156 | 0 | String longTextSuffix = ""; |
| 157 | 0 | String shortTextPrefix = ""; |
| 158 | 0 | String shortTextSuffix = ""; |
| 159 | 0 | while ((j = shortText.indexOf(seed, j + 1)) != -1) { |
| 160 | 0 | int prefixLength = Commonality.prefix(longText.substring(startIndex), shortText.substring(j)); |
| 161 | 0 | int suffixLength = Commonality.suffix(longText.substring(0, startIndex), shortText.substring(0, j)); |
| 162 | 0 | if (common.length() < (prefixLength + suffixLength)) { |
| 163 | 0 | common = shortText.substring(j - suffixLength, j) + shortText.substring(j, j + prefixLength); |
| 164 | 0 | longTextPrefix = longText.substring(0, startIndex - suffixLength); |
| 165 | 0 | longTextSuffix = longText.substring(startIndex + prefixLength); |
| 166 | 0 | shortTextPrefix = shortText.substring(0, j - suffixLength); |
| 167 | 0 | shortTextSuffix = shortText.substring(j + prefixLength); |
| 168 | |
} |
| 169 | 0 | } |
| 170 | |
|
| 171 | 0 | if (common.length() >= longText.length() / 2) { |
| 172 | 0 | return new CommonMiddle(longTextPrefix, longTextSuffix, shortTextPrefix, shortTextSuffix, common); |
| 173 | |
} |
| 174 | |
|
| 175 | 0 | return null; |
| 176 | |
} |
| 177 | |
|
| 178 | |
private static int ceil(int number, int divisor) { |
| 179 | 0 | assert divisor > 0; |
| 180 | 0 | int result = number / divisor + ((number % divisor) > 0 ? 1 : 0); |
| 181 | |
|
| 182 | 0 | return result; |
| 183 | |
} |
| 184 | |
|
| 185 | |
} |