Skip to content

Commit 1336781

Browse files
committed
IO-375 FilenameUtils.splitOnTokens(String text) check for '**' could be simplified
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1465505 13f79535-47bb-0310-9956-ffa450edef68
1 parent 2009a2c commit 1336781

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

src/changes/changes.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ The <action> type attribute can be add,update,fix,remove.
4747
<body>
4848
<!-- The release date is the date RC is cut -->
4949
<release version="2.5" date="2013-??-??" description="New features and bug fixes.">
50-
<action issue="IO-374" dev="sebb" type="fix">
50+
<action issue="IO-375" dev="sebb" type="update">
51+
FilenameUtils.splitOnTokens(String text) check for '**' could be simplified
52+
</action>
53+
<action issue="IO-374" dev="sebb" type="update">
5154
WildcardFileFilter ctors should not use null to mean IOCase.SENSITIVE when delegating to other ctors
5255
</action>
5356
<action issue="IO-362" dev="ggregory" type="fix" due-to="mmadson, ggregory">

src/main/java/org/apache/commons/io/FilenameUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1375,6 +1375,7 @@ static String[] splitOnTokens(final String text) {
13751375
final char[] array = text.toCharArray();
13761376
final ArrayList<String> list = new ArrayList<String>();
13771377
final StringBuilder buffer = new StringBuilder();
1378+
char prevChar = 0;
13781379
for (int i = 0; i < array.length; i++) {
13791380
final char ch = array[i];
13801381
if (ch == '?' || ch == '*') {
@@ -1384,13 +1385,13 @@ static String[] splitOnTokens(final String text) {
13841385
}
13851386
if (ch == '?') {
13861387
list.add("?");
1387-
} else if (list.isEmpty() || // ch == '*' here; check if previous char was '*'
1388-
i > 0 && list.get(list.size() - 1).equals("*") == false) {
1388+
} else if (prevChar != '*' ) {// ch == '*' here; check if previous char was '*'
13891389
list.add("*");
13901390
}
13911391
} else {
13921392
buffer.append(ch);
13931393
}
1394+
prevChar = ch;
13941395
}
13951396
if (buffer.length() != 0) {
13961397
list.add(buffer.toString());

src/test/java/org/apache/commons/io/FilenameUtilsWildcardTestCase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ public void testSplitOnTokens() {
123123
assertArrayEquals( new String[] { "*" }, FilenameUtils.splitOnTokens("****") );
124124
assertArrayEquals( new String[] { "*", "?", "?", "*" }, FilenameUtils.splitOnTokens("*??*") );
125125
assertArrayEquals( new String[] { "*", "?", "*", "?", "*" }, FilenameUtils.splitOnTokens("*?**?*") );
126+
assertArrayEquals( new String[] { "*", "?", "*", "?", "*" }, FilenameUtils.splitOnTokens("*?***?*") );
126127
assertArrayEquals( new String[] { "h", "?", "?", "*" }, FilenameUtils.splitOnTokens("h??*") );
127128
assertArrayEquals( new String[] { "" }, FilenameUtils.splitOnTokens("") );
128129
}

0 commit comments

Comments
 (0)