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.

Function std.path.buildPath

Combines one or more path segments.

This function takes a set of path segments, given as an input range of string elements or as a set of string arguments, and concatenates them with each other. Directory separators are inserted between segments if necessary. If any of the path segments are absolute (as defined by isAbsolute), the preceding segments will be dropped.

On Windows, if one of the path segments are rooted, but not absolute (e.g. \foo), all preceding path segments down to the previous root will be dropped. (See below for an example.)

This function always allocates memory to hold the resulting path. The variadic overload is guaranteed to only perform a single allocation, as is the range version if paths is a forward range.

Prototypes

immutable(ElementEncodingType!(ElementType!Range))[] buildPath(Range)(
  Range segments
)
if (isInputRange!Range && isSomeString!(ElementType!Range));

immutable(C)[] buildPath(C)(
  const(C)[][] paths
) pure nothrow @safe
if (isSomeChar!C);

Example

version (Posix)
{
    assert (buildPath("foo", "bar", "baz") == "foo/bar/baz");
    assert (buildPath("/foo/", "bar/baz")  == "/foo/bar/baz");
    assert (buildPath("/foo", "/bar")      == "/bar");
}

version (Windows)
{
    assert (buildPath("foo", "bar", "baz") == foo\bar\baz);
    assert (buildPath(c:\foo, bar\baz) == c:\foo\bar\baz);
    assert (buildPath("foo", d:\bar)     == d:\bar);
    assert (buildPath("foo", \bar)       == \bar);
    assert (buildPath(c:\foo, \bar)    == c:\bar);
}

Authors

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

License

Boost License 1.0

Comments