Improve performance of StringUtils#cleanPath

This commit is contained in:
Christoph Dreis 2020-03-10 20:51:29 +01:00 committed by Juergen Hoeller
parent ac0363c94e
commit 1c6dda3ca4
2 changed files with 6 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -701,6 +701,10 @@ public abstract class StringUtils {
} }
} }
// All path elements stayed the same - shortcut
if (pathArray.length == pathElements.size()) {
return prefix + pathToUse;
}
// Remaining top paths need to be retained. // Remaining top paths need to be retained.
for (int i = 0; i < tops; i++) { for (int i = 0; i < tops; i++) {
pathElements.add(0, TOP_PATH); pathElements.add(0, TOP_PATH);

View File

@ -399,6 +399,7 @@ class StringUtilsTests {
assertThat(StringUtils.cleanPath("file:../")).isEqualTo("file:../"); assertThat(StringUtils.cleanPath("file:../")).isEqualTo("file:../");
assertThat(StringUtils.cleanPath("file:./../")).isEqualTo("file:../"); assertThat(StringUtils.cleanPath("file:./../")).isEqualTo("file:../");
assertThat(StringUtils.cleanPath("file:.././")).isEqualTo("file:../"); assertThat(StringUtils.cleanPath("file:.././")).isEqualTo("file:../");
assertThat(StringUtils.cleanPath("file:/mypath/spring.factories")).isEqualTo("file:/mypath/spring.factories");
assertThat(StringUtils.cleanPath("file:///c:/some/../path/the%20file.txt")).isEqualTo("file:///c:/path/the%20file.txt"); assertThat(StringUtils.cleanPath("file:///c:/some/../path/the%20file.txt")).isEqualTo("file:///c:/path/the%20file.txt");
} }