
on Fri Jan 25 2013, Neil Groves <neil-AT-grovescomputing.com> wrote:
On Fri, Jan 25, 2013 at 4:30 PM, Beman Dawes <bdawes@acm.org> wrote:
On Thu, Jan 24, 2013 at 8:56 PM, Dave Abrahams <dave@boostpro.com> wrote:
In particular, this comes up because I'm trying to find the greatest common prefix of two paths. I thought this would be easy; I'd just use std::mismatch. But even once I've found the mismatch I don't see any obvious way to chop off the non-matching parts of one of the paths. I end up having to resort to some really ugly code (or I just haven't figured out how to use this thing correctly).
I wonder if this is *really* what you want!
A little credit, please. Yes, it's *really* what I want.
I suspect that you probably want to determine the common effective prefix of the paths after canonicalisation.
No, the paths are known to be already canonicalized (with a 'z' ;->)
Not particularly elegant, but this does work:
path x("/foo/bar"); path y("/foo/baar");
auto result = std::mismatch(x.begin(), x.end(), y.begin());
path prefix; for (auto itr = x.begin(); itr != result.first; ++itr) prefix /= *itr;
std::cout << prefix << std::endl;
I think this code doesn't "work" because it meets the stated requirements exactly! I think the requirements are normally greater than those we first think of when looking at the problem.
A. you didn't know my requirements ;-), and B. for such an operation requiring the input paths to be canonical beforehand might in fact be the most appropriate interface. -- Dave Abrahams BoostPro Computing Software Development Training http://www.boostpro.com Clang/LLVM/EDG Compilers C++ Boost