Add fix for "path::lexically_relative is confused by trailing slashes".

        * doc/xml/manual/intro.xml: Document LWG 3096 change.
        * src/filesystem/std-path.cc (path::lexically_relative(const path&)):
        Treat a final empty element equivalently to a final dot element.
        * testsuite/27_io/filesystem/path/generation/relative.cc: Add checks
        for the examples in the DR.

Tested x86_64-linux, committed to trunk.


commit 4e123b707a696a12eb29163116d956d68b067949
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Tue Apr 10 18:23:16 2018 +0100

    Apply resolution for LWG DR 3096
    
    Add fix for "path::lexically_relative is confused by trailing slashes".
    
            * doc/xml/manual/intro.xml: Document LWG 3096 change.
            * src/filesystem/std-path.cc (path::lexically_relative(const 
path&)):
            Treat a final empty element equivalently to a final dot element.
            * testsuite/27_io/filesystem/path/generation/relative.cc: Add checks
            for the examples in the DR.

diff --git a/libstdc++-v3/doc/xml/manual/intro.xml 
b/libstdc++-v3/doc/xml/manual/intro.xml
index cb187e1a2ed..7159b592c9f 100644
--- a/libstdc++-v3/doc/xml/manual/intro.xml
+++ b/libstdc++-v3/doc/xml/manual/intro.xml
@@ -1194,6 +1194,13 @@ requirements of the license of GCC.
     <listitem><para>Change constructors to constrained templates.
     </para></listitem></varlistentry>
 
+    <varlistentry xml:id="manual.bugs.dr3096"><term><link 
xmlns:xlink="http://www.w3.org/1999/xlink"; xlink:href="&DR;#3096">3096</link>:
+       <emphasis><code>path::lexically_relative</code> is confused by trailing 
slashes
+       </emphasis>
+    </term>
+    <listitem><para>Implement the fix for trailing slashes.
+    </para></listitem></varlistentry>
+
   </variablelist>
 
  </section>
diff --git a/libstdc++-v3/src/filesystem/std-path.cc 
b/libstdc++-v3/src/filesystem/std-path.cc
index fb8898d9040..06d882cf4de 100644
--- a/libstdc++-v3/src/filesystem/std-path.cc
+++ b/libstdc++-v3/src/filesystem/std-path.cc
@@ -529,10 +529,12 @@ path::lexically_relative(const path& base) const
       const path& p = *b;
       if (is_dotdot(p))
        --n;
-      else if (!is_dot(p))
+      else if (!p.empty() && !is_dot(p))
        ++n;
     }
-    if (n >= 0)
+    if (n == 0 && (a == end() || a->empty()))
+      ret = ".";
+    else if (n >= 0)
     {
       const path dotdot("..");
       while (n--)
diff --git 
a/libstdc++-v3/testsuite/27_io/filesystem/path/generation/relative.cc 
b/libstdc++-v3/testsuite/27_io/filesystem/path/generation/relative.cc
index 4bc475827b2..6652c196d07 100644
--- a/libstdc++-v3/testsuite/27_io/filesystem/path/generation/relative.cc
+++ b/libstdc++-v3/testsuite/27_io/filesystem/path/generation/relative.cc
@@ -45,6 +45,25 @@ test02()
   compare_paths( p.lexically_relative(p), "." );
   compare_paths( p.lexically_relative("a/../a/b/../b/c/../c/."), "../../b/c" );
   compare_paths( p.lexically_relative("../../../"), "" );
+
+  compare_paths( path("a/./.").lexically_relative("a"), "./." );
+}
+
+void
+test03()
+{
+  // LWG 3096
+  compare_paths( path("/dir").lexically_relative("/dir"), "." );
+  compare_paths( path("/dir").lexically_relative("/dir/"), "." );
+  compare_paths( path("/dir").lexically_relative("/dir/."), "." );
+
+  compare_paths( path("/dir/").lexically_relative("/dir"), "." );
+  compare_paths( path("/dir/").lexically_relative("/dir/"), "." );
+  compare_paths( path("/dir/").lexically_relative("/dir/."), "." );
+
+  compare_paths( path("/dir/.").lexically_relative("/dir"), "." );
+  compare_paths( path("/dir/.").lexically_relative("/dir/"), "." );
+  compare_paths( path("/dir/.").lexically_relative("/dir/."), "." );
 }
 
 int
@@ -52,4 +71,5 @@ main()
 {
   test01();
   test02();
+  test03();
 }

Reply via email to