Function std.path.isValidPath
Checks whether
is a valid path.
path
Generally, this function checks that
is not empty, and that
each component of the path
path
either satisfies isValidFilename
or is equal to "."
or ".."
.
It does not check whether the path points to an existing file
or directory; use std.file.exists
for this purpose.
On Windows, some special rules apply:
- If the second character of
is a colon (path
':'
), the first character is interpreted as a drive letter, and must be in the range A-Z (case insensitive). - If
is on the formpath
(UNC\\$(I server)\$(I share)\...
path
),isValidFilename
is applied to server and share as well. - If
starts withpath
(long UNC\\?\
path
), the only requirement for the rest of the string is that it does not contain the null character. - If
starts withpath
(Win32 device namespace) this function returns\\.\
false
; such paths are beyond the scope of this module.
Prototype
bool isValidPath(Range)( Range path ) if (is(StringTypeOf!Range) || isRandomAccessRange!Range && hasLength!Range && hasSlicing!Range && isSomeChar!(ElementEncodingType!Range));
Parameters
Name | Description |
---|---|
path | string or Range of characters to check |
Returns
true if
is a valid path.
path
Example
assert (isValidPath("/foo/bar")); assert (!isValidPath("/foo\0/bar")); assert (isValidPath("/")); assert (isValidPath("a")); version (Windows) { assert (isValidPath(c:\
)); assert (isValidPath(c:\foo
)); assert (isValidPath(c:\foo\.\bar\\\..\
)); assert (!isValidPath(!:\foo
)); assert (!isValidPath(c::\foo
)); assert (!isValidPath(c:\foo?
)); assert (!isValidPath(c:\foo.
)); assert (isValidPath(\\server\share
)); assert (isValidPath(\\server\share\foo
)); assert (isValidPath(\\server\share\\foo
)); assert (!isValidPath(\\\server\share\foo
)); assert (!isValidPath(\\server\\share\foo
)); assert (!isValidPath(\\ser*er\share\foo
)); assert (!isValidPath(\\server\sha?e\foo
)); assert (!isValidPath(\\server\share\|oo
)); assert (isValidPath(\\?\<>:"?*|/\..\.
)); assert (!isValidPath("\\\\?\\foo\0bar")); assert (!isValidPath(\\.\PhysicalDisk1
)); assert (!isValidPath(\\
)); } import std.utf : byCodeUnit; assert (isValidPath("/foo/bar".byCodeUnit)); } /** Performs tilde expansion in paths on POSIX systems. On Windows, this function does nothing. There are two ways of using tilde expansion in a path. One involves using the tilde alone or followed by a path separator. In this case, the tilde will be expanded with the value of the environment variableHOME
. The second way is putting a username after the tilde (i.e.~john/Mail
). Here, the username will be searched for in the user database (i.e./etc/passwd
on Unix systems) and will expand to whatever path is stored there. The username is considered the string after the tilde ending at the first instance of a path separator. Note that using the~user
syntax may give different values from just~
if the environment variable doesn't match the value stored in the user database. When the environment variable version is used, the path won't be modified if the environment variable doesn't exist or it is empty. When the database version is used, the path won't be modified if the user doesn't exist in the database or there is not enough memory to perform the query. This function performs several memory allocations. Returns:inputPath
with the tilde expanded, or justinputPath
if it could not be expanded. For Windows,expandTilde
merely returns its argumentinputPath
. Examples:
void processFile(string path
)
{
// Allow calling this function with paths such as ~/foo
auto fullPath = expandTilde
(path
);
...
Authors
Lars Tandle Kyllingstad, Walter Bright, Grzegorz Adam Hankiewicz, Thomas Kühne, Andrei Alexandrescu