From dc443ec823831aada0cad4c69178f24ce2cb66c6 Mon Sep 17 00:00:00 2001 From: taehyun <126179088+KNU-K@users.noreply.github.com> Date: Mon, 18 Nov 2024 03:40:06 +0900 Subject: [PATCH] Refactor: Remove label usage for improved efficiency --- .../springframework/util/AntPathMatcher.java | 39 ++++++++++++++----- 1 file changed, 30 insertions(+), 9 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 9afaf2532381..f62356f936d6 100644 --- a/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java +++ b/spring-core/src/main/java/org/springframework/util/AntPathMatcher.java @@ -311,17 +311,12 @@ else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) { int strLength = (pathIdxEnd - pathIdxStart + 1); int foundIdx = -1; - strLoop: for (int i = 0; i <= strLength - patLength; i++) { - for (int j = 0; j < patLength; j++) { - String subPat = pattDirs[pattIdxStart + j + 1]; - String subStr = pathDirs[pathIdxStart + i + j]; - if (!matchStrings(subPat, subStr, uriTemplateVariables)) { - continue strLoop; - } + MatchContext context = new MatchContext(pattDirs, pathDirs, pattIdxStart, pathIdxStart, uriTemplateVariables ); + if(checkSubPatternMatch(context, i, patLength)){ + foundIdx = pathIdxStart + i; + break; } - foundIdx = pathIdxStart + i; - break; } if (foundIdx == -1) { @@ -340,6 +335,32 @@ else if (!fullMatch && "**".equals(pattDirs[pattIdxStart])) { return true; } + private static class MatchContext{ + String[] pattDirs; + String[] pathDirs; + int pattIdxStart; + int pathIdxStart; + Map uriTemplateVariables; + + MatchContext(String[] pattDirs, String[] pathDirs, int pattIdxStart, int pathIdxStart, Map uriTemplateVariables) { + this.pattDirs = pattDirs; + this.pathDirs = pathDirs; + this.pattIdxStart = pattIdxStart; + this.pathIdxStart = pathIdxStart; + this.uriTemplateVariables = uriTemplateVariables; + } + } + + private boolean checkSubPatternMatch(MatchContext context, int i, int patLength) { + for (int j = 0; j < patLength; j++) { + String subPat = context.pattDirs[context.pattIdxStart + j + 1]; + String subStr = context.pathDirs[context.pathIdxStart + i + j]; + if (!matchStrings(subPat, subStr, context.uriTemplateVariables)) { + return false; + } + } + return true; + } private boolean isPotentialMatch(String path, String[] pattDirs) { if (!this.trimTokens) {