Removed StringMatcher and added RegEx support
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchScope.java b/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchScope.java
index b39efca..5b2646f 100644
--- a/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchScope.java
+++ b/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchScope.java
@@ -14,6 +14,8 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
@@ -24,7 +26,6 @@
import org.eclipse.search.internal.core.SearchScope;
import org.eclipse.search.internal.ui.SearchMessages;
-import org.eclipse.search.internal.ui.util.StringMatcher;
/**
* A special text search scope that take file extensions into account.
@@ -101,8 +102,70 @@
* Adds an extension to the scope.
*/
public void addExtension(String extension) {
- fExtensions.add(new StringMatcher(extension, true, false));
+ Pattern pattern= Pattern.compile(asRegEx(extension), Pattern.CASE_INSENSITIVE);
+ fExtensions.add(pattern.matcher("")); //$NON-NLS-1$
}
+
+ /*
+ * Converts '*' and '?' to regEx variables.
+ */
+ private String asRegEx(String pattern) {
+
+ StringBuffer out= new StringBuffer(pattern.length());
+
+ boolean escaped= false;
+ boolean quoting= false;
+
+ int i= 0;
+ while (i < pattern.length()) {
+ char ch= pattern.charAt(i++);
+
+ if (ch == '*' && !escaped) {
+ if (quoting) {
+ out.append("\\E"); //$NON-NLS-1$
+ quoting= false;
+ }
+ out.append(".*"); //$NON-NLS-1$
+ escaped= false;
+ continue;
+ } else if (ch == '?' && !escaped) {
+ if (quoting) {
+ out.append("\\E"); //$NON-NLS-1$
+ quoting= false;
+ }
+ out.append("."); //$NON-NLS-1$
+ escaped= false;
+ continue;
+ } else if (ch == '\\' && !escaped) {
+ escaped= true;
+ continue;
+
+ } else if (ch == '\\' && escaped) {
+ escaped= false;
+ if (quoting) {
+ out.append("\\E"); //$NON-NLS-1$
+ quoting= false;
+ }
+ out.append("\\\\"); //$NON-NLS-1$
+ continue;
+ }
+
+ if (!quoting) {
+ out.append("\\Q"); //$NON-NLS-1$
+ quoting= true;
+ }
+ if (escaped && ch != '*' && ch != '?' && ch != '\\')
+ out.append('\\');
+ out.append(ch);
+ escaped= ch == '\\';
+
+ }
+ if (quoting)
+ out.append("\\E"); //$NON-NLS-1$
+
+ return out.toString();
+ }
+
/**
* Adds all string patterns contained in <code>extensions</code> to this
* scope. The allowed pattern characters are <code>*</code> for any character
@@ -132,7 +195,7 @@
if (proxy != null) {
Iterator iter= fExtensions.iterator();
while (iter.hasNext()) {
- if (((StringMatcher)iter.next()).match(proxy.getName()))
+ if (((Matcher)iter.next()).reset(proxy.getName()).matches())
return false;
}
}
@@ -154,7 +217,7 @@
if (file != null) {
Iterator iter= fExtensions.iterator();
while (iter.hasNext()) {
- if (((StringMatcher)iter.next()).match(file.getName()))
+ if (((Matcher)iter.next()).reset(file.getName()).matches())
return false;
}
}
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java b/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java
index 38805f4..48c2d60 100644
--- a/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java
+++ b/org.eclipse.search/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java
@@ -21,6 +21,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
@@ -48,7 +50,6 @@
import org.eclipse.search.internal.core.ISearchScope;
import org.eclipse.search.internal.ui.SearchMessages;
import org.eclipse.search.internal.ui.SearchPlugin;
-import org.eclipse.search.internal.ui.util.StringMatcher;
/**
* The visitor that does the actual work.
@@ -57,14 +58,14 @@
protected static final int fgLF= '\n';
protected static final int fgCR= '\r';
- private String fPattern;
+ private String fPatternStr;
private ISearchScope fScope;
private ITextSearchResultCollector fCollector;
private String fOptions;
private IEditorPart[] fDirtyEditors;
private IProgressMonitor fProgressMonitor;
- private StringMatcher fMatcher;
+ private Matcher fMatcher;
private Integer[] fMessageFormatArgs;
private int fNumberOfScannedFiles;
@@ -77,7 +78,7 @@
public TextSearchVisitor(String pattern, String options, ISearchScope scope, ITextSearchResultCollector collector, MultiStatus status, int fileCount) {
super(status);
- fPattern= pattern;
+ fPatternStr= pattern;
fScope= scope;
fCollector= collector;
fPushback= false;
@@ -87,11 +88,80 @@
fOptions= ""; //$NON-NLS-1$
fProgressMonitor= collector.getProgressMonitor();
- fMatcher= new StringMatcher(pattern, options.indexOf('i') != -1, false);
+ Pattern regExPattern;
+ if (options.indexOf('r') == -1)
+ pattern= asRegEx(pattern);
+ if (options.indexOf('i') != -1)
+ regExPattern= Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
+ else
+ regExPattern= Pattern.compile(pattern);
+
+ fMatcher= regExPattern.matcher(""); //$NON-NLS-1$
+
fNumberOfScannedFiles= 0;
fNumberOfFilesToScan= fileCount;
fMessageFormatArgs= new Integer[] { new Integer(0), new Integer(fileCount) };
}
+
+ /*
+ * Converts '*' and '?' to regEx variables.
+ */
+ private String asRegEx(String pattern) {
+
+ StringBuffer out= new StringBuffer(pattern.length());
+
+ boolean escaped= false;
+ boolean quoting= false;
+
+ int i= 0;
+ while (i < pattern.length()) {
+ char ch= pattern.charAt(i++);
+
+ if (ch == '*' && !escaped) {
+ if (quoting) {
+ out.append("\\E"); //$NON-NLS-1$
+ quoting= false;
+ }
+ out.append(".*"); //$NON-NLS-1$
+ escaped= false;
+ continue;
+ } else if (ch == '?' && !escaped) {
+ if (quoting) {
+ out.append("\\E"); //$NON-NLS-1$
+ quoting= false;
+ }
+ out.append("."); //$NON-NLS-1$
+ escaped= false;
+ continue;
+ } else if (ch == '\\' && !escaped) {
+ escaped= true;
+ continue;
+
+ } else if (ch == '\\' && escaped) {
+ escaped= false;
+ if (quoting) {
+ out.append("\\E"); //$NON-NLS-1$
+ quoting= false;
+ }
+ out.append("\\\\"); //$NON-NLS-1$
+ continue;
+ }
+
+ if (!quoting) {
+ out.append("\\Q"); //$NON-NLS-1$
+ quoting= true;
+ }
+ if (escaped && ch != '*' && ch != '?' && ch != '\\')
+ out.append('\\');
+ out.append(ch);
+ escaped= ch == '\\';
+
+ }
+ if (quoting)
+ out.append("\\E"); //$NON-NLS-1$
+
+ return out.toString();
+ }
public void process(Collection projects) {
Iterator i= projects.iterator();
@@ -142,7 +212,7 @@
if (proxy.isDerived())
return false;
- if (fPattern.length() == 0) {
+ if (fPatternStr.length() == 0) {
fCollector.accept(proxy, "", -1, 0, -1); //$NON-NLS-1$
updateProgressMonitor();
return true;
@@ -169,13 +239,13 @@
int start= 0;
eof= eolStrLength == -1;
String line= sb.toString();
- StringMatcher.Position match;
while (start < lineLength) {
- if ((match= fMatcher.find(line, start, lineLength)) != null) {
- start= charCounter + match.getStart();
- int length= match.getEnd() - match.getStart();
+ fMatcher.reset(line);
+ if (fMatcher.find(start)) {
+ start= charCounter + fMatcher.start();
+ int length= fMatcher.end() - fMatcher.start();
fCollector.accept(proxy, line.trim(), start, length, lineCounter);
- start= match.getEnd();
+ start= fMatcher.end();
}
else // no match in this line
start= lineLength;
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/ui/SearchMessages.properties b/org.eclipse.search/search/org/eclipse/search/internal/ui/SearchMessages.properties
index e655d57..9cc3468 100644
--- a/org.eclipse.search/search/org/eclipse/search/internal/ui/SearchMessages.properties
+++ b/org.eclipse.search/search/org/eclipse/search/internal/ui/SearchMessages.properties
@@ -99,6 +99,8 @@
SearchPage.fileNamePatterns.text= File name &patterns:
SearchPage.fileNamePatterns.hint= The patterns are separated by comma (* = any string, ? = any character)
SearchPage.caseSensitive= Case sens&itive
+SearchPage.regularExpression= &Regular expression
+SearchPage.regularExpressionSyntaxProblem.title= Search: Invalid Regular Expression
TextSearchEngine.scanning= Scanning file {0} of {1}...
TextSearchEngine.statusMessage= Problems encountered during text search.
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/ui/text/TextSearchPage.java b/org.eclipse.search/search/org/eclipse/search/internal/ui/text/TextSearchPage.java
index d5fe936..08757f5 100644
--- a/org.eclipse.search/search/org/eclipse/search/internal/ui/text/TextSearchPage.java
+++ b/org.eclipse.search/search/org/eclipse/search/internal/ui/text/TextSearchPage.java
@@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
+import java.util.regex.PatternSyntaxException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
@@ -45,6 +46,7 @@
import org.eclipse.jface.dialogs.DialogPage;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableContext;
import org.eclipse.jface.text.ITextSelection;
@@ -79,30 +81,36 @@
// Dialog store id constants
private final static String PAGE_NAME= "TextSearchPage"; //$NON-NLS-1$
private final static String STORE_CASE_SENSITIVE= PAGE_NAME + "CASE_SENSITIVE"; //$NON-NLS-1$
+ private final static String STORE_IS_REG_EX_SEARCH= PAGE_NAME + "REG_EX_SEARCH"; //$NON-NLS-1$
private static List fgPreviousSearchPatterns= new ArrayList(20);
private IDialogSettings fDialogSettings;
private boolean fFirstTime= true;
private boolean fIsCaseSensitive;
+ private boolean fIsRegExSearch;
private Combo fPattern;
private Button fIgnoreCase;
+ private Button fIsRegExCheckbox;
private Combo fExtensions;
+ private Label fHintLabel;
private ISearchPageContainer fContainer;
private FileTypeEditor fFileTypeEditor;
private static class SearchPatternData {
- boolean ignoreCase;
- String textPattern;
- Set fileNamePatterns;
- int scope;
- IWorkingSet[] workingSets;
+ boolean ignoreCase;
+ boolean isRegExSearch;
+ String textPattern;
+ Set fileNamePatterns;
+ int scope;
+ IWorkingSet[] workingSets;
- public SearchPatternData(String textPattern, boolean ignoreCase, Set fileNamePatterns, int scope, IWorkingSet[] workingSets) {
+ public SearchPatternData(String textPattern, boolean ignoreCase, boolean isRegExSearch, Set fileNamePatterns, int scope, IWorkingSet[] workingSets) {
this.ignoreCase= ignoreCase;
+ this.isRegExSearch= isRegExSearch;
this.textPattern= textPattern;
this.fileNamePatterns= fileNamePatterns;
this.scope= scope;
@@ -156,7 +164,10 @@
try {
context.run(true, true, op);
} catch (InvocationTargetException ex) {
- ExceptionHandler.handle(ex, SearchMessages.getString("Search.Error.search.title"),SearchMessages.getString("Search.Error.search.message")); //$NON-NLS-2$ //$NON-NLS-1$
+ if (ex.getTargetException() instanceof PatternSyntaxException)
+ showRegExSyntaxError((PatternSyntaxException)ex.getTargetException());
+ else
+ ExceptionHandler.handle(ex, SearchMessages.getString("Search.Error.search.title"),SearchMessages.getString("Search.Error.search.message")); //$NON-NLS-2$ //$NON-NLS-1$
return false;
} catch (InterruptedException e) {
return false;
@@ -169,6 +180,11 @@
return true;
}
+ private void showRegExSyntaxError(PatternSyntaxException ex) {
+ String title= SearchMessages.getString("SearchPage.regularExpressionSyntaxProblem.title"); //$NON-NLS-1$
+ MessageDialog.openInformation(getShell(), title, ex.getLocalizedMessage());
+ }
+
private String getPattern() {
return fPattern.getText();
}
@@ -189,6 +205,7 @@
};
if (i >= 0) {
match.ignoreCase= ignoreCase();
+ match.isRegExSearch= fIsRegExCheckbox.getSelection();
match.textPattern= getPattern();
match.fileNamePatterns= getExtensions();
match.scope= getContainer().getSelectedScope();
@@ -199,6 +216,7 @@
match= new SearchPatternData(
getPattern(),
ignoreCase(),
+ fIsRegExCheckbox.getSelection(),
getExtensions(),
getContainer().getSelectedScope(),
getContainer().getSelectedWorkingSets());
@@ -230,6 +248,10 @@
StringBuffer result= new StringBuffer();
if (!ignoreCase())
result.append("i"); //$NON-NLS-1$
+
+ if (fIsRegExSearch)
+ result.append("r"); //$NON-NLS-1$
+
return result.toString();
}
@@ -329,10 +351,10 @@
gd= new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
gd.horizontalSpan= 2;
fPattern.setLayoutData(gd);
-
+
fIgnoreCase= new Button(group, SWT.CHECK);
fIgnoreCase.setText(SearchMessages.getString("SearchPage.caseSensitive")); //$NON-NLS-1$
- gd= new GridData(GridData.HORIZONTAL_ALIGN_END);
+ gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
fIgnoreCase.setLayoutData(gd);
fIgnoreCase.setSelection(!fIsCaseSensitive);
fIgnoreCase.addSelectionListener(new SelectionAdapter() {
@@ -343,11 +365,26 @@
});
// Text line which explains the special characters
- label= new Label(group, SWT.LEFT);
- label.setText(SearchMessages.getString("SearchPage.containingText.hint")); //$NON-NLS-1$
+ fHintLabel= new Label(group, SWT.LEFT);
+ fHintLabel.setText(SearchMessages.getString("SearchPage.containingText.hint")); //$NON-NLS-1$
+ fHintLabel.setVisible(!fIsRegExSearch);
gd= new GridData(GridData.BEGINNING);
- gd.horizontalSpan= 3;
- label.setLayoutData(gd);
+ gd.horizontalSpan= 2;
+ fHintLabel.setLayoutData(gd);
+
+ // RegEx checkbox
+ fIsRegExCheckbox= new Button(group, SWT.CHECK);
+ fIsRegExCheckbox.setText(SearchMessages.getString("SearchPage.regularExpression")); //$NON-NLS-1$
+ gd= new GridData(GridData.HORIZONTAL_ALIGN_BEGINNING);
+ fIsRegExCheckbox.setLayoutData(gd);
+ fIsRegExCheckbox.setSelection(fIsRegExSearch);
+ fIsRegExCheckbox.addSelectionListener(new SelectionAdapter() {
+ public void widgetSelected(SelectionEvent e) {
+ fIsRegExSearch= fIsRegExCheckbox.getSelection();
+ fHintLabel.setVisible(!fIsRegExSearch);
+ writeConfiguration();
+ }
+ });
return group;
}
@@ -360,6 +397,8 @@
if (patternData == null || !fPattern.getText().equals(patternData.textPattern))
return;
fIgnoreCase.setSelection(patternData.ignoreCase);
+ fIsRegExCheckbox.setSelection(patternData.isRegExSearch);
+ fHintLabel.setVisible(!patternData.isRegExSearch);
fPattern.setText(patternData.textPattern);
fFileTypeEditor.setFileTypes(patternData.fileNamePatterns);
if (patternData.workingSets != null)
@@ -573,6 +612,7 @@
private void readConfiguration() {
IDialogSettings s= getDialogSettings();
fIsCaseSensitive= s.getBoolean(STORE_CASE_SENSITIVE);
+ fIsRegExSearch= s.getBoolean(STORE_IS_REG_EX_SEARCH);
}
/**
@@ -581,5 +621,6 @@
private void writeConfiguration() {
IDialogSettings s= getDialogSettings();
s.put(STORE_CASE_SENSITIVE, fIsCaseSensitive);
+ s.put(STORE_IS_REG_EX_SEARCH, fIsRegExSearch);
}
}
diff --git a/org.eclipse.search/search/org/eclipse/search/internal/ui/util/StringMatcher.java b/org.eclipse.search/search/org/eclipse/search/internal/ui/util/StringMatcher.java
deleted file mode 100644
index ff6a56d..0000000
--- a/org.eclipse.search/search/org/eclipse/search/internal/ui/util/StringMatcher.java
+++ /dev/null
@@ -1,394 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2000, 2003 IBM Corporation and others.
- * All rights reserved. This program and the accompanying materials
- * are made available under the terms of the Common Public License v1.0
- * which accompanies this distribution, and is available at
- * http://www.eclipse.org/legal/cpl-v10.html
- *
- * Contributors:
- * IBM Corporation - initial API and implementation
- *******************************************************************************/
-package org.eclipse.search.internal.ui.util;
-
-import java.util.*;
-
-/**
- * A string pattern matcher, suppporting * and ? wildcards.
- */
-public class StringMatcher {
- protected String fPattern;
- protected int fLength; // pattern length
- protected boolean fIgnoreWildCards;
- protected boolean fIgnoreCase;
- protected boolean fHasLeadingStar;
- protected boolean fHasTrailingStar;
- protected String fSegments[]; //the given pattern is split into * separated segments
-
- /* boundary value beyond which we don't need to search in the text */
- protected int fBound= 0;
-
-
- protected static final char fSingleWildCard= '\u0000';
-
- public static class Position {
- int start; //inclusive
- int end; //exclusive
- public Position(int start, int end) {
- this.start= start;
- this.end= end;
- }
- public int getStart() {
- return start;
- }
- public int getEnd() {
- return end;
- }
- }
- /**
- * StringMatcher constructor takes in a String object that is a simple
- * pattern which may contain * for 0 and many characters and
- * ? for exactly one character.
- *
- * Literal '*' and '?' characters must be escaped in the pattern
- * e.g., "\*" means literal "*", etc.
- *
- * Escaping any other character (including the escape character itself),
- * just results in that character in the pattern.
- * e.g., "\a" means "a" and "\\" means "\"
- *
- * If invoking the StringMatcher with string literals in Java, don't forget
- * escape characters are represented by "\\".
- *
- * @param pattern the pattern to match text against
- * @param ignoreCase if true, case is ignored
- * @param ignoreWildCards if true, wild cards and their escape sequences are ignored
- * (everything is taken literally).
- */
- public StringMatcher(String pattern, boolean ignoreCase, boolean ignoreWildCards) {
- if (pattern == null)
- throw new IllegalArgumentException();
- fIgnoreCase= ignoreCase;
- fIgnoreWildCards= ignoreWildCards;
- fPattern= pattern;
- fLength= pattern.length();
-
- if (fIgnoreWildCards) {
- parseNoWildCards();
- } else {
- parseWildCards();
- }
- }
- /**
- * Find the first occurrence of the pattern between <code>start</code)(inclusive)
- * and <code>end</code>(exclusive).
- * @param <code>text</code>, the String object to search in
- * @param <code>start</code>, the starting index of the search range, inclusive
- * @param <code>end</code>, the ending index of the search range, exclusive
- * @return an <code>StringMatcher.Position</code> object that keeps the starting
- * (inclusive) and ending positions (exclusive) of the first occurrence of the
- * pattern in the specified range of the text; return null if not found or subtext
- * is empty (start==end). A pair of zeros is returned if pattern is empty string
- * Note that for pattern like "*abc*" with leading and trailing stars, position of "abc"
- * is returned. For a pattern like"*??*" in text "abcdf", (1,3) is returned
- */
- public StringMatcher.Position find(String text, int start, int end) {
- if (text == null)
- throw new IllegalArgumentException();
-
- int tlen= text.length();
- if (start < 0)
- start= 0;
- if (end > tlen)
- end= tlen;
- if (end < 0 ||start >= end )
- return null;
- if (fLength == 0)
- return new Position(start, start);
- if (fIgnoreWildCards) {
- int x= posIn(text, start, end);
- if (x < 0)
- return null;
- return new Position(x, x+fLength);
- }
-
- int segCount= fSegments.length;
- if (segCount == 0)//pattern contains only '*'(s)
- return new Position (start, end);
-
- int curPos= start;
- int matchStart= -1;
- int i;
- for (i= 0; i < segCount && curPos < end; ++i) {
- String current= fSegments[i];
- int nextMatch= regExpPosIn(text, curPos, end, current);
- if (nextMatch < 0 )
- return null;
- if(i == 0)
- matchStart= nextMatch;
- curPos= nextMatch + current.length();
- }
- if (i < segCount)
- return null;
- return new Position(matchStart, curPos);
- }
- /**
- * match the given <code>text</code> with the pattern
- * @return true if matched eitherwise false
- * @param <code>text</code>, a String object
- */
- public boolean match(String text) {
- return match(text, 0, text.length());
- }
- /**
- * Given the starting (inclusive) and the ending (exclusive) positions in the
- * <code>text</code>, determine if the given substring matches with aPattern
- * @return true if the specified portion of the text matches the pattern
- * @param String <code>text</code>, a String object that contains the substring to match
- * @param int <code>start<code> marks the starting position (inclusive) of the substring
- * @param int <code>end<code> marks the ending index (exclusive) of the substring
- */
- public boolean match(String text, int start, int end) {
- if (null == text)
- throw new IllegalArgumentException();
-
- if (start > end)
- return false;
-
- if (fIgnoreWildCards)
- return (end - start == fLength) && fPattern.regionMatches(fIgnoreCase, 0, text, start, fLength);
- int segCount= fSegments.length;
- if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) // pattern contains only '*'(s)
- return true;
- if (start == end)
- return fLength == 0;
- if (fLength == 0)
- return start == end;
-
- int tlen= text.length();
- if (start < 0)
- start= 0;
- if (end > tlen)
- end= tlen;
-
- int tCurPos= start;
- int bound= end - fBound;
- if ( bound < 0)
- return false;
- int i=0;
- String current= fSegments[i];
- int segLength= current.length();
-
- /* process first segment */
- if (!fHasLeadingStar){
- if(!regExpRegionMatches(text, start, current, 0, segLength)) {
- return false;
- } else {
- ++i;
- tCurPos= tCurPos + segLength;
- }
- }
- if ((fSegments.length == 1) && (!fHasLeadingStar) && (!fHasTrailingStar)) {
- // only one segment to match, no wildcards specified
- return tCurPos == end;
- }
- /* process middle segments */
- while (i < segCount) {
- current= fSegments[i];
- int currentMatch;
- int k= current.indexOf(fSingleWildCard);
- if (k < 0) {
- currentMatch= textPosIn(text, tCurPos, end, current);
- if (currentMatch < 0)
- return false;
- } else {
- currentMatch= regExpPosIn(text, tCurPos, end, current);
- if (currentMatch < 0)
- return false;
- }
- tCurPos= currentMatch + current.length();
- i++;
- }
-
- /* process final segment */
- if (!fHasTrailingStar && tCurPos != end) {
- int clen= current.length();
- return regExpRegionMatches(text, end - clen, current, 0, clen);
- }
- return i == segCount ;
- }
-
- /**
- * This method parses the given pattern into segments seperated by wildcard '*' characters.
- * Since wildcards are not being used in this case, the pattern consists of a single segment.
- */
- private void parseNoWildCards() {
- fSegments= new String[1];
- fSegments[0]= fPattern;
- fBound= fLength;
- }
- /**
- * Parses the given pattern into segments seperated by wildcard '*' characters.
- * @param p, a String object that is a simple regular expression with * and/or ?
- */
- private void parseWildCards() {
- if(fPattern.startsWith("*"))//$NON-NLS-1$
- fHasLeadingStar= true;
- if(fPattern.endsWith("*")) {//$NON-NLS-1$
- /* make sure it's not an escaped wildcard */
- if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') {
- fHasTrailingStar= true;
- }
- }
-
- Vector temp= new Vector();
-
- int pos= 0;
- StringBuffer buf= new StringBuffer();
- while (pos < fLength) {
- char c= fPattern.charAt(pos++);
- switch (c) {
- case '\\':
- if (pos >= fLength) {
- buf.append(c);
- } else {
- char next= fPattern.charAt(pos++);
- /* if it's an escape sequence */
- if (next == '*' || next == '?' || next == '\\') {
- buf.append(next);
- } else {
- /* not an escape sequence, just insert literally */
- buf.append(c);
- buf.append(next);
- }
- }
- break;
- case '*':
- if (buf.length() > 0) {
- /* new segment */
- temp.addElement(buf.toString());
- fBound += buf.length();
- buf.setLength(0);
- }
- break;
- case '?':
- /* append special character representing single match wildcard */
- buf.append(fSingleWildCard);
- break;
- default:
- buf.append(c);
- }
- }
-
- /* add last buffer to segment list */
- if (buf.length() > 0) {
- temp.addElement(buf.toString());
- fBound += buf.length();
- }
-
- fSegments= new String[temp.size()];
- temp.copyInto(fSegments);
- }
- /**
- * @param <code>text</code>, a string which contains no wildcard
- * @param <code>start</code>, the starting index in the text for search, inclusive
- * @param <code>end</code>, the stopping point of search, exclusive
- * @return the starting index in the text of the pattern , or -1 if not found
- */
- protected int posIn(String text, int start, int end) {//no wild card in pattern
- int max= end - fLength;
-
- if (!fIgnoreCase) {
- int i= text.indexOf(fPattern, start);
- if (i == -1 || i > max)
- return -1;
- return i;
- }
-
- for (int i= start; i <= max; ++i) {
- if (text.regionMatches(true, i, fPattern, 0, fLength))
- return i;
- }
-
- return -1;
- }
- /**
- * @param <code>text</code>, a simple regular expression that may only contain '?'(s)
- * @param <code>start</code>, the starting index in the text for search, inclusive
- * @param <code>end</code>, the stopping point of search, exclusive
- * @param <code>p</code>, a simple regular expression that may contains '?'
- * @param <code>caseIgnored</code>, wether the pattern is not casesensitive
- * @return the starting index in the text of the pattern , or -1 if not found
- */
- protected int regExpPosIn(String text, int start, int end, String p) {
- int plen= p.length();
-
- int max= end - plen;
- for (int i= start; i <= max; ++i) {
- if (regExpRegionMatches(text, i, p, 0, plen))
- return i;
- }
- return -1;
- }
- /**
- *
- * @return boolean
- * @param <code>text</code>, a String to match
- * @param <code>start</code>, int that indicates the starting index of match, inclusive
- * @param <code>end</code> int that indicates the ending index of match, exclusive
- * @param <code>p</code>, String, String, a simple regular expression that may contain '?'
- * @param <code>ignoreCase</code>, boolean indicating wether code>p</code> is case sensitive
- */
- protected boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) {
- while (plen-- > 0) {
- char tchar= text.charAt(tStart++);
- char pchar= p.charAt(pStart++);
-
- /* process wild cards */
- if (!fIgnoreWildCards) {
- /* skip single wild cards */
- if (pchar == fSingleWildCard) {
- continue;
- }
- }
- if (pchar == tchar)
- continue;
- if (fIgnoreCase) {
- if (Character.toUpperCase(tchar) == Character.toUpperCase(pchar))
- continue;
- // comparing after converting to upper case doesn't handle all cases;
- // also compare after converting to lower case
- if (Character.toLowerCase(tchar) == Character.toLowerCase(pchar))
- continue;
- }
- return false;
- }
- return true;
- }
- /**
- * @param <code>text</code>, the string to match
- * @param <code>start</code>, the starting index in the text for search, inclusive
- * @param <code>end</code>, the stopping point of search, exclusive
- * @param code>p</code>, a string that has no wildcard
- * @param <code>ignoreCase</code>, boolean indicating wether code>p</code> is case sensitive
- * @return the starting index in the text of the pattern , or -1 if not found
- */
- protected int textPosIn(String text, int start, int end, String p) {
-
- int plen= p.length();
- int max= end - plen;
-
- if (!fIgnoreCase) {
- int i= text.indexOf(p, start);
- if (i == -1 || i > max)
- return -1;
- return i;
- }
-
- for (int i= start; i <= max; ++i) {
- if (text.regionMatches(true, i, p, 0, plen))
- return i;
- }
-
- return -1;
- }
-}