Open EgorBo opened 1 year ago
Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.
Author: | EgorBo |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.IO`, `untriaged` |
Milestone: | - |
I see the benefit. At the same time, I'd think we'd want to generalize it a bit more. What would it look like to just support replacing or removing an arbitrary set of characters, possibly specified via IndexOfAnyValues? i.e. why should this be specific to paths?
I see the benefit. At the same time, I'd think we'd want to generalize it a bit more. What would it look like to just support replacing or removing an arbitrary set of characters, possibly specified via IndexOfAnyValues? i.e. why should this be specific to paths?
I filed it just because I quite often hit this when I need to store some logs where I use some user's input for file name e.g. {username}.log
and just want to make sure it won't crash if user's name contains invalid chars 🙂 Having this in Path
increases chances to notice this API when user types Path.InvalidChar...
and gets intellisense suggestions
If we had an IndexOfAnyValues
ReplaceAny
API, would we still consider exposing this one on Path?
It still seems like a useful API, even if it's just a place that stores a shared IndexOfAnyValues
instance for you.
(Maybe we again need to consider a rename for IndexOfAnyValues)
What would it look like to just support replacing or removing an arbitrary set of characters
As others mentioned, seems that this could be closed in favor of a more general approach on IndexOfAnyValue. Once https://github.com/dotnet/runtime/issues/82622 resolves, we can decide what to do with this.
Background and motivation
It seems to be a popular operation to quickly erase all invalid symbols from a path/filename. For that, we expose the following APIs:
and expect users to write loop-like code to handle them by hands. What is even more unfortunate that these APIs are allocating on each call: https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/IO/Path.Windows.cs#L15-L31
How OSS projects use these to clean up paths:
dotnet/runtime
superunitybuild/buildtool
Tons of other places: https://grep.app/search?current=5&q=Path.GetInvalidFileNameChars&words=true&filter[lang][0]=C%23 https://grep.app/search?q=Path.GetInvalidPathChars&words=true&filter[lang][0]=C%23
API Proposal