-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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 Path.RemoveRelativeSegments Api #2162
Comments
Do 1, 2, 3 above also apply to forward slash exactly the same? On all platforms? |
yes, the same will apply to forward slash and on all platforms |
This does not make sense to me
What is the skip length? When does it remove For yoru clarifications, please update the top post. Otherwise it seems reasonable to me, Jeremy can figure whether to flip the label to ready for review |
I updated the skipLength to rootLength in the post . The Api starts removing the relative segments from the path string after the rootlength. |
Can you add a For ease of use I'd also add You need to be very explicit about what normalizing the separator means. Please call out error handling for this as well. I presume given what we've done for Path.Join(), that we'd want empty/null to return empty/null. We should explicitly discuss in API review. |
@JeremyKuhne I'd like to add a vote for normalizing the separator. This is something I'm using |
Do I understand it correctly that it's length in characters, not in path segments? Why? In any case, I think this should be clarified. |
Because we can't always understand the relevance of separators. Notably with UNCs for paths and device paths on Windows ( |
@JeremyKuhne anything else that is required here ? |
@Anipik I think it's fine to mark ready for review whenever you're ready. |
Just a quick note on using this for URLs: what would be the result of |
What you want to do is normalize a path segment without it being combined with the current working directory. If you call |
Similar to dotnet/corefx#24685, this API will be defective on Linux in the presence of symbolic links:
Fire up WSL and give it a try. :)
|
This API will deliberately not follow links or touch the file system in any way. I'm updating the original description to me more precise about what the heuristics are. |
I guess since |
Alright, here is the shape we'd like to see: namespace System.IO
{
public class Path
{
public static string RemoveRedundantSegments(string path);
public static string RemoveRedundantSegments(ReadOnlySpan<char> path);
public static bool TryRemoveRedundantSegments(ReadOnlySpan<char> path, Span<char> destination, out int charsWritten);
}
} |
I'll work on this. |
Should this return Span, like existing |
@jkotas I see what you mean. I am working on this right now and as I was implementing that method, I started wondering about that. You were faster than me in asking about it. 😸 I did a quick replay of the video, but I couldn't find any mention of the return value, so it could've been a small oversight (I could've also missed it). Question to the reviewers in the video: Should we update the second signature to return a span instead of a string? What's the process to alter an already approved API? @terrajobst @bartonjs @GrabYourPitchforks @tannergooding @KrzysztofCwalina @scalablecory @JeremyKuhne |
I'm late to the game here, but I'm struggling with the word "redundant" in the API name, as I don't think we're matching the definition of "redundant". The path Node has a |
Moving this out to .NET 7.0. |
@carlossanlop What is the current progress for #37939 (closed for inactivity 😩)? |
@deeprobin The closed PR has the feature fully working and properly tested. The only problem is that it would be introduced a performance regression in Unix, which can be reproduced by calling Right now I don't have bandwidth to work on that, but we would welcome external contributions that can take on the above task and help drive that PR to completion. Would you be interested, @deeprobin? |
@carlossanlop I would take a look at it. However, I am currently not sure if I can improve the regression. |
Looking #37939 I guess the code could be more simple if handle the path from end to begin. |
@jeffhandley I've unassigned you given there has been no activity in .NET 7-8. |
Edit by carlossanlop: I marked this issue as up-for-grabs. If you're interested in taking it, please read this comment. This feature is fully implemented already, but the only thing pending to fix is a Unix performance regression before merging.
Rationale
Currently there is no direct way to normalize the path or to remove the relative Segments from any path string. We can use
GetFullPath
to normalize the path but it will resolve it relative to root directory or working directory which is not always desired.eg
working with relative paths in archives where we may require normalized paths that are "full" paths in said archive, but not on the system
Will help in building and working with relative urls.
Proposed Api
The
rootlength
is the length of the path which will never be trimmed while removing the relativeSegments.Behavior
Path.DirectorySeparatorChar
andPath.AltDirectorySeparatorChar
are considered path "segment" separators (\
and/
on Windows,/
on Unix)Path.AltDirectorySeparatorChar
characters will be changed toPath.DirectorySeparatorChar
(only relevant on Windows)..
will be removed...
will be removed along with the parent segment, if any.Implementation
This api is already implemented as an internal api and is being used by
GetFullPath
api to remove relative segments in case of device paths.runtime/src/libraries/System.Private.CoreLib/src/System/IO/PathInternal.cs
Line 129 in 96d9a47
Usage
More Usages can be find here
dotnet/corefx#37225
I have modified the internal tests to api proposal
Some Design Answers
Can directly call RemoveRelativeSegments(RelativePath);
Exception Handling
cc @danmosemsft @JeremyKuhne
related implementation prs dotnet/coreclr#24273 dotnet/corefx#37225
The text was updated successfully, but these errors were encountered: