function for inverse relative path?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Thu Dec 7 04:55:48 UTC 2017


On Wednesday, December 06, 2017 17:36:04 Timothee Cour via Digitalmars-d 
wrote:
> what would be a robust way to do this `inverseRelativePath`, and
> should that be in std.path?
>
> ```
> auto a="/a/b/c.d";
> auto b="b/c.d";
> assert(inverseRelativePath(a, b) == "/a");
> assertThrown(inverseRelativePath(a, "c2.d"));
> ```

I've never heard of inverse relative paths, but it looks like all you're
doing is looking for a substring match at the end and returning the parts at
the front that don't match. If you're doing that, you could simply do
something like

enforce(lhs.length >= rhs.length, "some error message");
if(lhs[rhs.length .. $] == rhs)
    return lhs[0 .. rhs.length];
throw new Exception("some error message");

though if you want /a instead of /a/ in your example, some extra code would
have to be added for properly handling trailing slashes, and depending, you
might want to normalize paths first (though typically, that sort of thing is
left up to the caller). It might also need to be enforced that the left-hand
argument is an absolute path.

- Jonathan M Davis



More information about the Digitalmars-d mailing list