commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bay...@apache.org
Subject cvs commit: jakarta-commons/io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
Date Sun, 13 Jun 2004 05:13:57 GMT
bayard      2004/06/12 22:13:57

  Modified:    io/src/java/org/apache/commons/io FilenameUtils.java
               io/src/test/org/apache/commons/io FilenameUtilsTestCase.java
  Log:
  improvement to the extension handling from Oliver Jones
  
  Revision  Changes    Path
  1.10      +35 -22    jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java
  
  Index: FilenameUtils.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/java/org/apache/commons/io/FilenameUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- FilenameUtils.java	13 Jun 2004 04:58:07 -0000	1.9
  +++ FilenameUtils.java	13 Jun 2004 05:13:57 -0000	1.10
  @@ -103,41 +103,54 @@
        * foo.txt    --> foo
        * a\b\c.jpg --> a\b\c
        * a\b\c     --> a\b\c
  +     * a.b\c        --> a.b\c
        * </pre>
        *
        * @param filename the filename
        * @return the filename minus extension
        */
  -    public static String removeExtension( String filename) {
  -        int index = filename.lastIndexOf('.');
  -
  -        if (-1 == index) {
  -            return filename;
  -        } else {
  -            return filename.substring(0, index);
  +    public static String removeExtension( final String filename) {
  +        String ext = getExtension(filename);
  +        int index = ext.length();
  +        if(index > 0) {
  +            // include the . in the count
  +            index++;
           }
  +        index = filename.length() - index;
  +        return filename.substring(0, index);
       }
   
  -    /**
  -     * Get extension from filename.
  -     * ie
  +   /**
  +     * Gets the extension of a filename.
  +     * <p>
  +     * eg
        * <pre>
  -     * foo.txt    --> "txt"
  -     * a\b\c.jpg --> "jpg"
  -     * a\b\c     --> ""
  +     * foo.txt      --> "txt"
  +     * a/b/c.jpg    --> "jpg"
  +     * a/b/c        --> ""
  +     * a.b/c.txt    --> "txt"
  +     * a.b/c        --> ""
        * </pre>
        *
  -     * @param filename the filename
  -     * @return the extension of filename or "" if none
  +     * @param filename the filename to retrieve the extension of.
  +     * @return the extension of filename or an empty string if none exists.
        */
  -    public static String getExtension( String filename) {
  -        int index = filename.lastIndexOf('.');
  +    public static String getExtension(final String filename) {
  +        String suffix = "";
  +        String shortFilename = filename;
  +
  +        int lastDirSeparator = filename.lastIndexOf(File.separatorChar);
  +        if(lastDirSeparator > 0){
  +            shortFilename = filename.substring(lastDirSeparator + 1);
  +        }
  +
  +        int index = shortFilename.lastIndexOf('.');
   
  -        if (-1 == index) {
  -            return "";
  -        } else {
  -            return filename.substring(index + 1);
  +        if (index > 0 && index < shortFilename.length() - 1) {
  +            suffix = shortFilename.substring(index + 1);
           }
  +
  +        return suffix;
       }
   
       /**
  
  
  
  1.8       +18 -10    jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsTestCase.java
  
  Index: FilenameUtilsTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/io/src/test/org/apache/commons/io/FilenameUtilsTestCase.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- FilenameUtilsTestCase.java	12 Mar 2004 21:59:19 -0000	1.7
  +++ FilenameUtilsTestCase.java	13 Jun 2004 05:13:57 -0000	1.8
  @@ -203,10 +203,14 @@
       }
   
       public void testGetExtension() {
  -        String[][] tests = { { "filename.ext", "ext" }, {
  -                "README", "" }, {
  -                "domain.dot.com", "com" }, {
  -                "image.jpeg", "jpeg" }
  +        String[][] tests = {
  +                { "filename.ext", "ext" }, 
  +                { "README", "" }, 
  +                { "domain.dot.com", "com" }, 
  +                { "image.jpeg", "jpeg" },
  +                { "a.b/c", "" },
  +                { "a.b/c.txt", "txt" },
  +                { "a/b/c", "" },
           };
           for (int i = 0; i < tests.length; i++) {
               assertEquals(tests[i][1], FilenameUtils.getExtension(tests[i][0]));
  @@ -234,11 +238,15 @@
       }
   
       public void testRemoveExtension() {
  -        String[][] tests = { { "filename.ext", "filename" }, {
  -                "first.second.third.ext", "first.second.third" }, {
  -                "README", "README" }, {
  -                "domain.dot.com", "domain.dot" }, {
  -                "image.jpeg", "image" }
  +        String[][] tests = { 
  +                { "filename.ext", "filename" }, 
  +                { "first.second.third.ext", "first.second.third" }, 
  +                { "README", "README" }, 
  +                { "domain.dot.com", "domain.dot" }, 
  +                { "image.jpeg", "image" },
  +                { "a.b/c", "a.b/c" },
  +                { "a.b/c.txt", "a.b/c" },
  +                { "a/b/c", "a/b/c" },
           };
   
           for (int i = 0; i < tests.length; i++) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message