Efficient checks for empty strings and single character matches

Closes gh-25552
Closes gh-25553
This commit is contained in:
Juergen Hoeller
2020-08-25 16:17:12 +02:00
parent 0d4040aa63
commit 04df9b8f49
15 changed files with 54 additions and 40 deletions

View File

@@ -50,7 +50,7 @@ class DefaultRequestPath implements RequestPath {
}
private static PathContainer initContextPath(PathContainer path, @Nullable String contextPath) {
if (!StringUtils.hasText(contextPath) || "/".equals(contextPath)) {
if (!StringUtils.hasText(contextPath) || StringUtils.matchesCharacter(contextPath, '/')) {
return PathContainer.parsePath("");
}
@@ -59,7 +59,7 @@ class DefaultRequestPath implements RequestPath {
int length = contextPath.length();
int counter = 0;
for (int i=0; i < path.elements().size(); i++) {
for (int i = 0; i < path.elements().size(); i++) {
PathContainer.Element element = path.elements().get(i);
counter += element.value().length();
if (length == counter) {

View File

@@ -170,7 +170,7 @@ public class UrlPathHelper {
}
// Else, use path within current servlet mapping if applicable
String rest = getPathWithinServletMapping(request);
if (!"".equals(rest)) {
if (StringUtils.hasLength(rest)) {
return rest;
}
else {
@@ -362,7 +362,7 @@ public class UrlPathHelper {
if (contextPath == null) {
contextPath = request.getContextPath();
}
if ("/".equals(contextPath)) {
if (StringUtils.matchesCharacter(contextPath, '/')) {
// Invalid case, but happens for includes on Jetty: silently adapt it.
contextPath = "";
}