Closed
Description
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