Added Windows's altsep support to getExt, getName and join (bugfix, I think).

Some cosmetic changes.
This commit is contained in:
denis-sh 2011-05-28 04:31:26 -07:00
parent 06dd205d28
commit 254d9a8fce

View file

@ -50,6 +50,15 @@ version(Windows)
enum string linesep = "\r\n"; /// String used to separate lines. enum string linesep = "\r\n"; /// String used to separate lines.
enum string curdir = "."; /// String representing the current directory. enum string curdir = "."; /// String representing the current directory.
enum string pardir = ".."; /// String representing the parent directory. enum string pardir = ".."; /// String representing the parent directory.
static assert(sep.length == 1 && altsep.length == 1);
private bool isSepOrAltSep(Char)(Char ch) if (isSomeChar!Char) {
return (ch == sep[0]) | (ch == altsep[0]);
}
private bool isSepOrAltSepOrDriveSep(Char)(Char ch) if (isSomeChar!Char) {
return isSepOrAltSep(ch) | (ch == ':');
}
} }
version(Posix) version(Posix)
{ {
@ -67,6 +76,11 @@ version(Posix)
enum string linesep = "\n"; enum string linesep = "\n";
enum string curdir = "."; /// String representing the current directory. enum string curdir = "."; /// String representing the current directory.
enum string pardir = ".."; /// String representing the parent directory. enum string pardir = ".."; /// String representing the parent directory.
static assert(sep.length == 1 && altsep.length == 0);
private bool isSep(Char)(Char ch) if (isSomeChar!Char) {
return ch == sep[0];
}
} }
/***************************** /*****************************
@ -119,16 +133,16 @@ string getExt(string fullname)
while (i > 0) while (i > 0)
{ {
if (fullname[i - 1] == '.') if (fullname[i - 1] == '.')
return fullname[i .. fullname.length]; return fullname[i .. $];
i--; i--;
version(Windows) version(Windows)
{ {
if (fullname[i] == ':' || fullname[i] == '\\') if (isSepOrAltSepOrDriveSep(fullname[i]))
break; break;
} }
else version(Posix) else version(Posix)
{ {
if (fullname[i] == '/') if (isSep(fullname[i]))
break; break;
} }
else else
@ -218,12 +232,12 @@ string getName(string fullname)
i--; i--;
version(Windows) version(Windows)
{ {
if (fullname[i] == ':' || fullname[i] == '\\') if (isSepOrAltSepOrDriveSep(fullname[i]))
break; break;
} }
else version(Posix) else version(Posix)
{ {
if (fullname[i] == '/') if (isSep(fullname[i]))
break; break;
} }
else else
@ -297,12 +311,12 @@ body
{ {
version(Windows) version(Windows)
{ {
if (fullname[i - 1] == ':' || fullname[i - 1] == '\\' || fullname[i - 1] == '/') if (isSepOrAltSepOrDriveSep(fullname[i - 1]))
break; break;
} }
else version(Posix) else version(Posix)
{ {
if (fullname[i - 1] == '/') if (isSep(fullname[i - 1]))
break; break;
} }
else else
@ -310,7 +324,7 @@ body
static assert(0); static assert(0);
} }
} }
return chomp(fullname[i .. fullname.length], return chomp(fullname[i .. $],
extension.length ? extension : ""); extension.length ? extension : "");
} }
@ -661,7 +675,7 @@ string defaultExt(string filename, string ext)
if (existing.length == 0) if (existing.length == 0)
{ {
// Check for filename ending in '.' // Check for filename ending in '.'
if (filename.length && filename[filename.length - 1] == '.') if (filename.length && filename[$ - 1] == '.')
filename ~= ext; filename ~= ext;
else else
filename = filename ~ "." ~ ext; filename = filename ~ "." ~ ext;
@ -701,7 +715,7 @@ string addExt(string filename, string ext)
if (existing.length == 0) if (existing.length == 0)
{ {
// Check for filename ending in '.' // Check for filename ending in '.'
if (filename.length && filename[filename.length - 1] == '.') if (filename.length && filename[$ - 1] == '.')
filename ~= ext; filename ~= ext;
else else
filename = filename ~ "." ~ ext; filename = filename ~ "." ~ ext;
@ -744,12 +758,11 @@ bool isabs(in char[] path)
auto d = getDrive(path); auto d = getDrive(path);
version (Windows) version (Windows)
{ {
return d.length < path.length && return d.length < path.length && isSepOrAltSep(path[d.length]);
(path[d.length] == sep[0] || path[d.length] == altsep[0]);
} }
else version (Posix) else version (Posix)
{ {
return d.length < path.length && path[d.length] == sep[0]; return d.length < path.length && isSep(path[d.length]);
} }
else else
{ {
@ -784,17 +797,17 @@ string rel2abs(string path)
return path; return path;
} }
auto myDir = getcwd; auto myDir = getcwd;
if (path.startsWith(curdir[])) if (path.startsWith(curdir))
{ {
auto p = path[curdir.length .. $]; auto p = path[curdir.length .. $];
if (p.startsWith(sep[])) if (p.startsWith(sep))
path = p[sep.length .. $]; path = p[sep.length .. $];
else if (altsep.length && p.startsWith(altsep[])) else if (altsep.length && p.startsWith(altsep))
path = p[altsep.length .. $]; path = p[altsep.length .. $];
else if (!p.length) else if (!p.length)
path = null; path = null;
} }
return myDir.endsWith(sep[]) || path.length return myDir.endsWith(sep) || path.length
? join(myDir, path) ? join(myDir, path)
: myDir; : myDir;
} }
@ -857,7 +870,7 @@ string join(const(char)[] p1, const(char)[] p2, const(char)[][] more...)
version (Posix) version (Posix)
{ {
if (isabs(p2)) return p2.idup; if (isabs(p2)) return p2.idup;
if (p1.endsWith(sep[]) || altsep.length && p1.endsWith(altsep[])) if (p1.endsWith(sep) || altsep.length && p1.endsWith(altsep))
{ {
return cast(string) (p1 ~ p2); return cast(string) (p1 ~ p2);
} }
@ -879,16 +892,16 @@ string join(const(char)[] p1, const(char)[] p2, const(char)[][] more...)
{ {
p = cast(string) (p1 ~ p2); p = cast(string) (p1 ~ p2);
} }
else if (p2[0] == '\\') else if (isSepOrAltSep(p2[0]))
{ {
if (d1.length == 0) if (d1.length == 0)
p = p2.idup; p = p2.idup;
else if (p1[p1.length - 1] == '\\') else if (isSepOrAltSep(p1[$ - 1]))
p = cast(string) (p1 ~ p2[1 .. p2.length]); p = cast(string) (p1 ~ p2[1 .. $]);
else else
p = cast(string) (p1 ~ p2); p = cast(string) (p1 ~ p2);
} }
else if (p1[p1.length - 1] == '\\') else if (isSepOrAltSep(p1[$ - 1]))
{ {
p = cast(string) (p1 ~ p2); p = cast(string) (p1 ~ p2);
} }
@ -1107,8 +1120,8 @@ body
return true; return true;
foreach (j; ni .. filename.length) foreach (j; ni .. filename.length)
{ {
if (fnmatch(filename[j .. filename.length], if (fnmatch(filename[j .. $],
pattern[pi + 1 .. pattern.length])) pattern[pi + 1 .. $]))
return true; return true;
} }
return false; return false;