The OpenD Programming Language

asNormalizedPath

Normalize a path by resolving current/parent directory symbols ("." and "..") and removing superfluous directory separators. It will return "." if the path leads to the starting directory. On Windows, slashes are replaced with backslashes.

Using asNormalizedPath on empty paths will always return an empty path.

Does not resolve symbolic links.

This function always allocates memory to hold the resulting path. Use buildNormalizedPath to allocate memory and return a string.

asNormalizedPath
(
R
)
(
return scope R path
)

Parameters

path R

string or random access range representing the path to normalize

Return Value

Type: auto

normalized path as a forward range

Examples

import std.array;
assert(asNormalizedPath("foo/..").array == ".");

version (Posix)
{
    assert(asNormalizedPath("/foo/./bar/..//baz/").array == "/foo/baz");
    assert(asNormalizedPath("../foo/.").array == "../foo");
    assert(asNormalizedPath("/foo/bar/baz/").array == "/foo/bar/baz");
    assert(asNormalizedPath("/foo/./bar/../../baz").array == "/baz");
}

version (Windows)
{
    assert(asNormalizedPath(`c:\foo\.\bar/..\\baz\`).array == `c:\foo\baz`);
    assert(asNormalizedPath(`..\foo\.`).array == `..\foo`);
    assert(asNormalizedPath(`c:\foo\bar\baz\`).array == `c:\foo\bar\baz`);
    assert(asNormalizedPath(`c:\foo\bar/..`).array == `c:\foo`);
    assert(asNormalizedPath(`\\server\share\foo\..\bar`).array ==
            `\\server\share\bar`);
}

Meta