Skip to content

Commit

Permalink
Update String#contentEquals(java.lang.CharSequence)
Browse files Browse the repository at this point in the history
Currently String#contentEquals(java.lang.CharSequence) implementation
only handles if the bytes in the String value are UTF16 encoded. If not
it throws an assertion error like the following:

    [junit] java.lang.AssertionError: Currently only UTF16 is supported for String comparision with an instance of type AbstractStringBuilder
    [junit]     at java.lang.String.contentEquals(String.java:275)
    [junit]     at gov.nasa.jpf.test.java.lang.StringTest.testContentEquals(StringTest.java:301)
    [junit]     at java.lang.reflect.Method.invoke(gov.nasa.jpf.vm.JPF_java_lang_reflect_Method)
    [junit]     at gov.nasa.jpf.util.test.TestJPF.runTestMethod(TestJPF.java:648)

This updates String#contentEquals(java.lang.CharSequence) method, so it
handles both LATIN1 and UTF16 encoded Strings.
  • Loading branch information
gayanW committed Jul 23, 2018
1 parent 9fad766 commit ed880f4
Showing 1 changed file with 35 additions and 8 deletions.
43 changes: 35 additions & 8 deletions src/classes/modules/java.base/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ public boolean contentEquals(StringBuffer stringBuffer){
}
}

private boolean nonSyncContentEquals(AbstractStringBuilder abstractStringBuilder) {
int len = length();
if (len != abstractStringBuilder.length()) {
return false;
}
byte v1[] = value;
byte v2[] = abstractStringBuilder.getValue();
if (coder() == abstractStringBuilder.getCoder()) {
int n = v1.length;
for (int i = 0; i < n; i++) {
if (v1[i] != v2[i]) {
return false;
}
}
} else {
return isLatin1() && StringUTF16.contentEquals(v1, v2, len);
}
return true;
}

native static boolean equals0 (char[] a, char[] b, int len);

/**
Expand All @@ -264,21 +284,28 @@ public boolean contentEquals (CharSequence charSequence){
return false;
}

// that should be the common case (String)
if (charSequence.equals(this)){
return true;
// cs is a String
if (charSequence instanceof String) {
return equals(charSequence);
}

// we can do that natively, too
// cs is a StringBuffer, or StringBuilder
if (charSequence instanceof AbstractStringBuilder) {
byte[] val = ((AbstractStringBuilder) charSequence).getValue();
byte coder = ((AbstractStringBuilder) charSequence).getCoder();
assert coder == UTF16 : "Currently only UTF16 is supported for String comparision with an instance of type AbstractStringBuilder";
return StringUTF16.contentEquals(this.value, val, this.length());
if (charSequence instanceof StringBuffer) {
synchronized (charSequence) {
return nonSyncContentEquals((AbstractStringBuilder) charSequence);
}
} else {
return nonSyncContentEquals((AbstractStringBuilder) charSequence);
}
}

// generic CharSequence - expensive
int n = charSequence.length();
if (n != length()) {
return false;
}

byte[] val = this.value;
if (isLatin1()) {
for (int i = 0; i < n; i++) {
Expand Down

0 comments on commit ed880f4

Please sign in to comment.