paths
nimony/lib/std/paths.nim
type Path = object data: string
func path(s: string): PathConstructs a
Pathfrom the raw strings(normalization happens on demand).func $(x: Path): stringString form of
x(native path representation).proc hash(x: Path): uint64Stable hash honoring filesystem case sensitivity (
hashIgnoreCaseon case-insensitive FS).func ==(x: Path; y: Path): boolCompares two paths.
On a case-sensitive filesystem this is done case-sensitively otherwise case-insensitively.
func add(x: var Path; y: Path)func /(head: Path; tail: Path): PathJoins two directory names to one.
returns normalized path concatenation of
headandtail, preserving whether or nottailhas a trailing slash (or, if tail if empty, whether head has one).See also:
splitPath proc_- uri.combine proc
- uri./ proc
func splitPath(path: Path): tuple[head: Path, tail: Path]Splits a directory into
(head, tail)tuple, so thathead / tail == path(except for edge cases like "/usr").See also:
add proc_/ proc_/../ proc_relativePath proc_
func splitFile(path: Path): tuple[dir: Path, name: Path, ext: string]Splits a filename into
(dir, name, extension)tuple.dirdoes not end in DirSep unless it's/.extensionincludes the leading dot.If
pathhas no extension,extis the empty string. Ifpathhas no directory component,diris the empty string. Ifpathhas no filename component,nameandextare empty strings.See also:
extractFilename proc_lastPathPart proc_changeFileExt proc_addFileExt proc_
func isAbsolute(path: Path): boolChecks whether a given
pathis absolute.On Windows, network paths are considered absolute too.
proc relativePath(path: Path; base: Path; sep: char): PathConverts
pathto a path relative tobase.The
sep(default: DirSep) is used for the path normalizations, this can be useful to ensure the relative path only contains'/'so that it can be used for URL constructions.On Windows, if a root of
pathand a root ofbaseare different, returnspathas is because it is impossible to make a relative path. That means an absolute path can be returned.See also:
splitPath proc_parentDir proc_tailDir proc_
proc isRelativeTo(path: Path; base: Path): boolReturns true if
pathis relative tobase.func parentDir(path: Path): PathReturns the parent directory of
path.This is similar to
splitPath(path).headwhenpathdoesn't end in a dir separator, but also takes care of path normalizations. The remainder can be obtained withlastPathPart(path) proc_.See also:
relativePath proc_splitPath proc_tailDir proc_parentDirs iterator_
func tailDir(path: Path): PathReturns the tail part of
path.See also:
relativePath proc_splitPath proc_parentDir proc_
func isRootDir(path: Path): boolChecks whether a given
pathis a root directory.iterator parentDirs(path: Path; fromRoot: bool; inclusive: bool): PathWalks over all parent directories of a given
path.If
fromRootis true (default: false), the traversal will start from the file system root directory. Ifinclusiveis true (default), the original argument will be included in the traversal.Relative paths won't be expanded by this iterator. Instead, it will traverse only the directories appearing in the relative path.
See also:
parentDir proc_
func /../(head: Path; tail: Path): PathThe same as
parentDir(head) / tail, unless there is no parent directory. Thenhead / tailis performed instead.See also:
/ proc_parentDir proc_
func extractFilename(path: Path): PathExtracts the filename of a given
path.This is the same as
name & extfromsplitFile(path) proc_.See also:
splitFile proc_lastPathPart proc_changeFileExt proc_addFileExt proc_
func lastPathPart(path: Path): PathLike
extractFilename proc_, but ignores trailing dir separator; aka:baseName:idx: in some other languages.See also:
splitFile proc_extractFilename proc_changeFileExt proc_addFileExt proc_
func changeFileExt(filename: Path; ext: string): PathChanges the file extension to
ext.If the
filenamehas no extension,extwill be added. Ifext== "" then any extension is removed.Extshould be given without the leading'.', because some filesystems may use a different character. (Although I know of none such beast.)See also:
splitFile proc_extractFilename proc_lastPathPart proc_addFileExt proc_
func addFileExt(filename: Path; ext: string): PathAdds the file extension
exttofilename, unlessfilenamealready has an extension.Extshould be given without the leading'.', because some filesystems may use a different character. (Although I know of none such beast.)See also:
splitFile proc_extractFilename proc_lastPathPart proc_changeFileExt proc_
func unixToNativePath(path: Path; drive: Path): PathConverts an UNIX-like path to a native one.
On an UNIX system this does nothing. Else it converts
'/','.','..'to the appropriate things.On systems with a concept of "drives",
driveis used to determine which drive label to use during absolute path conversion.drivedefaults to the drive of the current working directory, and is ignored on systems that do not have a concept of "drives".proc getCurrentDir(): PathReturns the
current working directory:idx: i.e. where the built binary is run.So the path returned by this proc is determined at run time.
See also:
proc normalizeExe(file: var Path)Normalize executable name.
On Windows this proc will check if an
.exeextension needs to be added. On other platforms it does nothing.proc normalizePath(path: var Path)Normalize a path.
Consecutive directory separators are collapsed, including directory separators at the end of the path.
proc normalizePathEnd(path: var Path; trailingSep: bool)Normalize path so that it maintains a trailing separator or not depending on the value of the
trailingSepparameter.proc absolutePath(path: Path; root: Path): PathReturns the absolute path of
path, rooted atroot(which must be absolute; default: current directory). Ifpathis absolute, return it, ignoringroot.See also:
normalizePath proc_
proc expandTilde(path: Path): PathExpands
~or a path starting with~/to a full path, replacing~with getHomeDir() (otherwise returnspathunmodified).Windows: this is still supported despite the Windows platform not having this convention; also, both
~/and~\are handled.