Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: leading zeroes incorrect for index key comparator #6934

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ private int compareNumbers(String a, String b, int startA, int startB) {
int i = startA;
int j = startB;

// Skip leading zeros for both numbers
while (i < a.length() && a.charAt(i) == '0') {
i++;
}
while (j < b.length() && b.charAt(j) == '0') {
j++;
}

// Compare lengths of remaining digits
int lengthA = countDigits(a, i);
int lengthB = countDigits(b, j);
Expand Down Expand Up @@ -114,13 +106,6 @@ private int compareIntegerPart(String a, String b, int startA, int startB, int p
int i = startA;
int j = startB;

while (i < pointA && a.charAt(i) == '0') {
i++;
}
while (j < pointB && b.charAt(j) == '0') {
j++;
}

int lengthA = pointA - i;
int lengthB = pointB - j;
if (lengthA != lengthB) {
Expand Down Expand Up @@ -150,6 +135,7 @@ private int compareFractionalPart(String a, String b, int i, int j) {
j++;
}

// If one number has more digits left, and they're not all zeroes, it is larger
while (i < a.length() && Character.isDigit(a.charAt(i))) {
if (a.charAt(i) != '0') {
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ public void pureNumbersTest() {
assertThat(comparator.compare("124", "123")).isGreaterThan(0);
// Leading zeros
assertThat(comparator.compare("00123", "123") > 0).isTrue();
assertThat(comparator.compare("0", "0")).isEqualTo(0);
assertThat(comparator.compare("0", "0000")).isLessThan(0);
assertThat(comparator.compare("0x", "0")).isGreaterThan(0);
assertThat(comparator.compare("0", "1")).isLessThan(0);
assertThat(comparator.compare("1", "0")).isGreaterThan(0);
assertThat(comparator.compare("001", "0")).isGreaterThan(0);
assertThat(comparator.compare("0x5e", "0000")).isLessThan(0);
}

@Test
Expand Down Expand Up @@ -430,6 +437,7 @@ public void decimalStringsTest() {
assertThat(comparator.compare("123.46", "123.45")).isGreaterThan(0);
// Decimal equivalence
assertThat(comparator.compare("123.5", "123.50")).isLessThan(0);
assertThat(comparator.compare("123.0005", "123.00050")).isLessThan(0);
}

@Test
Expand Down
Loading