Skip to content

Commit

Permalink
Merge pull request #3365 from WalterBright/rangeRootName
Browse files Browse the repository at this point in the history
Range-ify std.path.rootName()
  • Loading branch information
andralex committed Jun 3, 2015
2 parents c2ead96 + a73e4b4 commit 314242a
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions std/path.d
Expand Up @@ -537,6 +537,12 @@ unittest
/** Returns the root directory of the specified path, or $(D null) if the
path is not rooted.
Params:
path = filespec
Returns:
slice of $(D path)
Examples:
---
assert (rootName("foo") is null);
Expand All @@ -550,9 +556,12 @@ unittest
}
---
*/
inout(C)[] rootName(C)(inout(C)[] path) @safe pure nothrow @nogc if (isSomeChar!C)
auto rootName(R)(R path)
if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) ||
is(StringTypeOf!R))
{
if (path.empty) return null;
if (path.empty)
goto Lnull;

version (Posix)
{
Expand All @@ -574,7 +583,11 @@ inout(C)[] rootName(C)(inout(C)[] path) @safe pure nothrow @nogc if (isSomeCha
else static assert (0, "unsupported platform");

assert (!isRooted(path));
return null;
Lnull:
static if (is(StringTypeOf!R))
return null; // legacy code may rely on null return rather than slice
else
return path[0..0];
}


Expand All @@ -592,6 +605,22 @@ unittest
assert (rootName(`\\server\share\foo`) == `\\server\share`);
assert (rootName(`\\server\share`) == `\\server\share`);
}

import std.array;
import std.utf : byChar;

assert (rootName("".byChar).array == "");
assert (rootName("foo".byChar).array == "");
assert (rootName("/".byChar).array == "/");
assert (rootName("/foo/bar".byChar).array == "/");

version (Windows)
{
assert (rootName("d:foo".byChar).array == "");
assert (rootName(`d:\foo`.byChar).array == `d:\`);
assert (rootName(`\\server\share\foo`.byChar).array == `\\server\share`);
assert (rootName(`\\server\share`.byChar).array == `\\server\share`);
}
}


Expand Down

0 comments on commit 314242a

Please sign in to comment.