Use char indexOf rather than String, which can have a fast path implementation

PiperOrigin-RevId: 599529561
pull/15483/head
Protobuf Team Bot 10 months ago committed by Copybara-Service
parent 979c39178b
commit adacf6d0e1
  1. 6
      java/core/src/main/java/com/google/protobuf/Utf8.java

@ -1346,13 +1346,13 @@ final class Utf8 {
String decodeUtf8(byte[] bytes, int index, int size) throws InvalidProtocolBufferException {
String s = new String(bytes, index, size, Internal.UTF_8);
// "\uFFFD" is UTF-8 default replacement string, which illegal byte sequences get replaced
// '\uFFFD' is the UTF-8 default replacement char, which illegal byte sequences get replaced
// with.
if (!s.contains("\uFFFD")) {
if (s.indexOf('\uFFFD') < 0) {
return s;
}
// Since s contains "\uFFFD" there are 2 options:
// Since s contains '\uFFFD' there are 2 options:
// 1) The byte array slice is invalid UTF-8.
// 2) The byte array slice is valid UTF-8 and contains encodings for "\uFFFD".
// To rule out (1), we encode s and compare it to the byte array slice.

Loading…
Cancel
Save