Skip to content

Commit 4a8492d

Browse files
committed
Further optimize StringSequence.startsWith
See gh-21259
1 parent 70ffc70 commit 4a8492d

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

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

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,7 @@ boolean startsWith(String prefix, int offset) {
108108
if (length - prefixLength - offset < 0) {
109109
return false;
110110
}
111-
if (length == this.source.length()) {
112-
return this.source.startsWith(prefix, offset);
113-
}
114-
int prefixOffset = 0;
115-
int sourceOffset = offset;
116-
while (prefixLength-- != 0) {
117-
if (charAt(sourceOffset++) != prefix.charAt(prefixOffset++)) {
118-
return false;
119-
}
120-
}
121-
return true;
111+
return this.source.startsWith(prefix, this.start + offset);
122112
}
123113

124114
@Override

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,18 @@ void startsWithOffsetWhenShorterAndDoesNotStartWith() {
203203
assertThat(new StringSequence("xab").startsWith("c", 1)).isFalse();
204204
}
205205

206+
@Test
207+
void startsWithOnSubstringTailWhenMatch() {
208+
StringSequence subSequence = new StringSequence("xabc").subSequence(1);
209+
assertThat(subSequence.startsWith("abc")).isTrue();
210+
assertThat(subSequence.startsWith("abcd")).isFalse();
211+
}
212+
213+
@Test
214+
void startsWithOnSubstringMiddleWhenMatch() {
215+
StringSequence subSequence = new StringSequence("xabc").subSequence(1, 3);
216+
assertThat(subSequence.startsWith("ab")).isTrue();
217+
assertThat(subSequence.startsWith("abc")).isFalse();
218+
}
219+
206220
}

0 commit comments

Comments
 (0)