Skip to content

Commit f907eb8

Browse files
vaadin-botcaalador
andauthored
fix: Clean up countInstances (#24890) (CP: 24.10) (#24894)
This PR cherry-picks changes from the original PR #24890 to branch 24.10. --- #### Original PR description > Make count instances more effective > by not using split that compiles > a pattern and generates an array > for each invocation. > > fixes #9264 > Co-authored-by: caalador <mikael.grankvist@vaadin.com>
1 parent 62cca49 commit f907eb8

1 file changed

Lines changed: 23 additions & 1 deletion

File tree

flow-server/src/main/java/com/vaadin/flow/server/startup/DefaultApplicationConfigurationFactory.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,30 @@ && countInstances(viteGenerated.getPath(), "jar!/") >= 2) {
223223
return FrontendUtils.streamToString(firstResource.openStream());
224224
}
225225

226+
/**
227+
* Counts how many times {@code value} occurs as a non-overlapping substring
228+
* within {@code input}.
229+
* <p>
230+
* Used to determine how many nested {@code jar!/} segments appear in a
231+
* resource path, which indicates whether the resource is packaged inside
232+
* one or several jars.
233+
*
234+
* @param input
235+
* the string to search within, not {@code null}
236+
* @param value
237+
* the substring to count occurrences of, not {@code null} and
238+
* not empty
239+
* @return the number of non-overlapping occurrences of {@code value} in
240+
* {@code input}, or {@code 0} if none are found
241+
*/
226242
private int countInstances(String input, String value) {
227-
return input.split(value, -1).length - 1;
243+
int count = 0;
244+
int index = input.indexOf(value);
245+
while (index != -1) {
246+
count++;
247+
index = input.indexOf(value, index + value.length());
248+
}
249+
return count;
228250
}
229251

230252
private Logger getLogger() {

0 commit comments

Comments
 (0)