Skip to content

Commit 1c6dda3

Browse files
dreis2211jhoeller
authored andcommitted
Improve performance of StringUtils#cleanPath
1 parent ac0363c commit 1c6dda3

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -701,6 +701,10 @@ else if (TOP_PATH.equals(element)) {
701701
}
702702
}
703703

704+
// All path elements stayed the same - shortcut
705+
if (pathArray.length == pathElements.size()) {
706+
return prefix + pathToUse;
707+
}
704708
// Remaining top paths need to be retained.
705709
for (int i = 0; i < tops; i++) {
706710
pathElements.add(0, TOP_PATH);

spring-core/src/test/java/org/springframework/util/StringUtilsTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,7 @@ void cleanPath() {
399399
assertThat(StringUtils.cleanPath("file:../")).isEqualTo("file:../");
400400
assertThat(StringUtils.cleanPath("file:./../")).isEqualTo("file:../");
401401
assertThat(StringUtils.cleanPath("file:.././")).isEqualTo("file:../");
402+
assertThat(StringUtils.cleanPath("file:/mypath/spring.factories")).isEqualTo("file:/mypath/spring.factories");
402403
assertThat(StringUtils.cleanPath("file:///c:/some/../path/the%20file.txt")).isEqualTo("file:///c:/path/the%20file.txt");
403404
}
404405

0 commit comments

Comments
 (0)