-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add function stripPrefix
#73
Comments
I agree this is a useful addition. I think people usually do some super unsafe drop stuff which is very unpleasant. I note the name stripPrefix clashes with List, and nothing else in this module does, so we need a different name. I also note the closely related operation of take/drop directories from the front of a path. I wonder if they should be added at the same time? Or even if they replace this as a general case of it? In Shake I have takeDirectory1 (terrible name) for that purpose. |
I agree that it makes sense not to clash with I dont think that taking and dropping directories form the front of a path are a generalization of this. And even if they are, a well-named, well-documented function would make it easier to discover the functionality and understand it. Name suggestions, mainly for brainstorming:
|
I'd appreciate this function. 👍 I just tried to implement a function that checks whether a file is inside a directory or inside its subdirectories using this flawed approach:
This returns false, also for files that were indeed inside the directory. After a bit of head scratching I realized that |
@mb720 I would have thought |
Thanks @ndmitchell! I forgot that |
So I think what we want is:
Which turns |
I think, this is a reasonable primitive. |
For other additions to the library, there are the split/drop/take functions all in the library - I see no reason to not do that here too. The main question I have open is coming up with a name that is clear. |
In #77 I offered to change maintainership of this library. Given this decision is the big one outstanding, I'll wait for a resolution on that matter before figuring out this patch. (And sorry for the delay, I've got weighed down with lots of other stuff) |
A dual to |
There are some rather peculiar edge cases on windows: stripPrefix "C:foo" "C:foo/bar" This is a relative path that has a drive letter and means "relative to the current directory on drive C". Semantically it should probably produce Similarly UNC paths are difficult. Stripping can produce an "invalid path". But that problem already exists in other functions to some degree. The question is what properties do we really expect. If it's "the entire prefix should be gone from the output", then that won't hold on windows. |
The intention is to strip away a common prefix and return the relative FilePath, if and only if there is a common prefix.
Possible function signature:
Some example calls:
Motivation: I often want to strip away some directory from another FilePath if and only if the given FilePath is part of the directory. This is some combination of
makeRelative
and checking, whether the given FilePath is in the given directory.Other programming languages, such as Rust (https://doc.rust-lang.org/std/path/struct.Path.html#method.strip_prefix) also implement a similar function in the standard library for Path manipulation.
Is such a function useful to others as well?
Or maybe, this function is trivial to express with the given API already?
The text was updated successfully, but these errors were encountered: