Skip to content

Commit

Permalink
Range-ify std.path.driveName()
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jun 3, 2015
1 parent 314242a commit 97efe3f
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions std/path.d
Expand Up @@ -626,11 +626,18 @@ unittest



/** Returns the drive of a path, or $(D null) if the drive
is not specified. In the case of UNC paths, the network share
is returned.
/**
Get the drive portion of a path.
Always returns $(D null) on POSIX.
Params:
path = string or range of characters
Returns:
A slice of $(D _path) that is the drive, or an empty range if the drive
is not specified. In the case of UNC paths, the network share
is returned.
Always returns an empty range on POSIX.
Examples:
---
Expand All @@ -642,8 +649,9 @@ unittest
}
---
*/
inout(C)[] driveName(C)(inout(C)[] path) @safe pure nothrow @nogc
if (isSomeChar!C)
auto driveName(R)(R path)
if (isRandomAccessRange!R && hasSlicing!R && hasLength!R && isSomeChar!(ElementType!R) ||
is(StringTypeOf!R))
{
version (Windows)
{
Expand All @@ -652,7 +660,10 @@ inout(C)[] driveName(C)(inout(C)[] path) @safe pure nothrow @nogc
else if (isUNC(path))
return path[0 .. uncRootLength(path)];
}
return null;
static if (is(StringTypeOf!R))
return cast(ElementEncodingType!R[])null; // legacy code may rely on null return rather than slice
else
return path[0..0];
}


Expand All @@ -671,6 +682,23 @@ unittest

static assert (driveName(`d:\file`) == "d:");
}

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

version (Posix) assert (driveName("c:/foo".byChar).empty);
version (Windows)
{
assert (driveName(`dir\file`.byChar).empty);
assert (driveName(`d:file`.byChar).array == "d:");
assert (driveName(`d:\file`.byChar).array == "d:");
assert (driveName("d:".byChar).array == "d:");
assert (driveName(`\\server\share\file`.byChar).array == `\\server\share`);
assert (driveName(`\\server\share\`.byChar).array == `\\server\share`);
assert (driveName(`\\server\share`.byChar).array == `\\server\share`);

static assert (driveName(`d:\file`).array == "d:");
}
}


Expand Down

0 comments on commit 97efe3f

Please sign in to comment.