[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
proposed fileparts patch
From: |
Hall, Benjamin |
Subject: |
proposed fileparts patch |
Date: |
Thu, 23 Oct 2008 14:41:55 -0400 |
Matlab accepts both "/" and "\" as valid path separators on windows. The
proposed patch below would update fileparts to behave consistently with matlab
and correctly identify paths with mixed file separators when used in windows.
*** fileparts.orig.m Thu Oct 23 14:34:47 2008
--- fileparts.m Thu Oct 23 14:37:42 2008
*************** function [directory, name, extension, ve
*** 27,33 ****
if (nargin == 1)
if (ischar (filename))
! ds = rindex (filename, filesep);
es = rindex (filename, ".");
## These can be the same if they are both 0 (no dir or ext).
if (es <= ds)
--- 27,44 ----
if (nargin == 1)
if (ischar (filename))
! if ( ispc )
! ds = rindex (strrep (filename,"/",filesep), filesep);
! if ( ds < 1 )
! ds = index (filename, ":");
! if ( ds > 0 )
! filename = strrep (filename, ":", [":",filesep] );
! ds++;
! endif
! endif
! else
! ds = rindex (filename, filesep);
! endif
es = rindex (filename, ".");
## These can be the same if they are both 0 (no dir or ext).
if (es <= ds)
*************** function [directory, name, extension, ve
*** 49,51 ****
--- 60,108 ----
endif
endfunction
+
+ %Test cases: an ugly mix of file separators for dealing with
+ % "common" windows pathnames. The platform-dependent
+ % answers are common to the competition
+ %!test
+ %! [a,b,c]=fileparts('u:file.ext');
+ %! if ( ispc )
+ %! assert(a,'u:');
+ %! assert(b,'file');
+ %! assert(c,'.ext');
+ %! else
+ %! assert(isempty(a),true);
+ %! assert(b,'u:file');
+ %! assert(c,'.ext');
+ %! endif
+ %!test
+ %! [a,b,c]=fileparts('u:\path\to/my/special\place/file.ext');
+ %! assert(a,'u:\path\to/my/special\place');
+ %! assert(b,'file');
+ %! assert(c,'.ext');
+ %!test
+ %! [a,b,c]=fileparts('u:/file.ext');
+ %! assert(a,'u:');
+ %! assert(b,'file');
+ %! assert(c,'.ext');
+ %!test
+ %! [a,b,c]=fileparts('/home/me/my.file');
+ %! assert(a,'/home/me');
+ %! assert(b,'my');
+ %! assert(c,'.file');
+ %!test
+ %! [a,b,c]=fileparts('./here/file.txt');
+ %! assert(a,'./here');
+ %! assert(b,'file');
+ %! assert(c,'.txt');
+ %!test
+ %! [a,b,c]=fileparts('.\file.txt');
+ %! if ( ispc )
+ %! assert(a,'.');
+ %! assert(b,'file');
+ %! assert(c,'.txt');
+ %! else
+ %! assert(isempty(a),true);
+ %! assert(b,'.\file');
+ %! assert(c,'.txt');
+ %! endif
- proposed fileparts patch,
Hall, Benjamin <=