1 11 12 package org.eclipse.ui.internal.navigator; 13 14 import java.util.Vector ; 15 16 19 public class StringMatcher { 20 protected String fPattern; 21 22 protected int fLength; 24 protected boolean fIgnoreWildCards; 25 26 protected boolean fIgnoreCase; 27 28 protected boolean fHasLeadingStar; 29 30 protected boolean fHasTrailingStar; 31 32 protected String fSegments[]; 34 36 37 protected int fBound = 0; 38 39 protected static final char fSingleWildCard = '\u0000'; 40 41 44 static class Position { 45 int start; 47 int end; 49 Position(int start, int end) { 50 this.start = start; 51 this.end = end; 52 } 53 54 int getStart() { 55 return start; 56 } 57 58 int getEnd() { 59 return end; 60 } 61 } 62 63 86 public StringMatcher(String pattern, boolean ignoreCase, 87 boolean ignoreWildCards) { 88 if (pattern == null) { 89 throw new IllegalArgumentException (); 90 } 91 fIgnoreCase = ignoreCase; 92 fIgnoreWildCards = ignoreWildCards; 93 fPattern = pattern; 94 fLength = pattern.length(); 95 96 if (fIgnoreWildCards) { 97 parseNoWildCards(); 98 } else { 99 parseWildCards(); 100 } 101 } 102 103 117 public StringMatcher.Position find(String text, int start, int end) { 118 if (text == null) { 119 throw new IllegalArgumentException (); 120 } 121 122 int tlen = text.length(); 123 if (start < 0) { 124 start = 0; 125 } 126 if (end > tlen) { 127 end = tlen; 128 } 129 if (end < 0 || start >= end) { 130 return null; 131 } 132 if (fLength == 0) { 133 return new Position(start, start); 134 } 135 if (fIgnoreWildCards) { 136 int x = posIn(text, start, end); 137 if (x < 0) { 138 return null; 139 } 140 return new Position(x, x + fLength); 141 } 142 143 int segCount = fSegments.length; 144 if (segCount == 0) { 145 return new Position(start, end); 146 } 147 148 int curPos = start; 149 int matchStart = -1; 150 int i; 151 for (i = 0; i < segCount && curPos < end; ++i) { 152 String current = fSegments[i]; 153 int nextMatch = regExpPosIn(text, curPos, end, current); 154 if (nextMatch < 0) { 155 return null; 156 } 157 if (i == 0) { 158 matchStart = nextMatch; 159 } 160 curPos = nextMatch + current.length(); 161 } 162 if (i < segCount) { 163 return null; 164 } 165 return new Position(matchStart, curPos); 166 } 167 168 175 public boolean match(String text) { 176 if (text == null) { 177 return false; 178 } 179 return match(text, 0, text.length()); 180 } 181 182 195 public boolean match(String text, int start, int end) { 196 if (null == text) { 197 throw new IllegalArgumentException (); 198 } 199 200 if (start > end) { 201 return false; 202 } 203 204 if (fIgnoreWildCards) { 205 return (end - start == fLength) 206 && fPattern.regionMatches(fIgnoreCase, 0, text, start, 207 fLength); 208 } 209 int segCount = fSegments.length; 210 if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) { 211 return true; 215 } 216 if (start == end) { 217 return fLength == 0; 218 } 219 if (fLength == 0) { 220 return start == end; 221 } 222 223 int tlen = text.length(); 224 if (start < 0) { 225 start = 0; 226 } 227 if (end > tlen) { 228 end = tlen; 229 } 230 231 int tCurPos = start; 232 int bound = end - fBound; 233 if (bound < 0) { 234 return false; 235 } 236 int i = 0; 237 String current = fSegments[i]; 238 int segLength = current.length(); 239 240 241 if (!fHasLeadingStar) { 242 if (!regExpRegionMatches(text, start, current, 0, segLength)) { 243 return false; 244 } 245 ++i; 246 tCurPos = tCurPos + segLength; 247 248 } 249 if ((fSegments.length == 1) && (!fHasLeadingStar) 250 && (!fHasTrailingStar)) { 251 return tCurPos == end; 253 } 254 255 while (i < segCount) { 256 current = fSegments[i]; 257 int currentMatch; 258 int k = current.indexOf(fSingleWildCard); 259 if (k < 0) { 260 currentMatch = textPosIn(text, tCurPos, end, current); 261 if (currentMatch < 0) { 262 return false; 263 } 264 } else { 265 currentMatch = regExpPosIn(text, tCurPos, end, current); 266 if (currentMatch < 0) { 267 return false; 268 } 269 } 270 tCurPos = currentMatch + current.length(); 271 i++; 272 } 273 274 275 if (!fHasTrailingStar && tCurPos != end) { 276 int clen = current.length(); 277 return regExpRegionMatches(text, end - clen, current, 0, clen); 278 } 279 return i == segCount; 280 } 281 282 287 private void parseNoWildCards() { 288 fSegments = new String [1]; 289 fSegments[0] = fPattern; 290 fBound = fLength; 291 } 292 293 298 private void parseWildCards() { 299 if (fPattern.startsWith("*")) { fHasLeadingStar = true; 301 } 302 if (fPattern.endsWith("*")) { 304 if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') { 305 fHasTrailingStar = true; 306 } 307 } 308 309 Vector temp = new Vector (); 310 311 int pos = 0; 312 StringBuffer buf = new StringBuffer (); 313 while (pos < fLength) { 314 char c = fPattern.charAt(pos++); 315 switch (c) { 316 case '\\': 317 if (pos >= fLength) { 318 buf.append(c); 319 } else { 320 char next = fPattern.charAt(pos++); 321 322 if (next == '*' || next == '?' || next == '\\') { 323 buf.append(next); 324 } else { 325 326 buf.append(c); 327 buf.append(next); 328 } 329 } 330 break; 331 case '*': 332 if (buf.length() > 0) { 333 334 temp.addElement(buf.toString()); 335 fBound += buf.length(); 336 buf.setLength(0); 337 } 338 break; 339 case '?': 340 341 buf.append(fSingleWildCard); 342 break; 343 default: 344 buf.append(c); 345 } 346 } 347 348 349 if (buf.length() > 0) { 350 temp.addElement(buf.toString()); 351 fBound += buf.length(); 352 } 353 354 fSegments = new String [temp.size()]; 355 temp.copyInto(fSegments); 356 } 357 358 368 protected int posIn(String text, int start, int end) { int max = end - fLength; 371 372 if (!fIgnoreCase) { 373 int i = text.indexOf(fPattern, start); 374 if (i == -1 || i > max) { 375 return -1; 376 } 377 return i; 378 } 379 380 for (int i = start; i <= max; ++i) { 381 if (text.regionMatches(true, i, fPattern, 0, fLength)) { 382 return i; 383 } 384 } 385 386 return -1; 387 } 388 389 401 protected int regExpPosIn(String text, int start, int end, String p) { 402 int plen = p.length(); 403 404 int max = end - plen; 405 for (int i = start; i <= max; ++i) { 406 if (regExpRegionMatches(text, i, p, 0, plen)) { 407 return i; 408 } 409 } 410 return -1; 411 } 412 413 425 protected boolean regExpRegionMatches(String text, int tStart, String p, 426 int pStart, int plen) { 427 while (plen-- > 0) { 428 char tchar = text.charAt(tStart++); 429 char pchar = p.charAt(pStart++); 430 431 432 if (!fIgnoreWildCards) { 433 434 if (pchar == fSingleWildCard) { 435 continue; 436 } 437 } 438 if (pchar == tchar) { 439 continue; 440 } 441 if (fIgnoreCase) { 442 if (Character.toUpperCase(tchar) == Character 443 .toUpperCase(pchar)) { 444 continue; 445 } 446 if (Character.toLowerCase(tchar) == Character 450 .toLowerCase(pchar)) { 451 continue; 452 } 453 } 454 return false; 455 } 456 return true; 457 } 458 459 471 protected int textPosIn(String text, int start, int end, String p) { 472 473 int plen = p.length(); 474 int max = end - plen; 475 476 if (!fIgnoreCase) { 477 int i = text.indexOf(p, start); 478 if (i == -1 || i > max) { 479 return -1; 480 } 481 return i; 482 } 483 484 for (int i = start; i <= max; ++i) { 485 if (text.regionMatches(true, i, p, 0, plen)) { 486 return i; 487 } 488 } 489 490 return -1; 491 } 492 } 493 | Popular Tags |