1 11 package org.eclipse.ui.internal.misc; 12 13 import java.util.Vector ; 14 15 18 public class StringMatcher { 19 protected String fPattern; 20 21 protected int fLength; 23 protected boolean fIgnoreWildCards; 24 25 protected boolean fIgnoreCase; 26 27 protected boolean fHasLeadingStar; 28 29 protected boolean fHasTrailingStar; 30 31 protected String fSegments[]; 33 34 protected int fBound = 0; 35 36 protected static final char fSingleWildCard = '\u0000'; 37 38 public static class Position { 39 int start; 41 int end; 43 public Position(int start, int end) { 44 this.start = start; 45 this.end = end; 46 } 47 48 public int getStart() { 49 return start; 50 } 51 52 public int getEnd() { 53 return end; 54 } 55 } 56 57 77 public StringMatcher(String pattern, boolean ignoreCase, 78 boolean ignoreWildCards) { 79 if (pattern == null) { 80 throw new IllegalArgumentException (); 81 } 82 fIgnoreCase = ignoreCase; 83 fIgnoreWildCards = ignoreWildCards; 84 fPattern = pattern; 85 fLength = pattern.length(); 86 87 if (fIgnoreWildCards) { 88 parseNoWildCards(); 89 } else { 90 parseWildCards(); 91 } 92 } 93 94 107 public StringMatcher.Position find(String text, int start, int end) { 108 if (text == null) { 109 throw new IllegalArgumentException (); 110 } 111 112 int tlen = text.length(); 113 if (start < 0) { 114 start = 0; 115 } 116 if (end > tlen) { 117 end = tlen; 118 } 119 if (end < 0 || start >= end) { 120 return null; 121 } 122 if (fLength == 0) { 123 return new Position(start, start); 124 } 125 if (fIgnoreWildCards) { 126 int x = posIn(text, start, end); 127 if (x < 0) { 128 return null; 129 } 130 return new Position(x, x + fLength); 131 } 132 133 int segCount = fSegments.length; 134 if (segCount == 0) { 135 return new Position(start, end); 136 } 137 138 int curPos = start; 139 int matchStart = -1; 140 int i; 141 for (i = 0; i < segCount && curPos < end; ++i) { 142 String current = fSegments[i]; 143 int nextMatch = regExpPosIn(text, curPos, end, current); 144 if (nextMatch < 0) { 145 return null; 146 } 147 if (i == 0) { 148 matchStart = nextMatch; 149 } 150 curPos = nextMatch + current.length(); 151 } 152 if (i < segCount) { 153 return null; 154 } 155 return new Position(matchStart, curPos); 156 } 157 158 163 public boolean match(String text) { 164 if(text == null) { 165 return false; 166 } 167 return match(text, 0, text.length()); 168 } 169 170 178 public boolean match(String text, int start, int end) { 179 if (null == text) { 180 throw new IllegalArgumentException (); 181 } 182 183 if (start > end) { 184 return false; 185 } 186 187 if (fIgnoreWildCards) { 188 return (end - start == fLength) 189 && fPattern.regionMatches(fIgnoreCase, 0, text, start, 190 fLength); 191 } 192 int segCount = fSegments.length; 193 if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) { 194 return true; 195 } 196 if (start == end) { 197 return fLength == 0; 198 } 199 if (fLength == 0) { 200 return start == end; 201 } 202 203 int tlen = text.length(); 204 if (start < 0) { 205 start = 0; 206 } 207 if (end > tlen) { 208 end = tlen; 209 } 210 211 int tCurPos = start; 212 int bound = end - fBound; 213 if (bound < 0) { 214 return false; 215 } 216 int i = 0; 217 String current = fSegments[i]; 218 int segLength = current.length(); 219 220 221 if (!fHasLeadingStar) { 222 if (!regExpRegionMatches(text, start, current, 0, segLength)) { 223 return false; 224 } else { 225 ++i; 226 tCurPos = tCurPos + segLength; 227 } 228 } 229 if ((fSegments.length == 1) && (!fHasLeadingStar) 230 && (!fHasTrailingStar)) { 231 return tCurPos == end; 233 } 234 235 while (i < segCount) { 236 current = fSegments[i]; 237 int currentMatch; 238 int k = current.indexOf(fSingleWildCard); 239 if (k < 0) { 240 currentMatch = textPosIn(text, tCurPos, end, current); 241 if (currentMatch < 0) { 242 return false; 243 } 244 } else { 245 currentMatch = regExpPosIn(text, tCurPos, end, current); 246 if (currentMatch < 0) { 247 return false; 248 } 249 } 250 tCurPos = currentMatch + current.length(); 251 i++; 252 } 253 254 255 if (!fHasTrailingStar && tCurPos != end) { 256 int clen = current.length(); 257 return regExpRegionMatches(text, end - clen, current, 0, clen); 258 } 259 return i == segCount; 260 } 261 262 266 private void parseNoWildCards() { 267 fSegments = new String [1]; 268 fSegments[0] = fPattern; 269 fBound = fLength; 270 } 271 272 276 private void parseWildCards() { 277 if (fPattern.startsWith("*")) { fHasLeadingStar = true; 279 } 280 if (fPattern.endsWith("*")) { 282 if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') { 283 fHasTrailingStar = true; 284 } 285 } 286 287 Vector temp = new Vector (); 288 289 int pos = 0; 290 StringBuffer buf = new StringBuffer (); 291 while (pos < fLength) { 292 char c = fPattern.charAt(pos++); 293 switch (c) { 294 case '\\': 295 if (pos >= fLength) { 296 buf.append(c); 297 } else { 298 char next = fPattern.charAt(pos++); 299 300 if (next == '*' || next == '?' || next == '\\') { 301 buf.append(next); 302 } else { 303 304 buf.append(c); 305 buf.append(next); 306 } 307 } 308 break; 309 case '*': 310 if (buf.length() > 0) { 311 312 temp.addElement(buf.toString()); 313 fBound += buf.length(); 314 buf.setLength(0); 315 } 316 break; 317 case '?': 318 319 buf.append(fSingleWildCard); 320 break; 321 default: 322 buf.append(c); 323 } 324 } 325 326 327 if (buf.length() > 0) { 328 temp.addElement(buf.toString()); 329 fBound += buf.length(); 330 } 331 332 fSegments = new String [temp.size()]; 333 temp.copyInto(fSegments); 334 } 335 336 342 protected int posIn(String text, int start, int end) { int max = end - fLength; 344 345 if (!fIgnoreCase) { 346 int i = text.indexOf(fPattern, start); 347 if (i == -1 || i > max) { 348 return -1; 349 } 350 return i; 351 } 352 353 for (int i = start; i <= max; ++i) { 354 if (text.regionMatches(true, i, fPattern, 0, fLength)) { 355 return i; 356 } 357 } 358 359 return -1; 360 } 361 362 369 protected int regExpPosIn(String text, int start, int end, String p) { 370 int plen = p.length(); 371 372 int max = end - plen; 373 for (int i = start; i <= max; ++i) { 374 if (regExpRegionMatches(text, i, p, 0, plen)) { 375 return i; 376 } 377 } 378 return -1; 379 } 380 381 390 protected boolean regExpRegionMatches(String text, int tStart, String p, 391 int pStart, int plen) { 392 while (plen-- > 0) { 393 char tchar = text.charAt(tStart++); 394 char pchar = p.charAt(pStart++); 395 396 397 if (!fIgnoreWildCards) { 398 399 if (pchar == fSingleWildCard) { 400 continue; 401 } 402 } 403 if (pchar == tchar) { 404 continue; 405 } 406 if (fIgnoreCase) { 407 if (Character.toUpperCase(tchar) == Character 408 .toUpperCase(pchar)) { 409 continue; 410 } 411 if (Character.toLowerCase(tchar) == Character 414 .toLowerCase(pchar)) { 415 continue; 416 } 417 } 418 return false; 419 } 420 return true; 421 } 422 423 430 protected int textPosIn(String text, int start, int end, String p) { 431 432 int plen = p.length(); 433 int max = end - plen; 434 435 if (!fIgnoreCase) { 436 int i = text.indexOf(p, start); 437 if (i == -1 || i > max) { 438 return -1; 439 } 440 return i; 441 } 442 443 for (int i = start; i <= max; ++i) { 444 if (text.regionMatches(true, i, p, 0, plen)) { 445 return i; 446 } 447 } 448 449 return -1; 450 } 451 } 452 | Popular Tags |