1
1
/*
2
- * Copyright 2002-2023 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -37,13 +37,24 @@ public abstract class PatternMatchUtils {
37
37
* @return whether the String matches the given pattern
38
38
*/
39
39
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 ) {
40
51
if (pattern == null || str == null ) {
41
52
return false ;
42
53
}
43
54
44
55
int firstIndex = pattern .indexOf ('*' );
45
56
if (firstIndex == -1 ) {
46
- return pattern .equals (str );
57
+ return ( ignoreCase ? pattern .equalsIgnoreCase ( str ) : pattern . equals (str ) );
47
58
}
48
59
49
60
if (firstIndex == 0 ) {
@@ -52,25 +63,43 @@ public static boolean simpleMatch(@Nullable String pattern, @Nullable String str
52
63
}
53
64
int nextIndex = pattern .indexOf ('*' , 1 );
54
65
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 ));
56
68
}
57
69
String part = pattern .substring (1 , nextIndex );
58
70
if (part .isEmpty ()) {
59
- return simpleMatch (pattern .substring (nextIndex ), str );
71
+ return simpleMatch (pattern .substring (nextIndex ), str , ignoreCase );
60
72
}
61
- int partIndex = str . indexOf (part );
73
+ int partIndex = indexOf (str , part , 0 , ignoreCase );
62
74
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 )) {
64
76
return true ;
65
77
}
66
- partIndex = str . indexOf (part , partIndex + 1 );
78
+ partIndex = indexOf (str , part , partIndex + 1 , ignoreCase );
67
79
}
68
80
return false ;
69
81
}
70
82
71
83
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 ;
74
103
}
75
104
76
105
/**
@@ -94,4 +123,18 @@ public static boolean simpleMatch(@Nullable String[] patterns, @Nullable String
94
123
return false ;
95
124
}
96
125
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
+
97
140
}
0 commit comments