Skip to content

Commit

Permalink
Replace tail recursion with iteration.
Browse files Browse the repository at this point in the history
  • Loading branch information
Beman committed Aug 9, 2015
1 parent 34dd2c7 commit 2101376
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions include/boost/filesystem/operations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -641,16 +641,20 @@ namespace boost
void resize_file(const path& p, uintmax_t size, system::error_code& ec)
{detail::resize_file(p, size, &ec);}
// TODO: Add error handling
// TODO: The returned path needs to be normalized. That currently is implied by
// any existing (and thus canonical) portion, but not by the non-existing portion.
// TODO: Replace tail-recursion with iteration.
// Note: a good alternate name might be weak_canonical() or weakly_canonical().
inline
path weak_canonical(const path& p)
{
if (exists(p))
return canonical(p);
return p.parent_path().empty() ? p : weak_canonical(p.parent_path()) / p.filename();
path head(p);
path tail;
path::iterator itr = p.end();

for (; !head.empty() && !exists(head); --itr)
head.remove_filename();

for (; itr != p.end(); ++itr)
tail /= *itr;

return head.empty() ? p : (tail.empty() ? canonical(head) : canonical(head) / tail);
}

// TODO: Add error handling
Expand Down

0 comments on commit 2101376

Please sign in to comment.