Skip to content

Commit 84d2e5a

Browse files
committed
Optimize AntPathMatcher when checking for potential matches
Issue: SPR-15477 (cherry picked from commit baa7b1c)
1 parent dab56db commit 84d2e5a

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

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

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 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.
@@ -220,8 +220,7 @@ protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<St
220220
if (pathIdxStart > pathIdxEnd) {
221221
// Path is exhausted, only match if rest of pattern is * or **'s
222222
if (pattIdxStart > pattIdxEnd) {
223-
return (pattern.endsWith(this.pathSeparator) ? path.endsWith(this.pathSeparator) :
224-
!path.endsWith(this.pathSeparator));
223+
return (pattern.endsWith(this.pathSeparator) == path.endsWith(this.pathSeparator));
225224
}
226225
if (!fullMatch) {
227226
return true;
@@ -318,34 +317,32 @@ else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) {
318317

319318
private boolean isPotentialMatch(String path, String[] pattDirs) {
320319
if (!this.trimTokens) {
321-
char[] pathChars = path.toCharArray();
322320
int pos = 0;
323321
for (String pattDir : pattDirs) {
324322
int skipped = skipSeparator(path, pos, this.pathSeparator);
325323
pos += skipped;
326-
skipped = skipSegment(pathChars, pos, pattDir);
324+
skipped = skipSegment(path, pos, pattDir);
327325
if (skipped < pattDir.length()) {
328-
if (skipped > 0) {
329-
return true;
330-
}
331-
return (pattDir.length() > 0) && isWildcardChar(pattDir.charAt(0));
326+
return (skipped > 0 || (pattDir.length() > 0 && isWildcardChar(pattDir.charAt(0))));
332327
}
333328
pos += skipped;
334329
}
335330
}
336331
return true;
337332
}
338333

339-
private int skipSegment(char[] chars, int pos, String prefix) {
334+
private int skipSegment(String path, int pos, String prefix) {
340335
int skipped = 0;
341-
for (char c : prefix.toCharArray()) {
336+
for (int i = 0; i < prefix.length(); i++) {
337+
char c = prefix.charAt(i);
342338
if (isWildcardChar(c)) {
343339
return skipped;
344340
}
345-
else if (pos + skipped >= chars.length) {
341+
int currPos = pos + skipped;
342+
if (currPos >= path.length()) {
346343
return 0;
347344
}
348-
else if (chars[pos + skipped] == c) {
345+
if (c == path.charAt(currPos)) {
349346
skipped++;
350347
}
351348
}

0 commit comments

Comments
 (0)