std::filesystem::path::lexically_normal, std::filesystem::path::lexically_relative, std::filesystem::path::lexically_proximate
From cppreference.com
< cpp | filesystem | path
path lexically_normal() const;
|
(1) | (since C++17) |
path lexically_relative(const path& base) const;
|
(2) | (since C++17) |
path lexically_proximate(const path& base) const;
|
(3) | (since C++17) |
1) Returns
*this
converted to normal form (no redundant dot or dot-dot elements, and if the last element is a non-root directory separator, dot is added)
2) Returns
*this
made relative to base
. Effectively does the following:
First, determines the first mismatched element of *this
and base
using std::mismatch(begin(), end(), base.begin(), base.end()), then
- if the first mismatched element of
*this
is equal tobegin()
or the first mismatched element ofbase
is equal tobase.begin()
, returnspath()
(empty path). - otherwise, if the first mismatched element of
*this
is equal toend()
and the first mismatched element ofbase
is equal tobase.end()
, returns path(".") - otherwise returns an object of class path composed via application of operator/=(path("..")) for each element in the half-open range
[
first mismatched element of base,
base.end())
, and then application offor each element in the half-open range
[
first mismatched element of *this,
end())
.
- if the first mismatched element of
3) If the value of lexically_relative(base) is not an empty path, return it. Otherwise return
*this
.
Contents |
[edit] Parameters
(none)
[edit] Return value
1) The normal form of the path
2) The relative form of the path
3) The proximate form of the path
[edit] Exceptions
(none)
[edit] Notes
These conversions are purely lexical. They do not check that the paths exist, do not follow symlinks, and do not access the filesystem at all. For symlink-following counterparts of lexically_relative
and lexically_proximate
, see relative and proximate.
On Windows, the returned path has backslashes (the preferred separators),
[edit] Example
Run this code
#include <iostream> #include <filesystem> #include <cassert> namespace fs = std::filesystem; int main() { assert(fs::path("foo/./bar/..").lexically_normal() == "foo"); assert(fs::path("foo/.///bar/../").lexically_normal() == "foo/."); assert(path("/a/d").lexically_relative("/a/b/c") == "../../d"); assert(path("/a/b/c").lexically_relative("/a/d") == "../b/c"); assert(path("a/b/c").lexically_relative("a") == "b/c"); assert(path("a/b/c").lexically_relative("a/b/c/x/y") == "../.."); assert(path("a/b/c").lexically_relative("a/b/c") == "."); assert(path("a/b").lexically_relative("c/d") == ""); }
[edit] See also
(C++17)
|
composes a relative path (function) |