-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: MemoryExtensions.ContainsAny{Except} #86528
Comments
Tagging subscribers to this area: @dotnet/area-system-memory Issue DetailsBackground and motivationSimilar to how we have The motivation behind the API is similar to the original
Example performance numbers for IndexOfAny vs ContainsAny for a set of Ascii values
API Proposalnamespace System;
public static class MemoryExtensions
{
// Existing
public static int IndexOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>?;
public static bool Contains<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>?;
public static int IndexOfAny<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static int IndexOfAnyExcept<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static int IndexOfAny(this ReadOnlySpan<char> span, SearchValues<string> values);
// Proposed
public static bool ContainsAny<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static bool ContainsAnyExcept<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static bool ContainsAny(this ReadOnlySpan<char> span, SearchValues<string> values);
} API Usage-if (text.IndexOfAny(s_invalidChars) >= 0)
+if (text.ContainsAny(s_invalidChars))
-if (text.IndexOfAnyExcept(s_allowedChars) >= 0)
+if (text.ContainsAnyExcept(s_allowedChars)) Alternative Designs
static bool ContainsAny{Except}(this ReadOnlySpan<T> span, T value0, T value1);
static bool ContainsAny{Except}(this ReadOnlySpan<T> span, T value0, T value1, value2);
static bool ContainsAny{Except}(this ReadOnlySpan<T> span, ReadOnlySpan<T> values);
static bool ContainsAny{Except}InRange(this ReadOnlySpan<T> span, T min, T max); static bool ContainsNewLine(ReadOnlySpan<char> value) =>
- value.IndexOfAny('\r', '\n') >= 0;
+ value.ContainsAny('\r', '\n'); RisksMore methods to think about -
|
Which of the places we're currently using SearchValues with IndexOfAny{Except} would be replaceable/improved with the proposed methods? |
In
Other:
AspNetCore (some places would likely switch from
|
Thanks. So a reasonable percentage of our current IndexOfAny use with SearchValues could now use ContainsAny. Would we be able to share most of the code in the SearchValues implementations? |
By abusing generics some more, yes: MihaZupan@2dec12f#diff-1616491faf2fad45e66a3301f2ed62e37e1c6454605ee94e2210913d58b32988 |
namespace System;
public static partial class MemoryExtensions
{
public static bool ContainsAny<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static bool ContainsAnyExcept<T>(this ReadOnlySpan<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static bool ContainsAny(this ReadOnlySpan<char> span, SearchValues<string> values);
public static bool ContainsAny<T>(this Span<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static bool ContainsAnyExcept<T>(this Span<T> span, SearchValues<T> values) where T : IEquatable<T>?;
public static bool ContainsAny(this Span<char> span, SearchValues<string> values);
// If any overloads of IndexOfAny{Except} are missing, consider them approved, too.
// NOTE: Generic constraints are missing in these methods, because they were not reflected in the proposal.
// The constraints should match the IndexOfAny versions
public static bool ContainsAnyExcept<T>(this ReadOnlySpan<T> span, T value);
public static bool ContainsAny<T>(this ReadOnlySpan<T> span, T value0, T value1);
public static bool ContainsAny<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2);
public static bool ContainsAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> values);
public static bool ContainsAnyInRange<T>(this ReadOnlySpan<T> span, T lowInclusive, T highInclusive);
public static bool ContainsAnyWhiteSpace(this ReadOnlySpan<char> span);
public static bool ContainsAnyExcept<T>(this ReadOnlySpan<T> span, T value0, T value1);
public static bool ContainsAnyExcept<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2);
public static bool ContainsAnyExcept<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> values);
public static bool ContainsAnyExceptInRange<T>(this ReadOnlySpan<T> span, T lowInclusive, T highInclusive);
public static bool ContainsAnyExceptWhiteSpace(this ReadOnlySpan<char> span);
public static bool ContainsAnyExcept<T>(this Span<T> span, T value);
public static bool ContainsAny<T>(this Span<T> span, T value0, T value1);
public static bool ContainsAny<T>(this Span<T> span, T value0, T value1, T value2);
public static bool ContainsAny<T>(this Span<T> span, ReadOnlySpan<T> values);
public static bool ContainsAnyInRange<T>(this Span<T> span, T lowInclusive, T highInclusive);
public static bool ContainsAnyExcept<T>(this Span<T> span, T value0, T value1);
public static bool ContainsAnyExcept<T>(this Span<T> span, T value0, T value1, T value2);
public static bool ContainsAnyExcept<T>(this Span<T> span, ReadOnlySpan<T> values);
public static bool ContainsAnyExceptInRange<T>(this Span<T> span, T lowInclusive, T highInclusive);
} |
@stephentoub, @MihaZupan; same for this. Is this critical for .NET 8 or is it fine if it slips to .NET 9? |
Background and motivation
Similar to how we have
span.IndexOf(T)
andspan.Contains(T)
, I'm proposing a contains variant ofIndexOfAny{Except}
.These methods would be semantically equivalent to
IndexOfAny{Except} >= 0
.The motivation behind the API is similar to the original
Contains
(#25228, #33785 (comment)):ContainsAny
/!ContainsAny
is more readable than various combinations ofIndexOfAny
+>= 0
< 0
!= -1
...Example performance numbers for IndexOfAny vs ContainsAny for a set of Ascii values
API Proposal
API Usage
Alternative Designs
IndexOfAny
has many overloads and variants. Should any of them also haveContainsAny
variants?Risks
More methods to think about -
ContainsAny
/ContainsAnyExcept
would be a new set of overloads on a common type.The text was updated successfully, but these errors were encountered: