mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 22:52:11 +02:00
Bug 450057 - Open Element fails to find function for certain wildcard patterns
This commit is contained in:
parent
48a407097e
commit
24cb9ee547
1 changed files with 120 additions and 127 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2000, 2008 IBM Corporation and others.
|
* Copyright (c) 2000, 2014 IBM Corporation and others.
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -26,6 +26,7 @@ public class StringMatcher {
|
||||||
/* boundary value beyond which we don't need to search in the text */
|
/* 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 {
|
public static class Position {
|
||||||
|
@ -42,104 +43,33 @@ public class StringMatcher {
|
||||||
return end;
|
return end;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find the first occurrence of the pattern between <code>start</code)(inclusive)
|
* StringMatcher constructor takes in a String object that is a simple
|
||||||
* and <code>end</code>(exclusive).
|
* pattern. The pattern may contain '*' for 0 and many characters and
|
||||||
* @param text the String object to search in
|
* '?' for exactly one character.
|
||||||
* @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
|
|
||||||
* 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 (fPattern == null || 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;
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
return new Position(matchStart, curPos);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 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
|
|
||||||
* @deprecated Use StringMatcher(pattern, ignoreCase, ignoreWildCards).
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public StringMatcher(String aPattern, boolean ignoreCase) {
|
|
||||||
this(aPattern, ignoreCase, false);
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* 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
|
* Literal '*' and '?' characters must be escaped in the pattern
|
||||||
* e.g., "\*" means literal "*", etc.
|
* 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.
|
* just results in that character in the pattern.
|
||||||
* e.g., "\a" means "a" and "\\" means "\"
|
* e.g., "\a" means "a" and "\\" means "\"
|
||||||
*
|
*
|
||||||
* If invoking the StringMatcher with string literals in Java, don't forget
|
* If invoking the StringMatcher with string literals in Java, don't forget
|
||||||
* escape characters are represented by "\\".
|
* escape characters are represented by "\\".
|
||||||
*
|
*
|
||||||
* @param aPattern the pattern to match text against
|
* @param pattern the pattern to match text against
|
||||||
* @param ignoreCase if true, case is ignored
|
* @param ignoreCase if true, case is ignored
|
||||||
* @param ignoreWildCards if true, wild cards and their escape sequences are ignored
|
* @param ignoreWildCards if true, wild cards and their escape sequences are ignored
|
||||||
* (everything is taken literally).
|
* (everything is taken literally).
|
||||||
*/
|
*/
|
||||||
public StringMatcher(String aPattern, boolean ignoreCase, boolean ignoreWildCards) {
|
public StringMatcher(String pattern, boolean ignoreCase, boolean ignoreWildCards) {
|
||||||
|
if (pattern == null)
|
||||||
|
throw new IllegalArgumentException();
|
||||||
fIgnoreCase= ignoreCase;
|
fIgnoreCase= ignoreCase;
|
||||||
fIgnoreWildCards= ignoreWildCards;
|
fIgnoreWildCards= ignoreWildCards;
|
||||||
fLength= aPattern.length();
|
fPattern= pattern;
|
||||||
|
fLength= pattern.length();
|
||||||
/* convert case */
|
|
||||||
if (fIgnoreCase) {
|
|
||||||
char[] chars= aPattern.toCharArray();
|
|
||||||
for (int i = 0; i < chars.length; i++) {
|
|
||||||
chars[i]= Character.toUpperCase(chars[i]);
|
|
||||||
}
|
|
||||||
fPattern= new String(chars);
|
|
||||||
} else {
|
|
||||||
fPattern= aPattern;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (fIgnoreWildCards) {
|
if (fIgnoreWildCards) {
|
||||||
parseNoWildCards();
|
parseNoWildCards();
|
||||||
|
@ -148,24 +78,85 @@ public class StringMatcher {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Given the starting (inclusive) and the ending (exclusive) poisitions in the
|
* Find the first occurrence of the pattern between <code>start</code)(inclusive)
|
||||||
* <code>text</code>, determine if the given substring matches with aPattern
|
* 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
|
||||||
|
* 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 text 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
|
* @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 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) {
|
public boolean match(String text, int start, int end) {
|
||||||
if (null == fPattern || null == text)
|
if (null == text)
|
||||||
throw new IllegalArgumentException();
|
throw new IllegalArgumentException();
|
||||||
|
|
||||||
if (start > end)
|
if (start > end)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (fIgnoreWildCards)
|
if (fIgnoreWildCards)
|
||||||
return fPattern.regionMatches(fIgnoreCase, 0, text, start, fLength);
|
return (end - start == fLength) && 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
|
if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) // pattern contains only '*'(s)
|
||||||
return true;
|
return true;
|
||||||
if (start == end)
|
if (start == end)
|
||||||
return fLength == 0;
|
return fLength == 0;
|
||||||
|
@ -180,23 +171,27 @@ public class StringMatcher {
|
||||||
|
|
||||||
int tCurPos= start;
|
int tCurPos= start;
|
||||||
int bound= end - fBound;
|
int bound= end - fBound;
|
||||||
if (bound < 0)
|
if ( bound < 0)
|
||||||
return false;
|
return false;
|
||||||
int i= 0;
|
int i=0;
|
||||||
String current= fSegments[i];
|
String current= fSegments[i];
|
||||||
int segLength= current.length();
|
int segLength= current.length();
|
||||||
|
|
||||||
/* process first segment */
|
/* process first segment */
|
||||||
if (!fHasLeadingStar) {
|
if (!fHasLeadingStar){
|
||||||
if (!regExpRegionMatches(text, start, current, 0, segLength)) {
|
if(!regExpRegionMatches(text, start, current, 0, segLength)) {
|
||||||
return false;
|
return false;
|
||||||
|
} else {
|
||||||
|
++i;
|
||||||
|
tCurPos= tCurPos + segLength;
|
||||||
}
|
}
|
||||||
++i;
|
|
||||||
tCurPos= tCurPos + segLength;
|
|
||||||
}
|
}
|
||||||
|
if ((fSegments.length == 1) && (!fHasLeadingStar) && (!fHasTrailingStar)) {
|
||||||
|
// only one segment to match, no wildcards specified
|
||||||
|
return tCurPos == end;
|
||||||
|
}
|
||||||
/* process middle segments */
|
/* process middle segments */
|
||||||
for (; i < segCount && tCurPos <= bound; ++i) {
|
while (i < segCount) {
|
||||||
current= fSegments[i];
|
current= fSegments[i];
|
||||||
int currentMatch;
|
int currentMatch;
|
||||||
int k= current.indexOf(fSingleWildCard);
|
int k= current.indexOf(fSingleWildCard);
|
||||||
|
@ -210,6 +205,7 @@ public class StringMatcher {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
tCurPos= currentMatch + current.length();
|
tCurPos= currentMatch + current.length();
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* process final segment */
|
/* process final segment */
|
||||||
|
@ -217,16 +213,9 @@ public class StringMatcher {
|
||||||
int clen= current.length();
|
int clen= current.length();
|
||||||
return regExpRegionMatches(text, end - clen, current, 0, clen);
|
return regExpRegionMatches(text, end - clen, current, 0, clen);
|
||||||
}
|
}
|
||||||
return i == segCount;
|
return i == segCount ;
|
||||||
}
|
|
||||||
/**
|
|
||||||
* match the given <code>text</code> with the pattern
|
|
||||||
* @return true if matched eitherwise false
|
|
||||||
* @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.
|
* 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.
|
* Since wildcards are not being used in this case, the pattern consists of a single segment.
|
||||||
|
@ -237,12 +226,12 @@ public class StringMatcher {
|
||||||
fBound= fLength;
|
fBound= fLength;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* This method parses the given pattern into segments seperated by wildcard '*' characters.
|
* Parses the given pattern into segments seperated by wildcard '*' characters.
|
||||||
*/
|
*/
|
||||||
private void parseWildCards() {
|
private void parseWildCards() {
|
||||||
if (fPattern.startsWith("*")) //$NON-NLS-1$
|
if(fPattern.startsWith("*"))//$NON-NLS-1$
|
||||||
fHasLeadingStar= true;
|
fHasLeadingStar= true;
|
||||||
if (fPattern.endsWith("*")) { //$NON-NLS-1$
|
if(fPattern.endsWith("*")) {//$NON-NLS-1$
|
||||||
/* make sure it's not an escaped wildcard */
|
/* make sure it's not an escaped wildcard */
|
||||||
if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') {
|
if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') {
|
||||||
fHasTrailingStar= true;
|
fHasTrailingStar= true;
|
||||||
|
@ -256,7 +245,7 @@ public class StringMatcher {
|
||||||
while (pos < fLength) {
|
while (pos < fLength) {
|
||||||
char c= fPattern.charAt(pos++);
|
char c= fPattern.charAt(pos++);
|
||||||
switch (c) {
|
switch (c) {
|
||||||
case '\\' :
|
case '\\':
|
||||||
if (pos >= fLength) {
|
if (pos >= fLength) {
|
||||||
buf.append(c);
|
buf.append(c);
|
||||||
} else {
|
} else {
|
||||||
|
@ -270,20 +259,20 @@ public class StringMatcher {
|
||||||
buf.append(next);
|
buf.append(next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '*' :
|
case '*':
|
||||||
if (buf.length() > 0) {
|
if (buf.length() > 0) {
|
||||||
/* new segment */
|
/* new segment */
|
||||||
temp.addElement(buf.toString());
|
temp.addElement(buf.toString());
|
||||||
fBound += buf.length();
|
fBound += buf.length();
|
||||||
buf.setLength(0);
|
buf.setLength(0);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case '?' :
|
case '?':
|
||||||
/* append special character representing single match wildcard */
|
/* append special character representing single match wildcard */
|
||||||
buf.append(fSingleWildCard);
|
buf.append(fSingleWildCard);
|
||||||
break;
|
break;
|
||||||
default :
|
default:
|
||||||
buf.append(c);
|
buf.append(c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -297,13 +286,13 @@ public class StringMatcher {
|
||||||
fSegments= new String[temp.size()];
|
fSegments= new String[temp.size()];
|
||||||
temp.copyInto(fSegments);
|
temp.copyInto(fSegments);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param text a string which contains no wildcard
|
* @param text a string which contains no wildcard
|
||||||
* @param start the starting index in the text for search, inclusive
|
* @param start the starting index in the text for search, inclusive
|
||||||
* @param end the stopping point of search, exclusive
|
* @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
|
protected int posIn(String text, int start, int end) {//no wild card in pattern
|
||||||
int max= end - fLength;
|
int max= end - fLength;
|
||||||
|
|
||||||
if (!fIgnoreCase) {
|
if (!fIgnoreCase) {
|
||||||
|
@ -320,12 +309,12 @@ public class StringMatcher {
|
||||||
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param text a simple regular expression that may only contain '?'(s)
|
* @param text a simple regular expression that may only contain '?'(s)
|
||||||
* @param start the starting index in the text for search, inclusive
|
* @param start the starting index in the text for search, inclusive
|
||||||
* @param end the stopping point of search, exclusive
|
* @param end the stopping point of search, exclusive
|
||||||
* @param p a simple regular expression that may contains '?'
|
* @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) {
|
protected int regExpPosIn(String text, int start, int end, String p) {
|
||||||
int plen= p.length();
|
int plen= p.length();
|
||||||
|
@ -338,6 +327,7 @@ public class StringMatcher {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) {
|
protected boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) {
|
||||||
while (plen-- > 0) {
|
while (plen-- > 0) {
|
||||||
char tchar= text.charAt(tStart++);
|
char tchar= text.charAt(tStart++);
|
||||||
|
@ -353,20 +343,23 @@ public class StringMatcher {
|
||||||
if (pchar == tchar)
|
if (pchar == tchar)
|
||||||
continue;
|
continue;
|
||||||
if (fIgnoreCase) {
|
if (fIgnoreCase) {
|
||||||
char tc= Character.toUpperCase(tchar);
|
if (Character.toUpperCase(tchar) == Character.toUpperCase(pchar))
|
||||||
if (tc == 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;
|
continue;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @param text the string to match
|
* @param text the string to match
|
||||||
* @param start the starting index in the text for search, inclusive
|
* @param start the starting index in the text for search, inclusive
|
||||||
* @param end the stopping point of search, exclusive
|
* @param end the stopping point of search, exclusive
|
||||||
* @param p a string that has no wildcard
|
* @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) {
|
protected int textPosIn(String text, int start, int end, String p) {
|
||||||
|
|
||||||
|
@ -380,7 +373,7 @@ public class StringMatcher {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i= 0; i <= max; ++i) {
|
for (int i= start; i <= max; ++i) {
|
||||||
if (text.regionMatches(true, i, p, 0, plen))
|
if (text.regionMatches(true, i, p, 0, plen))
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue