mirror of
https://github.com/hneemann/Digital.git
synced 2025-09-28 07:28:20 -04:00
more flexible NumStringComparator
This commit is contained in:
parent
b6743695b0
commit
f1c6356f89
@ -6,8 +6,8 @@ import static java.lang.Character.isDigit;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* String comparator.
|
* String comparator.
|
||||||
* If the string begins with a digit, the numbers are taken to compare the two strings.
|
* If the string contains a digit, the numbers are taken to compare the two strings.
|
||||||
* Used to ensure the 74xx components appear in the correct order instead of lexical order.
|
* Used to ensure the 74xx components appear in the correct numerical order instead of lexical order.
|
||||||
* Created by hneemann on 15.05.17.
|
* Created by hneemann on 15.05.17.
|
||||||
*/
|
*/
|
||||||
public final class NumStringComparator implements Comparator<String> {
|
public final class NumStringComparator implements Comparator<String> {
|
||||||
@ -41,62 +41,50 @@ public final class NumStringComparator implements Comparator<String> {
|
|||||||
* @return the comparison result
|
* @return the comparison result
|
||||||
*/
|
*/
|
||||||
public static int compareStr(String a, String b) {
|
public static int compareStr(String a, String b) {
|
||||||
NumString na = new NumString(a);
|
int pa = 0;
|
||||||
NumString nb = new NumString(b);
|
int pb = 0;
|
||||||
return na.compareTo(nb);
|
while (true) {
|
||||||
}
|
final boolean ae = pa == a.length();
|
||||||
|
final boolean be = pb == b.length();
|
||||||
|
if (ae && be) return 0;
|
||||||
|
else if (ae) return -1;
|
||||||
|
else if (be) return 1;
|
||||||
|
|
||||||
private static final class NumString implements Comparable<NumString> {
|
char ca = Character.toLowerCase(a.charAt(pa));
|
||||||
private final int num;
|
char cb = Character.toLowerCase(b.charAt(pb));
|
||||||
private final String str;
|
|
||||||
private final boolean isNum;
|
|
||||||
|
|
||||||
private NumString(String str) {
|
if (isDigit(ca) && isDigit(cb)) {
|
||||||
str = str.trim();
|
ParseNumber da = new ParseNumber(a, pa);
|
||||||
if (str.length() > 0 && isDigit(str.charAt(0))) {
|
ParseNumber db = new ParseNumber(b, pb);
|
||||||
isNum = true;
|
int c = Integer.compare(da.num, db.num);
|
||||||
int n = 0;
|
if (c != 0)
|
||||||
int i = 0;
|
return c;
|
||||||
while (i < str.length() && isDigit(str.charAt(i))) {
|
else {
|
||||||
n = n * 10 + (str.charAt(i) - '0');
|
pa = da.p;
|
||||||
i++;
|
pb = db.p;
|
||||||
}
|
}
|
||||||
this.str = str.substring(i);
|
|
||||||
this.num = n;
|
|
||||||
} else {
|
} else {
|
||||||
this.str = str;
|
int c = Character.compare(ca, cb);
|
||||||
num = 0;
|
if (c != 0) {
|
||||||
isNum = false;
|
return c;
|
||||||
|
} else {
|
||||||
|
pa++;
|
||||||
|
pb++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private static final class ParseNumber {
|
||||||
public boolean equals(Object o) {
|
private int num;
|
||||||
if (this == o) return true;
|
private int p;
|
||||||
if (o == null || getClass() != o.getClass()) return false;
|
|
||||||
|
|
||||||
NumString numString = (NumString) o;
|
private ParseNumber(String a, int sp) {
|
||||||
|
p = sp;
|
||||||
if (num != numString.num) return false;
|
while (p < a.length() && isDigit(a.charAt(p))) {
|
||||||
return str != null ? str.equalsIgnoreCase(numString.str) : numString.str == null;
|
num = num * 10 + (a.charAt(p) - '0');
|
||||||
|
p++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
int result = num;
|
|
||||||
result = 31 * result + (str != null ? str.toLowerCase().hashCode() : 0);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compareTo(NumString other) {
|
|
||||||
if (isNum && other.isNum) {
|
|
||||||
if (num != other.num)
|
|
||||||
return num - other.num;
|
|
||||||
else
|
|
||||||
return str.compareToIgnoreCase(other.str);
|
|
||||||
} else
|
|
||||||
return str.compareToIgnoreCase(other.str);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ import junit.framework.TestCase;
|
|||||||
public class NumStringComparatorTest extends TestCase {
|
public class NumStringComparatorTest extends TestCase {
|
||||||
|
|
||||||
public void testSimple() {
|
public void testSimple() {
|
||||||
|
checkLess("a", "aa");
|
||||||
checkLess("a", "b");
|
checkLess("a", "b");
|
||||||
checkLess("12a", "b");
|
checkLess("12a", "b");
|
||||||
checkLess("2a", "12b");
|
checkLess("2a", "12b");
|
||||||
@ -15,6 +16,11 @@ public class NumStringComparatorTest extends TestCase {
|
|||||||
checkLess("2a", "02b");
|
checkLess("2a", "02b");
|
||||||
checkLess(" 2a", "02b");
|
checkLess(" 2a", "02b");
|
||||||
checkLess("2a", "2B");
|
checkLess("2a", "2B");
|
||||||
|
checkLess("05", "10");
|
||||||
|
checkLess("5", "10");
|
||||||
|
checkLess("a2b", "a10b");
|
||||||
|
checkLess("a5c2b", "a5c10b");
|
||||||
|
checkLess("a5c2b", "a005c10b");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkLess(String a, String b) {
|
private void checkLess(String a, String b) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user