diff options
Diffstat (limited to 'build/org.eclipse.cdt.autotools.ui/src/org/eclipse/cdt/internal/autotools/ui/editors/automake/StringMatcher.java')
-rw-r--r-- | build/org.eclipse.cdt.autotools.ui/src/org/eclipse/cdt/internal/autotools/ui/editors/automake/StringMatcher.java | 228 |
1 files changed, 120 insertions, 108 deletions
diff --git a/build/org.eclipse.cdt.autotools.ui/src/org/eclipse/cdt/internal/autotools/ui/editors/automake/StringMatcher.java b/build/org.eclipse.cdt.autotools.ui/src/org/eclipse/cdt/internal/autotools/ui/editors/automake/StringMatcher.java index ba3a32d4d7a..749766412a5 100644 --- a/build/org.eclipse.cdt.autotools.ui/src/org/eclipse/cdt/internal/autotools/ui/editors/automake/StringMatcher.java +++ b/build/org.eclipse.cdt.autotools.ui/src/org/eclipse/cdt/internal/autotools/ui/editors/automake/StringMatcher.java @@ -27,33 +27,36 @@ public class StringMatcher { 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 int fBound = 0; - protected static final char fSingleWildCard= '\u0000'; + 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; + this.start = start; + this.end = end; } + public int getStart() { return start; } + public int getEnd() { return end; } } /** - * Find the first occurrence of the pattern between <code>start</code)(inclusive) - * and <code>end</code>(exclusive). - * @param text the String object to search in + * Find the first occurrence of the pattern between <code>start</code)(inclusive) + * and <code>end</code>(exclusive). + * @param text the String object to search in * @param start the starting index of the search range, inclusive * @param end 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 + * @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" @@ -64,41 +67,42 @@ public class StringMatcher { if (fPattern == null || text == null) throw new IllegalArgumentException(); - int tlen= text.length(); + int tlen = text.length(); if (start < 0) - start= 0; + start = 0; if (end > tlen) - 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); + int x = posIn(text, start, end); if (x < 0) return null; return new Position(x, x + fLength); } - int segCount= fSegments.length; + int segCount = fSegments.length; if (segCount == 0) //pattern contains only '*'(s) return new Position(start, end); - int curPos= start; - int matchStart= -1; - for (int i= 0; i < segCount && curPos < end; ++i) { - String current= fSegments[i]; - int nextMatch= regExpPosIn(text, curPos, end, current); + int curPos = start; + int matchStart = -1; + for (int 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(); + matchStart = nextMatch; + curPos = nextMatch + current.length(); } return new Position(matchStart, curPos); } + /** - * StringMatcher constructor takes in a String object that is a simple + * 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. Also takes as parameter a boolean object * specifying if case should be ignored @@ -108,15 +112,16 @@ public class StringMatcher { public StringMatcher(String aPattern, boolean ignoreCase) { this(aPattern, ignoreCase, false); } + /** - * StringMatcher constructor takes in a String object that is a simple + * 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. + * ? for exactly one character. * - * Literal '*' and '?' characters must be escaped in the pattern + * Literal '*' and '?' characters must be escaped in the pattern * e.g., "\*" means literal "*", etc. * - * Escaping any other character (including the escape character itself), + * Escaping any other character (including the escape character itself), * just results in that character in the pattern. * e.g., "\a" means "a" and "\\" means "\" * @@ -129,15 +134,15 @@ public class StringMatcher { * (everything is taken literally). */ public StringMatcher(String aPattern, boolean ignoreCase, boolean ignoreWildCards) { - fIgnoreCase= ignoreCase; - fIgnoreWildCards= ignoreWildCards; - fLength= aPattern.length(); + fIgnoreCase = ignoreCase; + fIgnoreWildCards = ignoreWildCards; + fLength = aPattern.length(); /* convert case */ if (fIgnoreCase) { - fPattern= aPattern.toUpperCase(); + fPattern = aPattern.toUpperCase(); } else { - fPattern= aPattern; + fPattern = aPattern; } if (fIgnoreWildCards) { @@ -146,13 +151,14 @@ public class StringMatcher { parseWildCards(); } } + /** - * Given the starting (inclusive) and the ending (exclusive) poisitions in the - * <code>text</code>, determine if the given substring matches with aPattern + * Given the starting (inclusive) and the ending (exclusive) poisitions 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 text a String object that contains the substring to match + * @param text a String object that contains the substring to match * @param start marks the starting position (inclusive) of the substring - * @param end marks the ending index (exclusive) of the substring + * @param end marks the ending index (exclusive) of the substring */ public boolean match(String text, int start, int end) { if (null == fPattern || null == text) @@ -163,7 +169,7 @@ public class StringMatcher { if (fIgnoreWildCards) return fPattern.regionMatches(fIgnoreCase, 0, text, start, fLength); - int segCount= fSegments.length; + int segCount = fSegments.length; if (segCount == 0) //pattern contains only '*'(s) or empty pattern return true; if (start == end) @@ -171,19 +177,19 @@ public class StringMatcher { if (fLength == 0) return start == end; - int tlen= text.length(); + int tlen = text.length(); if (start < 0) - start= 0; + start = 0; if (end > tlen) - end= tlen; + end = tlen; - int tCurPos= start; - int bound= end - fBound; + int tCurPos = start; + int bound = end - fBound; if (bound < 0) return false; - int i= 0; - String current= fSegments[i]; - int segLength= current.length(); + int i = 0; + String current = fSegments[i]; + int segLength = current.length(); /* process first segment */ if (!fHasLeadingStar) { @@ -191,99 +197,102 @@ public class StringMatcher { return false; } ++i; - tCurPos= tCurPos + segLength; + tCurPos = tCurPos + segLength; } /* process middle segments */ for (; i < segCount && tCurPos <= bound; ++i) { - current= fSegments[i]; + current = fSegments[i]; int currentMatch; - int k= current.indexOf(fSingleWildCard); + int k = current.indexOf(fSingleWildCard); if (k < 0) { - currentMatch= textPosIn(text, tCurPos, end, current); + currentMatch = textPosIn(text, tCurPos, end, current); if (currentMatch < 0) return false; } else { - currentMatch= regExpPosIn(text, tCurPos, end, current); + currentMatch = regExpPosIn(text, tCurPos, end, current); if (currentMatch < 0) return false; } - tCurPos= currentMatch + current.length(); + tCurPos = currentMatch + current.length(); } /* process final segment */ if (!fHasTrailingStar && tCurPos != end) { - int clen= current.length(); + int clen = current.length(); return regExpRegionMatches(text, end - clen, current, 0, clen); } return i == segCount; } + /** - * match the given <code>text</code> with the pattern + * match the given <code>text</code> with the pattern * @return true if matched eitherwise false - * @param text a String object + * @param text a String object */ public boolean match(String text) { return match(text, 0, text.length()); } + /** * 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; + fSegments = new String[1]; + fSegments[0] = fPattern; + fBound = fLength; } + /** * This method parses the given pattern into segments seperated by wildcard '*' characters. */ private void parseWildCards() { if (fPattern.startsWith("*")) //$NON-NLS-1$ - fHasLeadingStar= true; + 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; + fHasTrailingStar = true; } } Vector<String> temp = new Vector<>(); - int pos= 0; - StringBuilder buf= new StringBuilder(); + int pos = 0; + StringBuilder buf = new StringBuilder(); while (pos < fLength) { - char c= fPattern.charAt(pos++); + char c = fPattern.charAt(pos++); switch (c) { - case '\\' : - if (pos >= fLength) { - buf.append(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 { - 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); + /* not an escape sequence, just insert literally */ + buf.append(c); + buf.append(next); } - break; - case '?' : - /* append special character representing single match wildcard */ - buf.append(fSingleWildCard); - break; - default : - buf.append(c); + } + 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); } } @@ -293,44 +302,46 @@ public class StringMatcher { fBound += buf.length(); } - fSegments= new String[temp.size()]; + fSegments = new String[temp.size()]; temp.copyInto(fSegments); } - /** + + /** * @param text a string which contains no wildcard * @param start the starting index in the text for search, inclusive * @param end the stopping point of search, exclusive - * @return the starting index in the text of the pattern , or -1 if not found + * @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; + int max = end - fLength; if (!fIgnoreCase) { - int i= text.indexOf(fPattern, start); + int i = text.indexOf(fPattern, start); if (i == -1 || i > max) return -1; return i; } - for (int i= start; i <= max; ++i) { + for (int i = start; i <= max; ++i) { if (text.regionMatches(true, i, fPattern, 0, fLength)) return i; } return -1; } - /** + + /** * @param text a simple regular expression that may only contain '?'(s) * @param start the starting index in the text for search, inclusive * @param end the stopping point of search, exclusive * @param p a simple regular expression that may contains '?' - * @return the starting index in the text of the pattern , or -1 if not found + * @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 plen = p.length(); - int max= end - plen; - for (int i= start; i <= max; ++i) { + int max = end - plen; + for (int i = start; i <= max; ++i) { if (regExpRegionMatches(text, i, p, 0, plen)) return i; } @@ -339,8 +350,8 @@ public class StringMatcher { 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++); + char tchar = text.charAt(tStart++); + char pchar = p.charAt(pStart++); /* process wild cards */ if (!fIgnoreWildCards) { @@ -352,7 +363,7 @@ public class StringMatcher { if (pchar == tchar) continue; if (fIgnoreCase) { - char tc= Character.toUpperCase(tchar); + char tc = Character.toUpperCase(tchar); if (tc == pchar) continue; } @@ -360,26 +371,27 @@ public class StringMatcher { } return true; } - /** + + /** * @param text the string to match * @param start the starting index in the text for search, inclusive * @param end the stopping point of search, exclusive * @param p a string that has no wildcard - * @return the starting index in the text of the pattern , or -1 if not found + * @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; + int plen = p.length(); + int max = end - plen; if (!fIgnoreCase) { - int i= text.indexOf(p, start); + int i = text.indexOf(p, start); if (i == -1 || i > max) return -1; return i; } - for (int i= 0; i <= max; ++i) { + for (int i = 0; i <= max; ++i) { if (text.regionMatches(true, i, p, 0, plen)) return i; } |