Skip to content

Commit

Permalink
fix the failing test
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Feb 19, 2025
1 parent 4c4d1a7 commit ec37ebd
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;

import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -97,12 +100,13 @@ public static int compareUtf8Strings(String left, String right) {
// ASCII comparison
return Integer.compare(leftCodePoint, rightCodePoint);
} else {
// UTF-8 encoded byte comparison, substring 2 indexes to cover surrogate pairs
ByteString leftBytes =
ByteString.copyFromUtf8(left.substring(i, Math.min(i + 2, left.length())));
ByteString rightBytes =
ByteString.copyFromUtf8(right.substring(i, Math.min(i + 2, right.length())));
return compareByteStrings(leftBytes, rightBytes);
// substring and do UTF-8 encoded byte comparison
byte[] leftBytes = getUtf8SafeBytes(left, i);
byte[] rightBytes = getUtf8SafeBytes(right, i);
int comp = compareByteArrays(leftBytes,rightBytes);
if(comp !=0) {
return comp;
}
}
}

Expand All @@ -114,6 +118,19 @@ public static int compareUtf8Strings(String left, String right) {
return Integer.compare(left.length(), right.length());
}

private static byte[] getUtf8SafeBytes(String str, int index) {
int firstCodePoint = str.codePointAt(index);
String sub;
if (firstCodePoint > 0xffff) {
// It's a surrogate pair, return the whole pair
sub = str.substring(index, index + 2);
} else {
// It's a single code point, return it
sub = str.substring(index, index + 1);
}
return sub.getBytes(StandardCharsets.UTF_8);
}

/**
* Utility function to compare longs. Note that we can't use Long.compare because it's only
* available after Android 19.
Expand Down

0 comments on commit ec37ebd

Please sign in to comment.