Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/filesystem/path/lexically normal"

From cppreference.com
< cpp‎ | filesystem‎ | path
(Late 15 / commit 066aba6 - more formal desc of lexically_relative)
Line 14: Line 14:
  
 
@1@ Returns {{tt|*this}} converted to [[cpp/filesystem/path|normal form]] (no redundant ''dot'' or ''dot-dot'' elements, and if the last element is a non-root directory separator, ''dot'' is added)
 
@1@ Returns {{tt|*this}} converted to [[cpp/filesystem/path|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 {{tt|*this}} made relative to {{tt|base}}. Effectively does the following:
+
@2@ Returns {{tt|*this}} made relative to {{tt|base}}. Effectively, first, determines the first mismatched element of {{tt|*this}} and {{tt|base}} as if by  {{c|auto [a, b] {{=}} mismatch(begin(), end(), base.begin(), base.end())}}, then
First, determines the first mismatched element of {{tt|*this}} and {{tt|base}} using {{c|std::mismatch(begin(), end(), base.begin(), base.end())}}, then
+
:* if {{c|a {{==}} begin()}} and {{c|b {{==}} base.begin()}}, returns {{c|path()}} (empty path).
:* if the first mismatched element of {{tt|*this}} is equal to {{tt|begin()}} or the first mismatched element of {{tt|base}} is equal to {{tt|base.begin()}}, returns {{tt|path()}} (empty path).
+
:* otherwise, if {{c|a {{==}} end()}} and {{c|b {{==}} base.end()}}, returns {{c|path(".")}};
:* otherwise, if the first mismatched element of {{tt|*this}} is equal to {{tt|end()}} and the first mismatched element of {{tt|base}} is equal to {{tt|base.end()}}, returns {{c|path(".")}}
+
:* otherwise returns an object composed from a default-constructed {{c|path()}} followed by one application of {{c|operator/{{=}}(path(".."))}} for each each element in the half-open range {{tt|[b, base.end())}}, and then by one application of {{c|1=operator/=}} for each element in the half-open range {{tt|[a, end())}}.
:* otherwise returns an object of class path composed via application of {{c|1=operator/=(path(".."))}} for each element in the half-open range {{ttb|[}}first mismatched element of base{{ttb|,}} base.end(){{ttb|)}}, and then application of {{tt|operator/=}} for each element in the half-open range {{ttb|[}}first mismatched element of *this{{ttb|,}} end(){{ttb|)}}.
+
 
@3@ If the value of {{c|lexically_relative(base)}} is not an empty path, return it. Otherwise return {{tt|*this}}.
 
@3@ If the value of {{c|lexically_relative(base)}} is not an empty path, return it. Otherwise return {{tt|*this}}.
  

Revision as of 06:20, 16 November 2016

 
 
 
 
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, first, determines the first mismatched element of *this and base as if by auto [a, b] = mismatch(begin(), end(), base.begin(), base.end()), then
  • if a == begin() and b == base.begin(), returns path() (empty path).
  • otherwise, if a == end() and b == base.end(), returns path(".");
  • otherwise returns an object composed from a default-constructed path() followed by one application of operator/=(path("..")) for each each element in the half-open range [b, base.end()), and then by one application of operator/= for each element in the half-open range [a, end()).
3) If the value of lexically_relative(base) is not an empty path, return it. Otherwise return *this.

Contents

Parameters

(none)

Return value

1) The normal form of the path
2) The relative form of the path
3) The proximate form of the path

Exceptions

(none)

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),

Example

#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") == "");
}

See also

composes a relative path
(function) [edit]