isValidPath

Checks whether path is a valid path.

Generally, this function checks that path is not empty, and that each component of the 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 path is a colon (':'), the first character is interpreted as a drive letter, and must be in the range A-Z (case insensitive).
  • If path is on the form `\\$(I server)\$(I share)\...` (UNC path), isValidFilename is applied to server and share as well.
  • If path starts with `\\?\` (long UNC path), the only requirement for the rest of the string is that it does not contain the null character.
  • If path starts with `\\.\` (Win32 device namespace) this function returns false; such paths are beyond the scope of this module.
bool
isValidPath
(
Range
)
(
Range path
)
if (
(
isRandomAccessRange!Range &&
hasLength!Range
&&
hasSlicing!Range
&&
isSomeChar!(ElementEncodingType!Range)
||
)
&&
)

Parameters

path Range

string or Range of characters to check

Return Value

Type: bool

true if path is a valid path.

Examples

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));

Meta