Fix `String` ctor with `hibyte` for empty strings.
Mark empty strings as compressed for any value of `hibyte`.
Test: New test in 021-string2.
Test: testrunner.py --host --optimizing -t 021-string2
Bug: 250530521
Change-Id: Ie129fba599a24021eda0a4a81a39fd3fc3769597
diff --git a/runtime/mirror/string-alloc-inl.h b/runtime/mirror/string-alloc-inl.h
index 533053d..925427a 100644
--- a/runtime/mirror/string-alloc-inl.h
+++ b/runtime/mirror/string-alloc-inl.h
@@ -216,7 +216,9 @@
const uint8_t* const src = reinterpret_cast<uint8_t*>(array->GetData()) + offset;
high_byte &= 0xff; // Extract the relevant bits before determining `compressible`.
const bool compressible =
- kUseStringCompression && String::AllASCII<uint8_t>(src, byte_length) && (high_byte == 0);
+ kUseStringCompression &&
+ String::AllASCII<uint8_t>(src, byte_length) &&
+ (high_byte == 0 || byte_length == 0);
const int32_t length_with_flag = String::GetFlaggedCount(byte_length, compressible);
SetStringCountAndBytesVisitor visitor(length_with_flag, array, offset, high_byte << 8);
return Alloc<kIsInstrumented>(self, length_with_flag, allocator_type, visitor);
diff --git a/test/021-string2/src/Main.java b/test/021-string2/src/Main.java
index 39595f3..5da10ca 100644
--- a/test/021-string2/src/Main.java
+++ b/test/021-string2/src/Main.java
@@ -119,6 +119,7 @@
testEqualsConstString();
testConstStringEquals();
testStringConcat();
+ testEmptyWithHighByte();
// Regression tests for String.setCharAt() breaking string compression invariants.
Locale en_US = new Locale("en", "US");
@@ -760,6 +761,11 @@
Assert.assertEquals("abc\u0440xyzw\u0440", "abc\u0440".concat("xyzw\u0440"));
}
+ public static void testEmptyWithHighByte() {
+ String empty = new String(new byte[0], 1);
+ Assert.assertEquals("", empty);
+ }
+
public static boolean $noinline$equalsConstString0(String s) {
return s.equals("");
}