Browse Source

Further optimize StringSequence.startsWith

See gh-21259
pull/21361/head
Phillip Webb 6 years ago
parent
commit
4a8492d428
  1. 12
      spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java
  2. 14
      spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java

12
spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java

@ -108,17 +108,7 @@ final class StringSequence implements CharSequence { @@ -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

14
spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/StringSequenceTests.java

@ -203,4 +203,18 @@ class StringSequenceTests { @@ -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();
}
}

Loading…
Cancel
Save