From 84d2e5a3c8a2b559c339996cbc191dddea681748 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Thu, 27 Apr 2017 21:31:50 +0200 Subject: [PATCH] Optimize AntPathMatcher when checking for potential matches Issue: SPR-15477 (cherry picked from commit baa7b1c) --- .../springframework/util/AntPathMatcher.java | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java index 51c479dd26a..50d1e9e9b69 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -220,8 +220,7 @@ public class AntPathMatcher implements PathMatcher { if (pathIdxStart > pathIdxEnd) { // Path is exhausted, only match if rest of pattern is * or **'s if (pattIdxStart > pattIdxEnd) { - return (pattern.endsWith(this.pathSeparator) ? path.endsWith(this.pathSeparator) : - !path.endsWith(this.pathSeparator)); + return (pattern.endsWith(this.pathSeparator) == path.endsWith(this.pathSeparator)); } if (!fullMatch) { return true; @@ -318,17 +317,13 @@ public class AntPathMatcher implements PathMatcher { private boolean isPotentialMatch(String path, String[] pattDirs) { if (!this.trimTokens) { - char[] pathChars = path.toCharArray(); int pos = 0; for (String pattDir : pattDirs) { int skipped = skipSeparator(path, pos, this.pathSeparator); pos += skipped; - skipped = skipSegment(pathChars, pos, pattDir); + skipped = skipSegment(path, pos, pattDir); if (skipped < pattDir.length()) { - if (skipped > 0) { - return true; - } - return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0)); + return (skipped > 0 || (pattDir.length() > 0 && isWildcardChar(pattDir.charAt(0)))); } pos += skipped; } @@ -336,16 +331,18 @@ public class AntPathMatcher implements PathMatcher { return true; } - private int skipSegment(char[] chars, int pos, String prefix) { + private int skipSegment(String path, int pos, String prefix) { int skipped = 0; - for (char c : prefix.toCharArray()) { + for (int i = 0; i < prefix.length(); i++) { + char c = prefix.charAt(i); if (isWildcardChar(c)) { return skipped; } - else if (pos + skipped >= chars.length) { + int currPos = pos + skipped; + if (currPos >= path.length()) { return 0; } - else if (chars[pos + skipped] == c) { + if (c == path.charAt(currPos)) { skipped++; } }