Skip to content

Commit 2f4496c

Browse files
committed
improvement to the extension handling from Oliver Jones
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140581 13f79535-47bb-0310-9956-ffa450edef68
1 parent e63fe54 commit 2f4496c

2 files changed

Lines changed: 53 additions & 32 deletions

File tree

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

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
* @author <a href="mailto:jefft@apache.org">Jeff Turner</a>
5353
* @author Matthew Hawthorne
5454
* @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
55-
* @version $Id: FilenameUtils.java,v 1.9 2004/06/13 04:58:07 bayard Exp $
55+
* @version $Id: FilenameUtils.java,v 1.10 2004/06/13 05:13:57 bayard Exp $
5656
*/
5757
public class FilenameUtils {
5858

@@ -103,41 +103,54 @@ public static void mkdir(String dir) {
103103
* foo.txt --> foo
104104
* a\b\c.jpg --> a\b\c
105105
* a\b\c --> a\b\c
106+
* a.b\c --> a.b\c
106107
* </pre>
107108
*
108109
* @param filename the filename
109110
* @return the filename minus extension
110111
*/
111-
public static String removeExtension( String filename) {
112-
int index = filename.lastIndexOf('.');
113-
114-
if (-1 == index) {
115-
return filename;
116-
} else {
117-
return filename.substring(0, index);
112+
public static String removeExtension( final String filename) {
113+
String ext = getExtension(filename);
114+
int index = ext.length();
115+
if(index > 0) {
116+
// include the . in the count
117+
index++;
118118
}
119+
index = filename.length() - index;
120+
return filename.substring(0, index);
119121
}
120122

121-
/**
122-
* Get extension from filename.
123-
* ie
123+
/**
124+
* Gets the extension of a filename.
125+
* <p>
126+
* eg
124127
* <pre>
125-
* foo.txt --> "txt"
126-
* a\b\c.jpg --> "jpg"
127-
* a\b\c --> ""
128+
* foo.txt --> "txt"
129+
* a/b/c.jpg --> "jpg"
130+
* a/b/c --> ""
131+
* a.b/c.txt --> "txt"
132+
* a.b/c --> ""
128133
* </pre>
129134
*
130-
* @param filename the filename
131-
* @return the extension of filename or "" if none
135+
* @param filename the filename to retrieve the extension of.
136+
* @return the extension of filename or an empty string if none exists.
132137
*/
133-
public static String getExtension( String filename) {
134-
int index = filename.lastIndexOf('.');
138+
public static String getExtension(final String filename) {
139+
String suffix = "";
140+
String shortFilename = filename;
135141

136-
if (-1 == index) {
137-
return "";
138-
} else {
139-
return filename.substring(index + 1);
142+
int lastDirSeparator = filename.lastIndexOf(File.separatorChar);
143+
if(lastDirSeparator > 0){
144+
shortFilename = filename.substring(lastDirSeparator + 1);
145+
}
146+
147+
int index = shortFilename.lastIndexOf('.');
148+
149+
if (index > 0 && index < shortFilename.length() - 1) {
150+
suffix = shortFilename.substring(index + 1);
140151
}
152+
153+
return suffix;
141154
}
142155

143156
/**

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

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*
3030
* @author Peter Donald
3131
* @author Matthew Hawthorne
32-
* @version $Id: FilenameUtilsTestCase.java,v 1.7 2004/03/12 21:59:19 jeremias Exp $
32+
* @version $Id: FilenameUtilsTestCase.java,v 1.8 2004/06/13 05:13:57 bayard Exp $
3333
* @see FilenameUtils
3434
*/
3535
public class FilenameUtilsTestCase extends FileBasedTestCase {
@@ -203,10 +203,14 @@ private String replaceAll(
203203
}
204204

205205
public void testGetExtension() {
206-
String[][] tests = { { "filename.ext", "ext" }, {
207-
"README", "" }, {
208-
"domain.dot.com", "com" }, {
209-
"image.jpeg", "jpeg" }
206+
String[][] tests = {
207+
{ "filename.ext", "ext" },
208+
{ "README", "" },
209+
{ "domain.dot.com", "com" },
210+
{ "image.jpeg", "jpeg" },
211+
{ "a.b/c", "" },
212+
{ "a.b/c.txt", "txt" },
213+
{ "a/b/c", "" },
210214
};
211215
for (int i = 0; i < tests.length; i++) {
212216
assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
@@ -234,11 +238,15 @@ public void DISABLED__testGetExtensionWithPaths() {
234238
}
235239

236240
public void testRemoveExtension() {
237-
String[][] tests = { { "filename.ext", "filename" }, {
238-
"first.second.third.ext", "first.second.third" }, {
239-
"README", "README" }, {
240-
"domain.dot.com", "domain.dot" }, {
241-
"image.jpeg", "image" }
241+
String[][] tests = {
242+
{ "filename.ext", "filename" },
243+
{ "first.second.third.ext", "first.second.third" },
244+
{ "README", "README" },
245+
{ "domain.dot.com", "domain.dot" },
246+
{ "image.jpeg", "image" },
247+
{ "a.b/c", "a.b/c" },
248+
{ "a.b/c.txt", "a.b/c" },
249+
{ "a/b/c", "a/b/c" },
242250
};
243251

244252
for (int i = 0; i < tests.length; i++) {

0 commit comments

Comments
 (0)