Apply normal() to weakly_canonical results, but only when know to be needed or it is not known if it may be needed.

This commit is contained in:
Beman 2015-08-19 08:43:36 -04:00
parent 24c6434228
commit c739cee694
2 changed files with 24 additions and 8 deletions

View File

@ -1887,16 +1887,27 @@ namespace detail
head.remove_filename();
}
bool tail_has_dots = false;
for (; itr != p.end(); ++itr)
{
tail /= *itr;
// for a later optimization, track if any dot or dot-dot elements are present
if (itr->native().size() <= 2
&& itr->native()[0] == dot
&& (itr->native().size() == 1 || itr->native()[1] == dot))
tail_has_dots = true;
}
// return head.empty() ? p : (tail.empty() ? canonical(head) : canonical(head) / tail);
if (head.empty())
return p;
return p.normal();
head = canonical(head, tmp_ec);
if (error(tmp_ec.value(), head, ec, "boost::filesystem::weakly_canonical"))
return path();
return tail.empty() ? head : head / tail;
return tail.empty()
? head
: (tail_has_dots // optimization: only normalize if tail had dot or dot-dot element
? (head/tail).normal()
: head/tail);
}
} // namespace detail

View File

@ -2029,15 +2029,20 @@ namespace
#endif
}
// weak_canonical_tests ------------------------------------------------------------//
// weakly_canonical_tests ----------------------------------------------------------//
void weak_canonical_tests()
void weakly_canonical_tests()
{
cout << "weak_canonical_tests..." << endl;
cout << "weakly_canonical_tests..." << endl;
cout << " dir is " << dir << endl;
BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/bar"), "no-such/foo/bar");
BOOST_TEST_EQ(fs::weakly_canonical(dir / "foo/bar"), dir / "foo/bar");
BOOST_TEST_EQ(fs::weakly_canonical("no-such/foo/../bar"), "no-such/bar");
BOOST_TEST_EQ(fs::weakly_canonical(dir), dir);
BOOST_TEST_EQ(fs::weakly_canonical(dir/"no-such/foo/bar"), dir/"no-such/foo/bar");
BOOST_TEST_EQ(fs::weakly_canonical(dir/"no-such/foo/../bar"), dir/"no-such/bar");
BOOST_TEST_EQ(fs::weakly_canonical(dir/"../no-such/foo/../bar"),
dir.parent_path()/"no-such/bar");
BOOST_TEST_EQ(fs::weakly_canonical("c:/no-such/foo/bar"), "c:/no-such/foo/bar");
fs::create_directory_symlink(dir / "d1", dir / "sld1");
@ -2167,7 +2172,7 @@ int cpp_main(int argc, char* argv[])
symlink_status_tests();
copy_symlink_tests(f1, d1);
canonical_symlink_tests();
weak_canonical_tests();
weakly_canonical_tests();
}
iterator_status_tests(); // lots of cases by now, so a good time to test
// dump_tree(dir);