1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
package ij.util;
/**
* This class allows for simple wildcard pattern matching. Possible
* patterns allow to match single characters ('?') or any count of
* characters ('*').<p>
* Wildcard characters can be escaped (default: by an '\').<p>
* This class always matches for the whole word.<p>
* Examples:
* <pre>
* WildcardMatch wm = new WildcardMatch();
* System.out.println(wm.match("CfgOptions.class", "C*.class")); // true
* System.out.println(wm.match("CfgOptions.class", "?gOpti*c?as?")); // false
* System.out.println(wm.match("CfgOptions.class", "??gOpti*c?ass")); // true
* System.out.println(wm.match("What's this?", "What*\\?")); // true
* System.out.println(wm.match("What's this?", "What*?")); // true
* System.out.println(wm.match("A \\ backslash", "*\\\\?back*")); // true
* </pre>
*/
public class WildcardMatch {
public WildcardMatch() {
}
public WildcardMatch(char singleChar, char multipleChars) {
this.sc = singleChar;
this.mc = multipleChars;
}
/**
* Sets new characters to be used as wildcard characters, overriding the
* the default of '?' for any single character match and '*' for any
* amount of characters, including 0 characters.
* @param singleChar The char used to match exactly ONE character.
* @param multipleChars The char used to match any amount of characters
* including o characters.
*/
public void setWildcardChars(char singleChar, char multipleChars) {
this.sc = singleChar;
this.mc = multipleChars;
}
/**
* Sets the new character to be used as an escape character, overriding the
* the default of '\'.
* @param escapeChar The char used to match escape wildcard characters.
*/
public void setEscapeChar(char escapeChar) {
this.ec = escapeChar;
}
/**
* Returns the character used to specify exactly one character.
* @return Wildcard character matching any single character.
*/
public char getSingleWildcardChar() {
return sc;
}
/**
* Returns the character used to specify any amount of characters.
* @return Wildcard character matching any count of characters.
*/
public char getMultipleWildcardChar() {
return mc;
}
/**
* Returns the character used to escape the wildcard functionality of a
* wildcard character. If two escape characters are used in sequence, they
* mean the escape character itself. It defaults to '\'.
* @return Escape character.
*/
public char getEscapeChar() {
return ec;
}
/**
* Makes pattern matching case insensitive.
* @param caseSensitive false for case insensitivity. Default is case
* sensitive match.
*/
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
/**
* Returns the current state of case sensitivity.
* @return true for case sensitive pattern matching, false otherwise.
*/
public boolean getCaseSensitive() {
return caseSensitive;
}
boolean preceededByMultipleChar = false;
/**
* Matches a string against a pattern with wildcards. Two wildcard types
* are supported: single character match (defaults to '?') and ANY
* character match ('*'), matching any count of characters including 0.
* Wildcard characters may be escaped by an escape character, which
* defaults to '\'.
* @param s The string, in which the search should be performed.
* @param pattern The search pattern string including wildcards.
* @return true, if string 's' matches 'pattern'.
*/
public boolean match(String s, String pattern) {
preceededByMultipleChar = false;
isEscaped = false;
if (!caseSensitive) {
pattern = pattern.toLowerCase();
s = s.toLowerCase();
}
int offset = 0;
while (true) {
String ps = getNextSubString(pattern);
int len = ps.length();
pattern = pattern.substring(len + escCnt);
if (len > 0 && isWildcard(ps.charAt(0)) && escCnt == 0) {
offset = getWildcardOffset(ps.charAt(0));
if (isSingleWildcardChar(ps.charAt(0))) {
s = s.substring(1);
// This is not yet enough: If a '*' precedes '?', 's' might be SHORTER
// than seen here, for this we need preceededByMultipleChar variable...
}
if (pattern.length() == 0) {
return s.length() <= offset || preceededByMultipleChar;
}
} else {
int idx = s.indexOf(ps);
if (idx < 0 || (idx > offset && !preceededByMultipleChar)) {
return false;
}
s = s.substring(idx + len);
preceededByMultipleChar = false;
}
if (pattern.length() == 0) {
return s.length() == 0;
}
}
}
private char sc = '?';
private char mc = '*';
private char ec = '\\'; // Escape character
private boolean caseSensitive = true;
private boolean isEscaped = false;
private int escCnt = 0;
private String getNextSubString(String pat) {
escCnt = 0;
if ("".equals(pat)) {
return "";
}
if (isWildcard(pat.charAt(0))) {
// if '?' is preceeded by '*', we need special considerations:
if (pat.length() > 1 && !isSingleWildcardChar(pat.charAt(0)) && isSingleWildcardChar(pat.charAt(1))) {
preceededByMultipleChar = true;
}
return pat.substring(0, 1);
} else {
String s = "";
int i = 0;
while (i < pat.length() && !isWildcard(pat.charAt(i), isEscaped)) {
if (pat.charAt(i) == ec) {
isEscaped = !isEscaped;
if (!isEscaped) {
s += pat.charAt(i);
}
escCnt++;
} else if (isWildcard(pat.charAt(i))) {
isEscaped = false;
s += pat.charAt(i);
} else {
s += pat.charAt(i);
}
i++;
}
return s;
}
}
private boolean isWildcard(char c, boolean isEsc) {
return !isEsc && isWildcard(c);
}
private boolean isSingleWildcardChar(char c) {
return c == sc;
}
private boolean isWildcard(char c) {
return c == mc || c == sc;
}
private int getWildcardOffset(char c) {
if (c == mc) {
return Integer.MAX_VALUE;
}
return 0;
}
}
|