11/*
2- * Copyright 2002-2023 the original author or authors.
2+ * Copyright 2002-2025 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.
@@ -37,13 +37,24 @@ public abstract class PatternMatchUtils {
3737 * @return whether the String matches the given pattern
3838 */
3939 public static boolean simpleMatch (@ Nullable String pattern , @ Nullable String str ) {
40+ return simpleMatch (pattern , str , false );
41+ }
42+
43+ /**
44+ * Variant of {@link #simpleMatch(String, String)} that ignores upper/lower case.
45+ */
46+ public static boolean simpleMatchIgnoreCase (@ Nullable String pattern , @ Nullable String str ) {
47+ return simpleMatch (pattern , str , true );
48+ }
49+
50+ private static boolean simpleMatch (@ Nullable String pattern , @ Nullable String str , boolean ignoreCase ) {
4051 if (pattern == null || str == null ) {
4152 return false ;
4253 }
4354
4455 int firstIndex = pattern .indexOf ('*' );
4556 if (firstIndex == -1 ) {
46- return pattern .equals (str );
57+ return ( ignoreCase ? pattern .equalsIgnoreCase ( str ) : pattern . equals (str ) );
4758 }
4859
4960 if (firstIndex == 0 ) {
@@ -52,25 +63,43 @@ public static boolean simpleMatch(@Nullable String pattern, @Nullable String str
5263 }
5364 int nextIndex = pattern .indexOf ('*' , 1 );
5465 if (nextIndex == -1 ) {
55- return str .endsWith (pattern .substring (1 ));
66+ String part = pattern .substring (1 );
67+ return (ignoreCase ? StringUtils .endsWithIgnoreCase (str , part ) : str .endsWith (part ));
5668 }
5769 String part = pattern .substring (1 , nextIndex );
5870 if (part .isEmpty ()) {
59- return simpleMatch (pattern .substring (nextIndex ), str );
71+ return simpleMatch (pattern .substring (nextIndex ), str , ignoreCase );
6072 }
61- int partIndex = str . indexOf (part );
73+ int partIndex = indexOf (str , part , 0 , ignoreCase );
6274 while (partIndex != -1 ) {
63- if (simpleMatch (pattern .substring (nextIndex ), str .substring (partIndex + part .length ()))) {
75+ if (simpleMatch (pattern .substring (nextIndex ), str .substring (partIndex + part .length ()), ignoreCase )) {
6476 return true ;
6577 }
66- partIndex = str . indexOf (part , partIndex + 1 );
78+ partIndex = indexOf (str , part , partIndex + 1 , ignoreCase );
6779 }
6880 return false ;
6981 }
7082
7183 return (str .length () >= firstIndex &&
72- pattern .startsWith (str .substring (0 , firstIndex )) &&
73- simpleMatch (pattern .substring (firstIndex ), str .substring (firstIndex )));
84+ checkStartsWith (pattern , str , firstIndex , ignoreCase ) &&
85+ simpleMatch (pattern .substring (firstIndex ), str .substring (firstIndex ), ignoreCase ));
86+ }
87+
88+ private static boolean checkStartsWith (String pattern , String str , int index , boolean ignoreCase ) {
89+ String part = str .substring (0 , index );
90+ return (ignoreCase ? StringUtils .startsWithIgnoreCase (pattern , part ) : pattern .startsWith (part ));
91+ }
92+
93+ private static int indexOf (String str , String otherStr , int startIndex , boolean ignoreCase ) {
94+ if (!ignoreCase ) {
95+ return str .indexOf (otherStr , startIndex );
96+ }
97+ for (int i = startIndex ; i <= (str .length () - otherStr .length ()); i ++) {
98+ if (str .regionMatches (true , i , otherStr , 0 , otherStr .length ())) {
99+ return i ;
100+ }
101+ }
102+ return -1 ;
74103 }
75104
76105 /**
@@ -94,4 +123,18 @@ public static boolean simpleMatch(@Nullable String[] patterns, @Nullable String
94123 return false ;
95124 }
96125
126+ /**
127+ * Variant of {@link #simpleMatch(String[], String)} that ignores upper/lower case.
128+ */
129+ public static boolean simpleMatchIgnoreCase (@ Nullable String [] patterns , @ Nullable String str ) {
130+ if (patterns != null ) {
131+ for (String pattern : patterns ) {
132+ if (simpleMatch (pattern , str , true )) {
133+ return true ;
134+ }
135+ }
136+ }
137+ return false ;
138+ }
139+
97140}
0 commit comments