std.path

This module is used to manipulate path strings.

All functions, with the exception of expandTilde (and in some cases absolutePath and relativePath), are pure string manipulation functions; they don't depend on any state outside the program, nor do they perform any actual file system actions. This has the consequence that the module does not make any distinction between a path that points to a directory and a path that points to a file, and it does not know whether or not the object pointed to by the path actually exists in the file system. To differentiate between these cases, use std.file.isDir and std.file.exists.

Note that on Windows, both the backslash (`\`) and the slash (`/`) are in principle valid directory separators. This module treats them both on equal footing, but in cases where a new separator is added, a backslash will be used. Furthermore, the buildNormalizedPath function will replace all slashes with backslashes on that platform.

In general, the functions in this module assume that the input paths are well-formed. (That is, they should not contain invalid characters, they should follow the file system's path format, etc.) The result of calling a function on an ill-formed path is undefined. When there is a chance that a path or a file name is invalid (for instance, when it has been input by the user), it may sometimes be desirable to use the isValidFilename and isValidPath functions to check this.

Most functions do not perform any memory allocations, and if a string is returned, it is usually a slice of an input string. If a function allocates, this is explicitly mentioned in the documentation.

Members

Enums

CaseSensitive
enum CaseSensitive

This enum is used as a template argument to functions which compare file names, and determines whether the comparison is case sensitive or not.

Functions

absolutePath
string absolutePath(string path, string base)

Transforms path into an absolute path.

asAbsolutePath
auto asAbsolutePath(R path)

Transforms path into an absolute path.

asNormalizedPath
auto asNormalizedPath(R path)

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.

asRelativePath
auto asRelativePath(R1 path, R2 base)

Transforms path into a path relative to base.

baseName
auto baseName(R path)
auto baseName(C[] path)
inout(C)[] baseName(inout(C)[] path, C1[] suffix)
buildNormalizedPath
immutable(C)[] buildNormalizedPath(const(C[])[] paths)

Performs the same task as buildPath, while at the same time 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.

buildPath
immutable(ElementEncodingType!(ElementType!Range))[] buildPath(Range segments)
immutable(C)[] buildPath(const(C)[][] paths)

Combines one or more path segments.

chainPath
auto chainPath(R1 r1, R2 r2, Ranges ranges)

Concatenate path segments together to form one path.

defaultExtension
immutable(C1)[] defaultExtension(C1[] path, C2[] ext)
dirName
auto dirName(R path)
auto dirName(C[] path)

Returns the parent directory of path. On Windows, this includes the drive letter if present. If path is a relative path and the parent directory is the current working directory, returns ".".

driveName
auto driveName(R path)
auto driveName(C[] path)

Get the drive portion of a path.

expandTilde
string expandTilde(string inputPath)

Performs tilde expansion in paths on POSIX systems. On Windows, this function does nothing.

extension
auto extension(R path)
filenameCharCmp
int filenameCharCmp(dchar a, dchar b)

Compares filename characters.

filenameCmp
int filenameCmp(Range1 filename1, Range2 filename2)

Compares file names and returns

globMatch
bool globMatch(Range path, const(C)[] pattern)

Matches a pattern against a path.

isAbsolute
bool isAbsolute(R path)

Determines whether a path is absolute or not.

isDirSeparator
bool isDirSeparator(dchar c)

Determines whether the given character is a directory separator.

isRooted
bool isRooted(R path)

Determines whether a path starts at a root directory.

isValidFilename
bool isValidFilename(Range filename)

Checks that the given file or directory name is valid.

isValidPath
bool isValidPath(Range path)

Checks whether path is a valid path.

pathSplitter
auto pathSplitter(R path)

Slice up a path into its elements.

relativePath
string relativePath(string path, string base)

Translates path into a relative path.

rootName
auto rootName(R path)
auto rootName(C[] path)

Returns the root directory of the specified path, or null if the path is not rooted.

setExtension
immutable(C1)[] setExtension(C1[] path, C2[] ext)
immutable(C1)[] setExtension(immutable(C1)[] path, const(C2)[] ext)

Sets or replaces an extension.

stripDrive
auto stripDrive(R path)
auto stripDrive(C[] path)

Strips the drive from a Windows path. On POSIX, the path is returned unaltered.

stripExtension
auto stripExtension(R path)
auto stripExtension(C[] path)

Remove extension from path.

withDefaultExtension
auto withDefaultExtension(R path, C[] ext)
auto withDefaultExtension(C1[] path, C2[] ext)

Set the extension of path to ext if path doesn't have one.

withExtension
auto withExtension(R path, C[] ext)
auto withExtension(C1[] path, C2[] ext)

Replace existing extension on filespec with new one.

Variables

dirSeparator
enum string dirSeparator;

String used to separate directory names in a path. Under POSIX this is a slash, under Windows a backslash.

pathSeparator
enum string pathSeparator;

Path separator string. A colon under POSIX, a semicolon under Windows.

Meta

Authors

Lars Tandle Kyllingstad, Walter Bright, Grzegorz Adam Hankiewicz, Thomas K$(UUML)hne, Andrei Alexandrescu