Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Feb 20, 2025
1 parent 391867a commit 8159f25
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.grpc.Status;
import io.grpc.StatusException;
import io.grpc.StatusRuntimeException;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -98,9 +97,9 @@ public static int compareUtf8Strings(String left, String right) {
return Integer.compare(leftCodePoint, rightCodePoint);
} else {
// substring and do UTF-8 encoded byte comparison
byte[] leftBytes = getUtf8SafeBytes(left, i);
byte[] rightBytes = getUtf8SafeBytes(right, i);
int comp = compareByteArrays(leftBytes, rightBytes);
ByteString leftBytes = ByteString.copyFromUtf8(getUtf8SafeBytes(left, i));
ByteString rightBytes = ByteString.copyFromUtf8(getUtf8SafeBytes(right, i));
int comp = compareByteStrings(leftBytes, rightBytes);
if (comp != 0) {
return comp;
}
Expand All @@ -112,17 +111,15 @@ public static int compareUtf8Strings(String left, String right) {
return Integer.compare(left.length(), right.length());
}

private static byte[] getUtf8SafeBytes(String str, int index) {
private static String 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);
return str.substring(index, index + 2);
} else {
// It's a single code point, return it
sub = str.substring(index, index + 1);
return str.substring(index, index + 1);
}
return sub.getBytes(StandardCharsets.UTF_8);
}

/**
Expand Down

0 comments on commit 8159f25

Please sign in to comment.