1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.basepom.mojo.repack;
16
17 import static com.google.common.base.Preconditions.checkNotNull;
18
19
20
21
22
23
24 final class Wildcard {
25
26 private Wildcard() {
27 throw new AssertionError("Wildcard can not be instantiated");
28 }
29
30 static boolean wildcardMatch(String pattern, String value) {
31 checkNotNull(pattern, "pattern is null");
32 checkNotNull(value, "value is null");
33
34
35 if (pattern.isEmpty()) {
36 return value.isEmpty();
37 }
38
39
40 if (pattern.equals("*")) {
41 return true;
42 }
43
44 if (value.isEmpty()) {
45 return false;
46 }
47
48 return doGlobMatch(pattern, value);
49 }
50
51 static boolean doGlobMatch(String pattern, String value) {
52 int valueIndex = 0;
53 int patternIndex = 0;
54 int patternLength = pattern.length();
55 int valueLength = value.length();
56
57 for (; patternIndex < patternLength; valueIndex++, patternIndex++) {
58 char patternChar = pattern.charAt(patternIndex);
59
60
61
62 if (valueIndex == valueLength && patternChar != '*') {
63 return false;
64 }
65
66 switch (patternChar) {
67 case '*':
68
69 do {
70
71 if (patternIndex + 1 == patternLength) {
72 return true;
73 }
74 patternIndex++;
75
76 } while (pattern.charAt(patternIndex) == '*');
77
78 for (; valueIndex < valueLength; valueIndex++) {
79 boolean matched = doGlobMatch(value.substring(valueIndex), pattern.substring(patternIndex));
80 if (matched) {
81 return true;
82 }
83 }
84 return false;
85 case '?':
86 continue;
87 default:
88 if (value.charAt(valueIndex) != patternChar) {
89 return false;
90 }
91 }
92 }
93
94 return valueIndex == valueLength;
95 }
96 }