<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3096: path::lexically_relative is confused by trailing slashes</title>
<meta property="og:title" content="Issue 3096: path::lexically_relative is confused by trailing slashes">
<meta property="og:description" content="C++ library issue. Status: C++20">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3096.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#C++20">C++20</a> status.</em></p>
<h3 id="3096"><a href="lwg-defects.html#3096">3096</a>. <code>path::lexically_relative</code> is confused by trailing slashes</h3>
<p><b>Section:</b> 31.12.6.5.11 <a href="https://wg21.link/fs.path.gen">[fs.path.gen]</a> <b>Status:</b> <a href="lwg-active.html#C++20">C++20</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2018-04-04 <b>Last modified:</b> 2021-02-25</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#fs.path.gen">issues</a> in [fs.path.gen].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++20">C++20</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<code>filesystem::proximate("/dir", "/dir/")</code> returns <code>"."</code> when <code>"/dir"</code> exists, and <code>".."</code> 
otherwise. It should always be <code>"."</code> because whether it exists shouldn't matter.
<p/>
The problem is in <code>filesystem::path::lexically_relative</code>, as shown by:
</p>
<blockquote><pre>
path("/dir").lexically_relative("/dir/.");  <i>// yields ""</i>
path("/dir").lexically_relative("/dir/");   <i>// yields ".."</i>
</pre></blockquote>
<p>
The two calls should yield the same result, and when iteration of a <code>path</code> with a trailing slash gave "." as the final 
element they did yield the same result. In the final C++17 spec the trailing slash produces an empty filename in the 
iteration sequence, and <code>lexically_relative</code> doesn't handle that correctly.
</p>

<p><i>[2018-04-10, Jonathan comments]</i></p>

<p>
There are more inconsistencies with paths that are "obviously" equivalent to the human reader:
</p>
<blockquote><pre>
path("a/b/c").lexically_relative("a/b/c")    <i>// yields</i> "."
path("a/b/c").lexically_relative("a/b/c/")   <i>// yields</i> ".."
path("a/b/c").lexically_relative("a/b/c/.")  <i>// yields</i> ""
path("a/b/c/").lexically_relative("a/b/c")   <i>// yields</i> ""
path("a/b/c/.").lexically_relative("a/b/c")  <i>// yields</i> "."
path("a/b/c/.").lexically_relative("a/b/c/") <i>// yields</i> "../."
</pre></blockquote>
<p>
I think the right solution is:
</p>
<ol>
<li><p>
when counting <code>[b, base.end())</code> in bullet (4.2) handle empty filename elements (which can only occur as the last 
element, due to a trailing slash) equivalently to dot elements; and
</p></li>
<li><p>
add a new condition for the case where <code>n == 0</code> and <code>[a, end())</code> contains no non-empty elements, i.e. the 
paths are equivalent except for final dots or a final slash, which don't introduce any relative difference between the paths.
</p></li>
</ol>

<p><i>[2018-06-18 after reflector discussion]</i></p>

<p>Priority set to 2</p>

<p><i>[2018-08-23 Batavia Issues processing]</i></p>

<p>Status to Tentatively Ready</p>
<p><i>[2018-11, Adopted in San Diego]</i></p>



<p id="res-3096"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="https://wg21.link/n4727">N4727</a>.
</p>

<ol>
<li>
<p>Edit 31.12.6.5.11 <a href="https://wg21.link/fs.path.gen">[fs.path.gen]</a> as indicated:</p>
<blockquote>
<pre>
path lexically_relative(const path&amp; base) const;
</pre>
<blockquote>
<p>
-3- <i>Returns:</i> <code>*this</code> made relative to <code>base</code>. Does not resolve (31.12.6 <a href="https://wg21.link/fs.class.path">[fs.class.path]</a>) symlinks. 
Does not first normalize (31.12.6.2 <a href="https://wg21.link/fs.path.generic">[fs.path.generic]</a>) <code>*this</code> or <code>base</code>.
<p/>
-4- <i>Effects:</i> If <code>root_name() != base.root_name()</code> is <code>true</code> or <code>is_absolute() != base.is_absolute()</code>
is <code>true</code> or <code>!has_root_directory() &amp;&amp; base.has_root_directory()</code> is <code>true</code>, returns <code>path()</code>. 
Determines the first mismatched element of <code>*this</code> and <code>base</code> as if by:
</p>
<blockquote><pre>
auto [a, b] = mismatch(begin(), end(), base.begin(), base.end());
</pre></blockquote>
<p>
Then,
</p>
<ol style="list-style-type: none">
<li><p>(4.1) &mdash; if <code>a == end()</code> and <code>b == base.end()</code>, returns <code>path(".")</code>; otherwise</p></li>
<li><p>(4.2) &mdash; let <code>n</code> be the number of <i>filename</i> elements in <code>[b, base.end())</code> that are not dot 
or dot-dot <ins>or empty,</ins> minus the number that are dot-dot. If <code>n&lt;0</code>, returns <code>path()</code>; otherwise</p></li>
<li><p><ins>(4.?) &mdash; if <code>n == 0</code> and <code>(a == end() || a-&gt;empty())</code>, returns <code>path(".")</code>; otherwise</ins></p></li>
<li><p>(4.3) &mdash; returns an object of class <code>path</code> that is default-constructed, followed by [&hellip;]</p></li>
</ol>

</blockquote>
</blockquote>
</li>
</ol>





</body>
</html>
