From 27e89bbb8a6641b12ef8c8fd3d0a07c507d36216 Mon Sep 17 00:00:00 2001 From: dreis2211 Date: Fri, 14 Dec 2018 21:28:52 +0100 Subject: [PATCH] Optimize StringSequence Closes gh-15473 --- .../boot/loader/jar/StringSequence.java | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java index 9d5b05fe25..a236fa95dd 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/main/java/org/springframework/boot/loader/jar/StringSequence.java @@ -66,6 +66,9 @@ final class StringSequence implements CharSequence { if (subSequenceEnd > this.end) { throw new StringIndexOutOfBoundsException(end); } + if (start == 0 && subSequenceEnd == this.end) { + return this; + } return new StringSequence(this.source, subSequenceStart, subSequenceEnd); } @@ -100,10 +103,18 @@ final class StringSequence implements CharSequence { } public boolean startsWith(CharSequence prefix, int offset) { - if (length() - prefix.length() - offset < 0) { + int prefixLength = prefix.length(); + if (length() - prefixLength - offset < 0) { return false; } - return subSequence(offset, offset + prefix.length()).equals(prefix); + int prefixOffset = 0; + int sourceOffset = offset; + while (prefixLength-- != 0) { + if (charAt(sourceOffset++) != prefix.charAt(prefixOffset++)) { + return false; + } + } + return true; } @Override