Further optimize StringSequence.startsWith

See gh-21259
pull/21361/head
Phillip Webb 5 years ago
parent 70ffc70993
commit 4a8492d428

@ -108,17 +108,7 @@ final class StringSequence implements CharSequence {
if (length - prefixLength - offset < 0) { if (length - prefixLength - offset < 0) {
return false; return false;
} }
if (length == this.source.length()) { return this.source.startsWith(prefix, this.start + offset);
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;
} }
@Override @Override

@ -203,4 +203,18 @@ class StringSequenceTests {
assertThat(new StringSequence("xab").startsWith("c", 1)).isFalse(); 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();
}
} }

Loading…
Cancel
Save