Skip to content

[API Proposal]: Path.ContainsInvalidPathChar and Path.ContainsInvalidFileNameChar #110301

@carlreinke

Description

@carlreinke

Background and motivation

Path.GetInvalidFileNameChars() and Path.GetInvalidPathChars() each make a copy and aren't even very convenient for one of the most common operation you want to do with them: ask if your string contains one.

API Proposal

namespace System.IO;

public partial static class Path
{
    public static bool ContainsInvalidPathChar(ReadOnlySpan<char> path);
    public static bool ContainsInvalidFileNameChar(ReadOnlySpan<char> fileName);
}

API Usage

if (Path.ContainsInvalidPathNameChar(pipeName) || pipeName.EndsWith(Path.DirectorySeparatorChar))
    throw new PlatformNotSupportedException(SR.PlatformNotSupported_InvalidPipeNameChars);
if (Path.ContainsInvalidFileNameChar(pipeName))
{
    throw new PlatformNotSupportedException(SR.PlatformNotSupported_InvalidPipeNameChars);
}

(Instead of caching in a static and using .AsSpan().ContainsAny(...).)

Alternative Designs

Maybe expose SearchValues. (Could do this in addition to the above proposal.)

namespace System.IO;

public partial static class Path
{
    public static SearchValues<char> InvalidPathCharSearchValues { get; }
    public static SearchValues<char> InvalidFileNameCharSearchValues { get; }
}

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions