Make String.compare() match the Java specification

This commit is contained in:
Eric Scharff 2007-09-28 11:00:31 -06:00
parent 60eeb73e0f
commit 8a4d3effe0

View File

@ -89,36 +89,37 @@ public final class String implements Comparable<String> {
public int compareTo(String s) { public int compareTo(String s) {
if (this == s) return 0; if (this == s) return 0;
int d = length - s.length; int idx = 0;
if (d != 0) { int result;
return d;
} else { int end = (length < s.length ? length : s.length);
for (int i = 0; i < length; ++i) {
d = charAt(i) - s.charAt(i); while (idx < end) {
if (d != 0) { if ((result = charAt(idx) - s.charAt(idx)) != 0) {
return d; return result;
}
} }
return 0; idx++;
} }
return length - s.length;
} }
public int compareToIgnoreCase(String s) { public int compareToIgnoreCase(String s) {
if (this == s) return 0; if (this == s) return 0;
int d = length - s.length; int idx = 0;
if (d != 0) { int result;
return d;
} else { int end = (length < s.length ? length : s.length);
for (int i = 0; i < length; ++i) {
d = Character.toLowerCase(charAt(i)) while (idx < end) {
- Character.toLowerCase(s.charAt(i)); if ((result =
if (d != 0) { Character.toLowerCase(charAt(idx)) -
return d; Character.toLowerCase(s.charAt(idx))) != 0) {
} return result;
} }
return 0; idx++;
} }
return length - s.length;
} }
public String trim() { public String trim() {