* https://svn.boost.org/trac/boost/ticket/1976#comment:2 * * "The idea: uncomplete(/foo/new, /foo/bar) => ../new * The use case for this is any time you get a full path (from an open dialog, perhaps) * and want to store a relative path so that the group of files can be moved to a different * directory without breaking the paths. An IDE would be a simple example, so that the * project fil
| 108 | * write to output, the remaining elements in path |
| 109 | */ |
| 110 | std::string naive_uncomplete(llvm::StringRef base, llvm::StringRef path) { |
| 111 | using namespace llvm; |
| 112 | if (sys::path::has_root_path(path)){ |
| 113 | if (sys::path::root_path(path) != sys::path::root_path(base)) { |
| 114 | return path; |
| 115 | } else { |
| 116 | return naive_uncomplete(sys::path::relative_path(base), sys::path::relative_path(path)); |
| 117 | } |
| 118 | } else { |
| 119 | if (sys::path::has_root_path(base)) { |
| 120 | std::cerr << "naive_uncomplete(" << base.str() << "," << path.str() |
| 121 | << "): cannot uncomplete a path relative path from a rooted base" << std::endl; |
| 122 | return path; |
| 123 | } else { |
| 124 | auto path_it = sys::path::begin(path); |
| 125 | auto path_it_end = sys::path::end(path); |
| 126 | auto base_it = sys::path::begin(base); |
| 127 | auto base_it_end = sys::path::end(base); |
| 128 | while ( path_it != path_it_end && base_it != base_it_end ) { |
| 129 | if (*path_it != *base_it) break; |
| 130 | ++path_it; ++base_it; |
| 131 | } |
| 132 | llvm::SmallString<128> result; |
| 133 | for (; base_it != base_it_end; ++base_it) { |
| 134 | sys::path::append(result, ".."); |
| 135 | } |
| 136 | sys::path::append(result, path_it, path_it_end); |
| 137 | return result.str(); |
| 138 | } |
| 139 | } |
| 140 | } |