Skip to content

[API Proposal]: IndexOfAnyValues<T>.Contains(T) #78722

Closed
@MihaZupan

Description

@MihaZupan

Background and motivation

Augmenting the recently-added IndexOfAnyValues APIs: #68328 (comment)

When updating existing code to use the new IndexOfAnyValues API, it is very common to replace the existing needle constant with the IndexOfAnyValues instance.

This is fine if the only operations using said needle were {Last}IndexOfAny{Except} calls, but it is not uncommon to also use that needle for single-value checks Needle.Contains(c). In those cases, you are forced into keeping the original needle around.

As IndexOfAnyValues is also already a representation of values optimized for searching, it can be more efficient than a full linear scan of the needle.

API Proposal

namespace System.Buffers;

public class IndexOfAnyValues<T> where T : IEquatable<T>?
{
    public virtual bool Contains(T value);
}

API Usage

Instead of

private const string Needle = ",[]&*+";
private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(Needle);

if (Needle.Contains(c))
{
    // ...
}

you can do

private static readonly IndexOfAnyValues<char> s_needle = IndexOfAnyValues.Create(",[]&*+");

if (s_needle.Contains(c))
{
    // ...
}

Alternative Designs

No response

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions