mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-06 22:15:31 +00:00
ICU-6824 Combined Differ files, moved to common location
X-SVN-Rev: 26398
This commit is contained in:
parent
c361c76465
commit
cd7e4f87e7
3 changed files with 3 additions and 173 deletions
|
@ -11,6 +11,7 @@ import java.awt.event.*;
|
|||
import java.util.*;
|
||||
import java.text.CharacterIterator;
|
||||
|
||||
import com.ibm.icu.impl.Differ;
|
||||
import com.ibm.icu.lang.*;
|
||||
import com.ibm.icu.text.*;
|
||||
|
||||
|
|
|
@ -1,171 +0,0 @@
|
|||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1996-2008, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
package com.ibm.icu.dev.demo.translit;
|
||||
|
||||
/** VERY Basic Diff program. Compares two sequences of objects fed into it, and
|
||||
* lets you know where they are different.
|
||||
* @author Mark Davis
|
||||
* @version 1.0
|
||||
*/
|
||||
|
||||
final public class Differ {
|
||||
public static final String copyright =
|
||||
"Copyright (C) 2000, International Business Machines Corporation and others. All Rights Reserved.";
|
||||
|
||||
/**
|
||||
* @param stackSize The size of the largest difference you expect.
|
||||
* @param matchCount The number of items that have to be the same to count as a match
|
||||
*/
|
||||
public Differ(int stackSize, int matchCount) {
|
||||
this.STACKSIZE = stackSize;
|
||||
this.EQUALSIZE = matchCount;
|
||||
a = new Object[stackSize+matchCount];
|
||||
b = new Object[stackSize+matchCount];
|
||||
}
|
||||
|
||||
public void add (Object aStr, Object bStr) {
|
||||
addA(aStr);
|
||||
addB(bStr);
|
||||
}
|
||||
|
||||
public void addA (Object aStr) {
|
||||
flush();
|
||||
a[aCount++] = aStr;
|
||||
}
|
||||
|
||||
public void addB (Object bStr) {
|
||||
flush();
|
||||
b[bCount++] = bStr;
|
||||
}
|
||||
|
||||
public int getALine(int offset) {
|
||||
return aLine + maxSame + offset;
|
||||
}
|
||||
|
||||
public Object getA(int offset) {
|
||||
if (offset < 0) return last;
|
||||
if (offset > aTop-maxSame) return next;
|
||||
return a[offset];
|
||||
}
|
||||
|
||||
public int getACount() {
|
||||
return aTop-maxSame;
|
||||
}
|
||||
|
||||
public int getBCount() {
|
||||
return bTop-maxSame;
|
||||
}
|
||||
|
||||
public int getBLine(int offset) {
|
||||
return bLine + maxSame + offset;
|
||||
}
|
||||
|
||||
public Object getB(int offset) {
|
||||
if (offset < 0) return last;
|
||||
if (offset > bTop-maxSame) return next;
|
||||
return b[offset];
|
||||
}
|
||||
|
||||
public void checkMatch(boolean finalPass) {
|
||||
// find the initial strings that are the same
|
||||
int max = aCount;
|
||||
if (max > bCount) max = bCount;
|
||||
int i;
|
||||
for (i = 0; i < max; ++i) {
|
||||
if (!a[i].equals(b[i])) break;
|
||||
}
|
||||
// at this point, all items up to i are equal
|
||||
maxSame = i;
|
||||
aTop = bTop = maxSame;
|
||||
if (maxSame > 0) last = a[maxSame-1];
|
||||
next = "";
|
||||
|
||||
if (finalPass) {
|
||||
aTop = aCount;
|
||||
bTop = bCount;
|
||||
next = "";
|
||||
return;
|
||||
}
|
||||
|
||||
if (aCount - maxSame < EQUALSIZE || bCount - maxSame < EQUALSIZE) return;
|
||||
|
||||
// now see if the last few a's occur anywhere in the b's, or vice versa
|
||||
int match = find (a, aCount-EQUALSIZE, aCount, b, maxSame, bCount);
|
||||
if (match != -1) {
|
||||
aTop = aCount-EQUALSIZE;
|
||||
bTop = match;
|
||||
next = a[aTop];
|
||||
return;
|
||||
}
|
||||
match = find (b, bCount-EQUALSIZE, bCount, a, maxSame, aCount);
|
||||
if (match != -1) {
|
||||
bTop = bCount-EQUALSIZE;
|
||||
aTop = match;
|
||||
next = b[bTop];
|
||||
return;
|
||||
}
|
||||
if (aCount >= STACKSIZE || bCount >= STACKSIZE) {
|
||||
// flush some of them
|
||||
aCount = (aCount + maxSame) / 2;
|
||||
bCount = (bCount + maxSame) / 2;
|
||||
next = "";
|
||||
}
|
||||
}
|
||||
|
||||
/** Convenient utility
|
||||
* finds a segment of the first array in the second array.
|
||||
* @return -1 if not found, otherwise start position in b
|
||||
*/
|
||||
|
||||
public int find (Object[] aArray, int aStart, int aEnd, Object[] bArray, int bStart, int bEnd) {
|
||||
int len = aEnd - aStart;
|
||||
int bEndMinus = bEnd - len;
|
||||
tryA:
|
||||
for (int i = bStart; i <= bEndMinus; ++i) {
|
||||
for (int j = 0; j < len; ++j) {
|
||||
if (!bArray[i + j].equals(aArray[aStart + j])) continue tryA;
|
||||
}
|
||||
return i; // we have a match!
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// ====================== PRIVATES ======================
|
||||
|
||||
private void flush() {
|
||||
if (aTop != 0) {
|
||||
int newCount = aCount-aTop;
|
||||
System.arraycopy(a, aTop, a, 0, newCount);
|
||||
aCount = newCount;
|
||||
aLine += aTop;
|
||||
aTop = 0;
|
||||
}
|
||||
|
||||
if (bTop != 0) {
|
||||
int newCount = bCount-bTop;
|
||||
System.arraycopy(b, bTop, b, 0, newCount);
|
||||
bCount = newCount;
|
||||
bLine += bTop;
|
||||
bTop = 0;
|
||||
}
|
||||
}
|
||||
|
||||
private int STACKSIZE;
|
||||
private int EQUALSIZE;
|
||||
|
||||
private Object [] a;
|
||||
private Object [] b;
|
||||
private Object last = "";
|
||||
private Object next = "";
|
||||
private int aCount = 0;
|
||||
private int bCount = 0;
|
||||
private int aLine = 1;
|
||||
private int bLine = 1;
|
||||
private int maxSame = 0, aTop = 0, bTop = 0;
|
||||
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
/**
|
||||
*******************************************************************************
|
||||
* Copyright (C) 1996-2007, International Business Machines Corporation and *
|
||||
* Copyright (C) 1996-2009, International Business Machines Corporation and *
|
||||
* others. All Rights Reserved. *
|
||||
*******************************************************************************
|
||||
*/
|
||||
|
||||
package com.ibm.icu.dev.test.util;
|
||||
package com.ibm.icu.impl;
|
||||
|
||||
/** VERY Basic Diff program. Compares two sequences of objects fed into it, and
|
||||
* lets you know where they are different.
|
Loading…
Add table
Reference in a new issue