Path data type and helper functions

Type path

type path = str

A path or fragment of a filesystem path

Function basename

fn basename(pp: path) -> path

Get the file name portion of a path

Returns the portion of the path after the final path separator. The basename of "/usr/share" will be "share". If there are no path separators in the path then the returned path is identical to the provided path. If an empty path is provided or the path ends with a path separator then an empty path is returned.

Function connect

fn connect(pre: path, post: path) -> path

Connects to path segments

Given paths pre and post, removes any trailing path separator onpreand any leading path separator onpost`, and returns the concatenation of the two with a single path separator between them.

Function connect_many

fn connect_many(paths: ~[path]) -> path

Connects a vector of path segments into a single path.

Inserts path separators as needed.

Function dirname

fn dirname(pp: path) -> path

Get the directory portion of a path

Returns all of the path up to, but excluding, the final path separator. The dirname of "/usr/share" will be "/usr", but the dirname of "/usr/share/" is "/usr/share".

If the path is not prefixed with a directory, then "." is returned.

Function normalize

fn normalize(p: path) -> path

Collapses redundant path separators.

Does not follow symbolic links.

Examples

Function path_is_absolute

fn path_is_absolute(p: path) -> bool

Indicates whether a path is absolute.

A path is considered absolute if it begins at the filesystem root ("/") or, on Windows, begins with a drive letter.

Function path_sep

fn path_sep() -> str

Get the default path separator for the host platform

Function split

fn split(p: path) -> ~[path]

Split a path into its individual components

Splits a given path by path separators and returns a vector containing each piece of the path. On Windows, if the path is absolute then the first element of the returned vector will be the drive letter followed by a colon.

Function splitext

fn splitext(p: path) -> (str, str)

Split a path into the part before the extension and the extension

Split a path into a pair of strings with the first element being the filename without the extension and the second being either empty or the file extension including the period. Leading periods in the basename are ignored. If the path includes directory components then they are included in the filename part of the result pair.