Skip to content

[API Proposal]: System.IO.Path APIs that takes ReadOnlySpan<char> instead of string as arguments. #88730

@aAndrzej-dev

Description

@aAndrzej-dev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.IOneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions