Further optimize StringSequence.startsWith
See gh-21259
This commit is contained in:
@@ -108,17 +108,7 @@ final class StringSequence implements CharSequence {
|
||||
if (length - prefixLength - offset < 0) {
|
||||
return false;
|
||||
}
|
||||
if (length == this.source.length()) {
|
||||
return this.source.startsWith(prefix, offset);
|
||||
}
|
||||
int prefixOffset = 0;
|
||||
int sourceOffset = offset;
|
||||
while (prefixLength-- != 0) {
|
||||
if (charAt(sourceOffset++) != prefix.charAt(prefixOffset++)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return this.source.startsWith(prefix, this.start + offset);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -203,4 +203,18 @@ class StringSequenceTests {
|
||||
assertThat(new StringSequence("xab").startsWith("c", 1)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void startsWithOnSubstringTailWhenMatch() {
|
||||
StringSequence subSequence = new StringSequence("xabc").subSequence(1);
|
||||
assertThat(subSequence.startsWith("abc")).isTrue();
|
||||
assertThat(subSequence.startsWith("abcd")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void startsWithOnSubstringMiddleWhenMatch() {
|
||||
StringSequence subSequence = new StringSequence("xabc").subSequence(1, 3);
|
||||
assertThat(subSequence.startsWith("ab")).isTrue();
|
||||
assertThat(subSequence.startsWith("abc")).isFalse();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user