Skip to content

Commit

Permalink
Allow empty path nodes. Fixes #177.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-ludwig committed May 14, 2014
1 parent d02e48e commit 466d06f
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions source/dub/internal/vibecompat/inet/path.d
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ struct Path {
m_nodes = cast(immutable)splitPath(pathstr);
m_absolute = (pathstr.startsWith("/") || m_nodes.length > 0 && (m_nodes[0].toString().countUntil(':')>0 || m_nodes[0] == "\\"));
m_endsWithSlash = pathstr.endsWith("/");
foreach( e; m_nodes ) assert(e.toString().length > 0);
}

/// Constructs a path object from a list of PathEntry objects.
Expand Down Expand Up @@ -65,7 +64,7 @@ struct Path {
default:
newnodes ~= n;
break;
case ".": break;
case "", ".": break;
case "..":
enforce(!m_absolute || newnodes.length > 0, "Path goes below root node.");
if( newnodes.length > 0 && newnodes[$-1] != ".." ) newnodes = newnodes[0 .. $-1];
Expand Down Expand Up @@ -215,7 +214,7 @@ struct Path {
foreach(folder; rhs.m_nodes){
switch(folder.toString()){
default: ret.m_nodes = ret.m_nodes ~ folder; break;
case ".": break;
case "", ".": break;
case "..":
enforce(!ret.absolute || ret.m_nodes.length > 0, "Relative path goes below root node!");
if( ret.m_nodes.length > 0 && ret.m_nodes[$-1].toString() != ".." )
Expand Down Expand Up @@ -334,12 +333,10 @@ PathEntry[] splitPath(string path)
size_t startidx = 0;
foreach( i, char ch; path )
if( ch == '\\' || ch == '/' ){
enforce(i - startidx > 0, "Empty path entries not allowed.");
elements[eidx++] = PathEntry(path[startidx .. i]);
startidx = i+1;
}
elements[eidx++] = PathEntry(path[startidx .. $]);
enforce(path.length - startidx > 0, "Empty path entries not allowed.");
assert(eidx == nelements);
return elements;
}
Expand All @@ -364,6 +361,7 @@ unittest
{
auto unc = "\\\\server\\share\\path";
auto uncp = Path(unc);
uncp.normalize();
version(Windows) assert(uncp.toNativeString() == unc);
assert(uncp.absolute);
assert(!uncp.endsWithSlash);
Expand Down Expand Up @@ -397,9 +395,13 @@ unittest
{
auto winpath = "C:\\windows\\test";
auto winpathp = Path(winpath);
assert(winpathp.toString() == "/C:/windows/test");
version(Windows) assert(winpathp.toNativeString() == winpath);
else assert(winpathp.toNativeString() == "/C:/windows/test");
version(Windows) {
assert(winpathp.toString() == "C:/windows/test", winpathp.toString());
assert(winpathp.toNativeString() == winpath);
} else {
assert(winpathp.toString() == "/C:/windows/test", winpathp.toString());
assert(winpathp.toNativeString() == "/C:/windows/test");
}
assert(winpathp.absolute);
assert(!winpathp.endsWithSlash);
assert(winpathp.length == 3);
Expand All @@ -416,6 +418,14 @@ unittest
assert(dotpathp.toString() == "/test2/x/y");
}

{
auto dotpath = "/test/..////test2//./x/y";
auto dotpathp = Path(dotpath);
assert(dotpathp.toString() == "/test/..////test2//./x/y");
dotpathp.normalize();
assert(dotpathp.toString() == "/test2/x/y");
}

{
auto parentpath = "/path/to/parent";
auto parentpathp = Path(parentpath);
Expand Down

0 comments on commit 466d06f

Please sign in to comment.