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 isDir
and
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.
Category | Functions |
---|---|
Normalization | absolutePath
asAbsolutePath
asNormalizedPath
asRelativePath
buildNormalizedPath
buildPath
chainPath
expandTilde
|
Partitioning | baseName
dirName
dirSeparator
driveName
pathSeparator
pathSplitter
relativePath
rootName
stripDrive
|
Validation | isAbsolute
isDirSeparator
isRooted
isValidFilename
isValidPath
|
Extension | defaultExtension
extension
setExtension
stripExtension
withDefaultExtension
withExtension
|
Other | filenameCharCmp
filenameCmp
globMatch
CaseSensitive
|
Functions
Name | Description |
---|---|
absolutePath(path, base)
|
Transforms path into an absolute path.
|
asAbsolutePath(path)
|
Transforms path into an absolute path.
|
asNormalizedPath(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(path, base)
|
Transforms path into a path relative to base .
|
baseName(path)
|
|
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(segments)
|
Combines one or more path segments. |
chainPath(r1, r2, ranges)
|
Concatenate path segments together to form one path. |
defaultExtension(path, ext)
|
|
dirName(path)
|
Returns the parent directory of path. On Windows, this includes the drive letter if present. |
driveName(path)
|
Get the drive portion of a path. |
expandTilde(inputPath)
|
Performs tilde expansion in paths on POSIX systems. On Windows, this function does nothing. |
extension(path)
|
|
filenameCharCmp(a, b)
|
Compares filename characters. |
filenameCmp(filename1, filename2)
|
Compares file names and returns |
globMatch(path, pattern)
|
Matches a pattern against a path. |
isAbsolute(path)
|
Determines whether a path is absolute or not. |
isDirSeparator(c)
|
Determines whether the given character is a directory separator. |
isRooted(path)
|
Determines whether a path starts at a root directory. |
isValidFilename(filename)
|
Checks that the given file or directory name is valid. |
isValidPath(path)
|
Checks whether path is a valid path.
|
pathSplitter(path)
|
Slice up a path into its elements. |
relativePath(path, base)
|
Translates path into a relative path.
|
rootName(path)
|
Returns the root directory of the specified path, or null if the
path is not rooted.
|
setExtension(path, ext)
|
Sets or replaces an extension. |
stripDrive(path)
|
Strips the drive from a Windows path. On POSIX, the path is returned unaltered. |
stripExtension(path)
|
Remove extension from path. |
withDefaultExtension(path, ext)
|
Set the extension of path to ext if path doesn't have one.
|
withExtension(path, ext)
|
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.
|
Manifest constants
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