bayard 2004/09/21 21:12:18
Modified: io/src/java/org/apache/commons/io/find WildcardUtils.java
io/src/test/org/apache/commons/io/find
WildcardUtilsTest.java
Log:
More tests for WildcardUtils.match and a new implementation of the match method.
Mostly the same as Bugzilla #31115, though the old tests were kept and they
showed a bug.
Submitted by: Jason Anderson
Revision Changes Path
1.2 +50 -27
jakarta-commons/io/src/java/org/apache/commons/io/find/WildcardUtils.java
Index: WildcardUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/find/WildcardUtils.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WildcardUtils.java 17 Sep 2004 00:14:48 -0000 1.1
+++ WildcardUtils.java 22 Sep 2004 04:12:18 -0000 1.2
@@ -28,42 +28,65 @@
* matches to a specified wildcard.
*/
public static boolean match(String text, String wildcard) {
- // split wildcard on ? and *
- // for each element of the array, find a matching block in text
- // earliest matching block counts
String[] wcs = splitOnTokens(wildcard);
+
int textIdx = 0;
- for(int i=0; i<wcs.length; i++) {
- if(textIdx == text.length()) {
- if("*".equals(wcs[i])) {
- return true;
- }
- return wcs[i].length() == 0;
- }
-
- if("?".equals(wcs[i])) {
+ int wcsIdx = 0;
+ boolean anyChars = false;
+
+ // loop whilst tokens and text left to process
+ while (wcsIdx < wcs.length && textIdx < text.length()) {
+
+ // ? so move to next text char
+ if (wcs[wcsIdx].equals("?")) {
textIdx++;
} else
- if("*".equals(wcs[i])) {
- int nextIdx = i+1;
- if(nextIdx == wcs.length) {
- return true;
- }
- int restartIdx = text.indexOf(wcs[nextIdx], textIdx);
- if(restartIdx == -1) {
- return false;
- } else {
- textIdx = restartIdx;
- }
- } else {
- if(!text.startsWith(wcs[i], textIdx)) {
- return false;
+ if (!wcs[wcsIdx].equals("*")) {
+ // matching text token
+ if (anyChars) {
+ // any chars then try to locate text token
+ textIdx = text.indexOf(wcs[wcsIdx], textIdx);
+
+ if (textIdx == -1) {
+ // token not found
+ return false;
+ }
} else {
- textIdx += wcs[i].length();
+ // matching from current position
+ if (!text.startsWith(wcs[wcsIdx], textIdx)) {
+ // couldnt match token
+ return false;
+ }
}
+
+ // matched text token, move text index to end of matched token
+ textIdx += wcs[wcsIdx].length();
}
+
+ // set any chars status
+ anyChars = wcs[wcsIdx].equals("*");
+
+ wcsIdx++;
}
+ // didnt match all wildcards
+ if (wcsIdx < wcs.length) {
+ // ok if one remaining and wildcard or empty
+ if (wcsIdx + 1 != wcs.length || !(wcs[wcsIdx].equals("*") ||
wcs[wcsIdx].equals("")) ) {
+ return false;
+ }
+ }
+
+ // ran out of text chars
+ if (textIdx > text.length()) {
+ return false;
+ }
+
+ // didnt match all text chars, only ok if any chars set
+ if (textIdx < text.length() && !anyChars) {
+ return false;
+ }
+
return true;
}
1.2 +33 -0
jakarta-commons/io/src/test/org/apache/commons/io/find/WildcardUtilsTest.java
Index: WildcardUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/find/WildcardUtilsTest.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- WildcardUtilsTest.java 17 Sep 2004 00:14:49 -0000 1.1
+++ WildcardUtilsTest.java 22 Sep 2004 04:12:18 -0000 1.2
@@ -62,4 +62,37 @@
}
}
+ private void assertMatch(String text, String wildcard, boolean expected) {
+ assertEquals(text + " " + wildcard, expected, WildcardUtils.match(text,
wildcard));
+ }
+
+ // A separate set of tests, added to this batch
+ public void testMatch2() {
+ assertMatch("log.txt", "log.txt", true);
+ assertMatch("log.txt1", "log.txt", false);
+
+ assertMatch("log.txt", "log.txt*", true);
+ assertMatch("log.txt", "log.txt*1", false);
+ assertMatch("log.txt", "*log.txt*", true);
+
+ assertMatch("log.txt", "*.txt", true);
+ assertMatch("txt.log", "*.txt", false);
+ assertMatch("config.ini", "*.ini", true);
+
+ assertMatch("config.txt.bak", "con*.txt", false);
+
+ assertMatch("log.txt9", "*.txt?", true);
+ assertMatch("log.txt", "*.txt?", false);
+
+ assertMatch("progtestcase.java~5~", "*test*.java~*~", true);
+ assertMatch("progtestcase.java;5~", "*test*.java~*~", false);
+ assertMatch("progtestcase.java~5", "*test*.java~*~", false);
+
+ assertMatch("log.txt", "log.*", true);
+
+ assertMatch("log.txt", "log?*", true);
+
+ assertMatch("log.txt12", "log.txt??", true);
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]