1 11 package org.eclipse.jdt.internal.ui.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 if (pattern == null) 69 throw new IllegalArgumentException (); 70 fIgnoreCase= ignoreCase; 71 fIgnoreWildCards= ignoreWildCards; 72 fPattern= pattern; 73 fLength= pattern.length(); 74 75 if (fIgnoreWildCards) { 76 parseNoWildCards(); 77 } else { 78 parseWildCards(); 79 } 80 } 81 94 public StringMatcher.Position find(String text, int start, int end) { 95 if (text == null) 96 throw new IllegalArgumentException (); 97 98 int tlen= text.length(); 99 if (start < 0) 100 start= 0; 101 if (end > tlen) 102 end= tlen; 103 if (end < 0 ||start >= end ) 104 return null; 105 if (fLength == 0) 106 return new Position(start, start); 107 if (fIgnoreWildCards) { 108 int x= posIn(text, start, end); 109 if (x < 0) 110 return null; 111 return new Position(x, x+fLength); 112 } 113 114 int segCount= fSegments.length; 115 if (segCount == 0) return new Position (start, end); 117 118 int curPos= start; 119 int matchStart= -1; 120 int i; 121 for (i= 0; i < segCount && curPos < end; ++i) { 122 String current= fSegments[i]; 123 int nextMatch= regExpPosIn(text, curPos, end, current); 124 if (nextMatch < 0 ) 125 return null; 126 if(i == 0) 127 matchStart= nextMatch; 128 curPos= nextMatch + current.length(); 129 } 130 if (i < segCount) 131 return null; 132 return new Position(matchStart, curPos); 133 } 134 139 public boolean match(String text) { 140 return match(text, 0, text.length()); 141 } 142 150 public boolean match(String text, int start, int end) { 151 if (null == text) 152 throw new IllegalArgumentException (); 153 154 if (start > end) 155 return false; 156 157 if (fIgnoreWildCards) 158 return (end - start == fLength) && fPattern.regionMatches(fIgnoreCase, 0, text, start, fLength); 159 int segCount= fSegments.length; 160 if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) return true; 162 if (start == end) 163 return fLength == 0; 164 if (fLength == 0) 165 return start == end; 166 167 int tlen= text.length(); 168 if (start < 0) 169 start= 0; 170 if (end > tlen) 171 end= tlen; 172 173 int tCurPos= start; 174 int bound= end - fBound; 175 if ( bound < 0) 176 return false; 177 int i=0; 178 String current= fSegments[i]; 179 int segLength= current.length(); 180 181 182 if (!fHasLeadingStar){ 183 if(!regExpRegionMatches(text, start, current, 0, segLength)) { 184 return false; 185 } else { 186 ++i; 187 tCurPos= tCurPos + segLength; 188 } 189 } 190 if ((fSegments.length == 1) && (!fHasLeadingStar) && (!fHasTrailingStar)) { 191 return tCurPos == end; 193 } 194 195 while (i < segCount) { 196 current= fSegments[i]; 197 int currentMatch; 198 int k= current.indexOf(fSingleWildCard); 199 if (k < 0) { 200 currentMatch= textPosIn(text, tCurPos, end, current); 201 if (currentMatch < 0) 202 return false; 203 } else { 204 currentMatch= regExpPosIn(text, tCurPos, end, current); 205 if (currentMatch < 0) 206 return false; 207 } 208 tCurPos= currentMatch + current.length(); 209 i++; 210 } 211 212 213 if (!fHasTrailingStar && tCurPos != end) { 214 int clen= current.length(); 215 return regExpRegionMatches(text, end - clen, current, 0, clen); 216 } 217 return i == segCount ; 218 } 219 220 224 private void parseNoWildCards() { 225 fSegments= new String [1]; 226 fSegments[0]= fPattern; 227 fBound= fLength; 228 } 229 232 private void parseWildCards() { 233 if(fPattern.startsWith("*")) fHasLeadingStar= true; 235 if(fPattern.endsWith("*")) { 237 if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') { 238 fHasTrailingStar= true; 239 } 240 } 241 242 Vector temp= new Vector(); 243 244 int pos= 0; 245 StringBuffer buf= new StringBuffer (); 246 while (pos < fLength) { 247 char c= fPattern.charAt(pos++); 248 switch (c) { 249 case '\\': 250 if (pos >= fLength) { 251 buf.append(c); 252 } else { 253 char next= fPattern.charAt(pos++); 254 255 if (next == '*' || next == '?' || next == '\\') { 256 buf.append(next); 257 } else { 258 259 buf.append(c); 260 buf.append(next); 261 } 262 } 263 break; 264 case '*': 265 if (buf.length() > 0) { 266 267 temp.addElement(buf.toString()); 268 fBound += buf.length(); 269 buf.setLength(0); 270 } 271 break; 272 case '?': 273 274 buf.append(fSingleWildCard); 275 break; 276 default: 277 buf.append(c); 278 } 279 } 280 281 282 if (buf.length() > 0) { 283 temp.addElement(buf.toString()); 284 fBound += buf.length(); 285 } 286 287 fSegments= new String [temp.size()]; 288 temp.copyInto(fSegments); 289 } 290 296 protected int posIn(String text, int start, int end) { int max= end - fLength; 298 299 if (!fIgnoreCase) { 300 int i= text.indexOf(fPattern, start); 301 if (i == -1 || i > max) 302 return -1; 303 return i; 304 } 305 306 for (int i= start; i <= max; ++i) { 307 if (text.regionMatches(true, i, fPattern, 0, fLength)) 308 return i; 309 } 310 311 return -1; 312 } 313 320 protected int regExpPosIn(String text, int start, int end, String p) { 321 int plen= p.length(); 322 323 int max= end - plen; 324 for (int i= start; i <= max; ++i) { 325 if (regExpRegionMatches(text, i, p, 0, plen)) 326 return i; 327 } 328 return -1; 329 } 330 331 332 protected boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) { 333 while (plen-- > 0) { 334 char tchar= text.charAt(tStart++); 335 char pchar= p.charAt(pStart++); 336 337 338 if (!fIgnoreWildCards) { 339 340 if (pchar == fSingleWildCard) { 341 continue; 342 } 343 } 344 if (pchar == tchar) 345 continue; 346 if (fIgnoreCase) { 347 if (Character.toUpperCase(tchar) == Character.toUpperCase(pchar)) 348 continue; 349 if (Character.toLowerCase(tchar) == Character.toLowerCase(pchar)) 352 continue; 353 } 354 return false; 355 } 356 return true; 357 } 358 365 protected int textPosIn(String text, int start, int end, String p) { 366 367 int plen= p.length(); 368 int max= end - plen; 369 370 if (!fIgnoreCase) { 371 int i= text.indexOf(p, start); 372 if (i == -1 || i > max) 373 return -1; 374 return i; 375 } 376 377 for (int i= start; i <= max; ++i) { 378 if (text.regionMatches(true, i, p, 0, plen)) 379 return i; 380 } 381 382 return -1; 383 } 384 } 385 | Popular Tags |