View source code Display the source code in std/path.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

Module 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.

Upgrading

can be replaced with globMatch.

Functions

Name Description
absolutePath Tranforms path into an absolute path.
asAbsolutePath Tranforms path into an absolute path.
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.
asRelativePath Transforms path into a path relative to base.
baseName Returns the name of a file, without any leading directory and with an optional suffix chopped off.
buildNormalizedPath 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 Combines one or more path segments.
chainPath Concatenate path segments together to form one path.
defaultExtension Returns the path given by path, with the extension given by ext appended if the path doesn't already have one.
dirName Returns the directory part of a path. On Windows, this includes the drive letter if present.
driveName Get the drive portion of a path.
expandTilde Performs tilde expansion in paths on POSIX systems. On Windows, this function does nothing.
extension Returns the extension part of a file name, including the dot.
filenameCharCmp Compares filename characters and return < 0 if a < b, 0 if a == b and > 0 if a > b.
filenameCmp Compares file names and returns
globMatch Matches a pattern against a path.
isAbsolute Determines whether a path is absolute or not.
isDirSeparator Determines whether the given character is a directory separator.
isRooted Determines whether a path starts at a root directory.
isValidFilename Checks that the given file or directory name is valid.
isValidPath Checks whether path is a valid path.
pathSplitter Slice up a path into its elements.
relativePath Translates path into a relative path.
rootName Returns the root directory of the specified path, or null if the path is not rooted.
setExtension Returns a string containing the path given by path, but where the extension has been set to ext.
stripDrive Strips the drive from a Windows path. On POSIX, the path is returned unaltered.
stripExtension Remove extension from path.
withDefaultExtension Set the extension of path to ext if path doesn't have one.
withExtension Replace existing extension on filespec with new one.

Enums

Name Description
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.

Enum values

Name Type Description
dirSeparator String used to separate directory names in a path. Under POSIX this is a slash, under Windows a backslash.
pathSeparator Path separator string. A colon under POSIX, a semicolon under Windows.

Authors

Lars Tandle Kyllingstad, Walter Bright, Grzegorz Adam Hankiewicz, Thomas Kühne, Andrei Alexandrescu

License

Boost License 1.0

Comments