Background and motivation
System.IO.Path have some APIs that use ReadOnySpan<char> as well as string (for example: GetDirectoryName), but lots of APIs use only string as agruments (for example: GetFullPath). But most of them use ReadOnlySpan<char> internaly, so it is only change in APIs arguments type. By adding APIs with spans you can save some memory when performing multiple operations on paths.
API Proposal
namespace System.IO;
public static class Path
{
// Existing APIs
public static string GetFullPath(string path);
public static string GetFullPath(string path, string basePath);
public static string GetRelativePath(string relativeTo, string path);
public static string Combine(string path1, string path2);
// The same methods but with ReadOnlySpan<char> instead of string
public static string GetFullPath(ReadOnlySpan<char> path);
public static string GetFullPath(ReadOnlySpan<char> path, ReadOnlySpan<char> basePath);
public static string GetRelativePath(ReadOnlySpan<char> relativeTo, ReadOnlySpan<char> path);
public static string Combine(ReadOnlySpan<char> path1, ReadOnlySpan<char> path2);
}
API Usage
ReadOnlySpan<char> test1FileFullPath = @"C:\somefolder\test1.txt";
ReadOnlySpan<char> test2FileRelativeToTest1 = @"..\test2.txt"; // Located in `C:\test2.txt`
string otherFilePath = Path.GetFullPath(test2FileRelativeToTest1, Path.GetDirectoryName(test1FileFullPath));
When Path.GetFullPath use ReadOnlySpan<char> there will be no need to allocate string in Path.GetDirectoryName because it will return span not string.
Alternative Designs
No response
Risks
No response
Background and motivation
System.IO.Pathhave some APIs that useReadOnySpan<char>as well asstring(for example:GetDirectoryName), but lots of APIs use onlystringas agruments (for example:GetFullPath). But most of them useReadOnlySpan<char>internaly, so it is only change in APIs arguments type. By adding APIs with spans you can save some memory when performing multiple operations on paths.API Proposal
API Usage
When
Path.GetFullPathuseReadOnlySpan<char>there will be no need to allocate string inPath.GetDirectoryNamebecause it will return span not string.Alternative Designs
No response
Risks
No response