1 11 package org.eclipse.pde.internal.core.util; 12 13 import java.util.*; 14 15 18 public class StringMatcher { 19 protected String fPattern; 20 protected int fLength; protected boolean fIgnoreWildCards; 22 protected boolean fIgnoreCase; 23 protected boolean fHasLeadingStar; 24 protected boolean fHasTrailingStar; 25 protected String fSegments[]; 27 28 protected int fBound= 0; 29 30 31 protected static final char fSingleWildCard= '\u0000'; 32 33 public static class Position { 34 int start; int end; public Position(int start, int end) { 37 this.start= start; 38 this.end= end; 39 } 40 public int getStart() { 41 return start; 42 } 43 public int getEnd() { 44 return end; 45 } 46 } 47 67 public StringMatcher(String pattern, boolean ignoreCase, boolean ignoreWildCards) { 68 fIgnoreCase= ignoreCase; 69 fIgnoreWildCards= ignoreWildCards; 70 setPattern(pattern); 71 } 72 73 public void setPattern(String pattern) { 74 if (pattern == null) 75 throw new IllegalArgumentException (); 76 77 fPattern= pattern; 78 fLength= pattern.length(); 79 fHasLeadingStar = false; 80 fHasTrailingStar = false; 81 fBound= 0; 82 fSegments = null; 83 84 if (fIgnoreWildCards) { 85 parseNoWildCards(); 86 } else { 87 parseWildCards(); 88 } 89 } 90 103 public StringMatcher.Position find(String text, int start, int end) { 104 if (text == null) 105 throw new IllegalArgumentException (); 106 107 int tlen= text.length(); 108 if (start < 0) 109 start= 0; 110 if (end > tlen) 111 end= tlen; 112 if (end < 0 ||start >= end ) 113 return null; 114 if (fLength == 0) 115 return new Position(start, start); 116 if (fIgnoreWildCards) { 117 int x= posIn(text, start, end); 118 if (x < 0) 119 return null; 120 return new Position(x, x+fLength); 121 } 122 123 int segCount= fSegments.length; 124 if (segCount == 0) return new Position (start, end); 126 127 int curPos= start; 128 int matchStart= -1; 129 int i; 130 for (i= 0; i < segCount && curPos < end; ++i) { 131 String current= fSegments[i]; 132 int nextMatch= regExpPosIn(text, curPos, end, current); 133 if (nextMatch < 0 ) 134 return null; 135 if(i == 0) 136 matchStart= nextMatch; 137 curPos= nextMatch + current.length(); 138 } 139 if (i < segCount) 140 return null; 141 return new Position(matchStart, curPos); 142 } 143 148 public boolean match(String text) { 149 return match(text, 0, text.length()); 150 } 151 159 public boolean match(String text, int start, int end) { 160 if (null == text) 161 throw new IllegalArgumentException (); 162 163 if (start > end) 164 return false; 165 166 if (fIgnoreWildCards) 167 return (end - start == fLength) 168 && fPattern.regionMatches(fIgnoreCase, 0, text, start, fLength); 169 int segCount = fSegments.length; 170 if (segCount == 0 171 && (fHasLeadingStar 172 || fHasTrailingStar)) return true; 174 if (start == end) 175 return fLength == 0; 176 if (fLength == 0) 177 return start == end; 178 179 int tlen = text.length(); 180 if (start < 0) 181 start = 0; 182 if (end > tlen) 183 end = tlen; 184 185 int tCurPos = start; 186 int bound = end - fBound; 187 if (bound < 0) 188 return false; 189 int i = 0; 190 String current = fSegments[i]; 191 int segLength = current.length(); 192 193 194 if (!fHasLeadingStar) { 195 if (!regExpRegionMatches(text, start, current, 0, segLength)) { 196 return false; 197 } else { 198 ++i; 199 tCurPos = tCurPos + segLength; 200 } 201 } 202 if ((fSegments.length == 1) 203 && (!fHasLeadingStar) 204 && (!fHasTrailingStar)) { 205 return tCurPos == end; 207 } 208 209 while (i < segCount) { 210 current = fSegments[i]; 211 int currentMatch; 212 int k = current.indexOf(fSingleWildCard); 213 if (k < 0) { 214 currentMatch = textPosIn(text, tCurPos, end, current); 215 if (currentMatch < 0) 216 return false; 217 } else { 218 currentMatch = regExpPosIn(text, tCurPos, end, current); 219 if (currentMatch < 0) 220 return false; 221 } 222 tCurPos = currentMatch + current.length(); 223 i++; 224 } 225 226 227 if (!fHasTrailingStar && tCurPos != end) { 228 int clen = current.length(); 229 return regExpRegionMatches(text, end - clen, current, 0, clen); 230 } 231 return i == segCount; 232 } 233 237 private void parseNoWildCards() { 238 fSegments= new String [1]; 239 fSegments[0]= fPattern; 240 fBound= fLength; 241 } 242 246 private void parseWildCards() { 247 if(fPattern.startsWith("*")) fHasLeadingStar= true; 249 if(fPattern.endsWith("*")) { 251 if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') { 252 fHasTrailingStar= true; 253 } 254 } 255 256 Vector temp= new Vector(); 257 258 int pos= 0; 259 StringBuffer buf= new StringBuffer (); 260 while (pos < fLength) { 261 char c= fPattern.charAt(pos++); 262 switch (c) { 263 case '\\': 264 if (pos >= fLength) { 265 buf.append(c); 266 } else { 267 char next= fPattern.charAt(pos++); 268 269 if (next == '*' || next == '?' || next == '\\') { 270 buf.append(next); 271 } else { 272 273 buf.append(c); 274 buf.append(next); 275 } 276 } 277 break; 278 case '*': 279 if (buf.length() > 0) { 280 281 temp.addElement(buf.toString()); 282 fBound += buf.length(); 283 buf.setLength(0); 284 } 285 break; 286 case '?': 287 288 buf.append(fSingleWildCard); 289 break; 290 default: 291 buf.append(c); 292 } 293 } 294 295 296 if (buf.length() > 0) { 297 temp.addElement(buf.toString()); 298 fBound += buf.length(); 299 } 300 301 fSegments= new String [temp.size()]; 302 temp.copyInto(fSegments); 303 } 304 310 protected int posIn(String text, int start, int end) { int max= end - fLength; 312 313 if (!fIgnoreCase) { 314 int i= text.indexOf(fPattern, start); 315 if (i == -1 || i > max) 316 return -1; 317 return i; 318 } 319 320 for (int i= start; i <= max; ++i) { 321 if (text.regionMatches(true, i, fPattern, 0, fLength)) 322 return i; 323 } 324 325 return -1; 326 } 327 335 protected int regExpPosIn(String text, int start, int end, String p) { 336 int plen= p.length(); 337 338 int max= end - plen; 339 for (int i= start; i <= max; ++i) { 340 if (regExpRegionMatches(text, i, p, 0, plen)) 341 return i; 342 } 343 return -1; 344 } 345 354 protected boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) { 355 while (plen-- > 0) { 356 char tchar= text.charAt(tStart++); 357 char pchar= p.charAt(pStart++); 358 359 360 if (!fIgnoreWildCards) { 361 362 if (pchar == fSingleWildCard) { 363 continue; 364 } 365 } 366 if (pchar == tchar) 367 continue; 368 if (fIgnoreCase) { 369 if (Character.toUpperCase(tchar) == Character.toUpperCase(pchar)) 370 continue; 371 if (Character.toLowerCase(tchar) == Character.toLowerCase(pchar)) 374 continue; 375 } 376 return false; 377 } 378 return true; 379 } 380 388 protected int textPosIn(String text, int start, int end, String p) { 389 390 int plen= p.length(); 391 int max= end - plen; 392 393 if (!fIgnoreCase) { 394 int i= text.indexOf(p, start); 395 if (i == -1 || i > max) 396 return -1; 397 return i; 398 } 399 400 for (int i= start; i <= max; ++i) { 401 if (text.regionMatches(true, i, p, 0, plen)) 402 return i; 403 } 404 405 return -1; 406 } 407 } 408 | Popular Tags |