Skip to content

Add API IndexNotOf  #28795

Closed
Closed

Description

@benaadams
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;

Activity

  1. EgorBo commented on Feb 26, 2019

    @EgorBo
    Member

    As an alternative name - IndexOfAnyBut

  2. grant-d commented on Feb 26, 2019

    @grant-d
    Contributor

    Or IndexOfAnyExcept

  3. danmoseley commented on Feb 26, 2019

    @danmoseley
    Contributor

    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)

  4. benaadams commented on Feb 26, 2019

    @benaadams
    MemberAuthor

    Are these more commonly needed on Span than on String?

    var firstNonSpace = text.AsSpan().IndexNotOf(' ');
    if (firstNonSpace > 0)
    {
        return text.Substring(firstNonSpace, text.Length - firstNonSpace);
    }
    
    return text;

    😉

    string would also presumably require culture and ignorecase variants? Thought I'd start simple :)

  5. krwq commented on Feb 26, 2019

    @krwq
    Member

    What about Find which takes a predicate? That should cover this scenario?

  6. benaadams commented on Feb 26, 2019

    @benaadams
    MemberAuthor

    What about Find which takes a predicate?

    Can't vectorize that for primitive types

  7. krwq commented on Feb 26, 2019

    @krwq
    Member

    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)

  8. stephentoub commented on Jan 10, 2020

    @stephentoub
    Member

    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.

  9. benaadams commented on Jan 14, 2020

    @benaadams
    MemberAuthor

    @danmosemsft 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)

    For string would could re-implement the following apis with these span versions

    partial class String
    {
        string Trim();
        string Trim(char trimChar);
        string Trim(params char[] trimChars);
        string TrimEnd();
        string TrimEnd(char trimChar);
        string TrimEnd(params char[] trimChars);
        string TrimStart();
        string TrimStart(char trimChar);
        string TrimStart(params char[] trimChars);
    }
  10. stephentoub commented on Jan 14, 2020

    @stephentoub
    Member

    For string would could re-implement the following apis with these span versions

    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.

  11. transferred this issue fromdotnet/corefxon Feb 1, 2020
  12. added this to the 5.0 milestone on Feb 1, 2020
  13. danmoseley commented on May 4, 2020

    @danmoseley
    Contributor

    Would we want all the variety of overloads we have for IndexOf, etc? That would give us

    public static int IndexNotOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>;
    public static int IndexNotOf<T>(this Span<T> span, T value) where T : IEquatable<T>;
    public static int IndexNotOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>;
    public static int IndexNotOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>;
    public static int IndexNotOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType);
    public static int IndexNotOfAny<T>(this Span<T> span, T value0, T value1) where T : IEquatable<T>;
    public static int IndexNotOfAny<T>(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
    public static int IndexNotOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
    public static int IndexNotOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>;
    public static int IndexNotOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    public static int IndexNotOfAny<T>(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    public static int LastIndexNotOf<T>(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>;
    public static int LastIndexNotOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType);
    public static int LastIndexNotOf<T>(this Span<T> span, T value) where T : IEquatable<T>;
    public static int LastIndexNotOf<T>(this Span<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>;
    public static int LastIndexNotOf<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> value) where T : IEquatable<T>;
    public static int LastIndexNotOfAny<T>(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
    public static int LastIndexNotOfAny<T>(this Span<T> span, T value0, T value1) where T : IEquatable<T>;
    public static int LastIndexNotOfAny<T>(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    public static int LastIndexNotOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
    public static int LastIndexNotOfAny<T>(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>;
    public static int LastIndexNotOfAny<T>(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
  14. 24 remaining items

  15. unlocked this conversation on Mar 23, 2022
  16. modified the milestones: Future, 7.0.0 on Mar 23, 2022
  17. EgorBo commented on Mar 23, 2022

    @EgorBo
    Member
  18. stephentoub commented on Mar 29, 2022

    @stephentoub
    Member

    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.

  19. adamsitnik commented on Mar 31, 2022

    @adamsitnik
    Member

    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?

  20. stephentoub commented on Mar 31, 2022

    @stephentoub
    Member

    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?

    The methods are:

    • Span vs ReadOnlySpan, crossed with
    • IndexOf vs LastIndexOf, crossed with
    • 1/2/3/any number of values being searched for

    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.

  21. terrajobst commented on Apr 12, 2022

    @terrajobst
    Contributor

    Video

    • We'd prefer a less-Yoda like name such as IndexOfAnyExcept which would also allow us to fold the single case into the multiple case.
    namespace System;
    
    public static partial class MemoryExtensions
    {
        int IndexOfAnyExcept(this Span<T> span, T value) where T : IEquatable<T>;
        int IndexOfAnyExcept(this Span<T> span, T value0, T value1) where T : IEquatable<T>;
        int IndexOfAnyExcept(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
        int IndexOfAnyExcept(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    
        int IndexOfAnyExcept(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>;
        int IndexOfAnyExcept(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>;
        int IndexOfAnyExcept(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
        int IndexOfAnyExcept(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    
        int LastIndexOfAnyExcept(this Span<T> span, T value) where T : IEquatable<T>;
        int LastIndexOfAnyExcept(this Span<T> span, T value0, T value1) where T : IEquatable<T>;
        int LastIndexOfAnyExcept(this Span<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
        int LastIndexOfAnyExcept(this Span<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    
        int LastIndexOfAnyExcept(this ReadOnlySpan<T> span, T value) where T : IEquatable<T>;
        int LastIndexOfAnyExcept(this ReadOnlySpan<T> span, T value0, T value1) where T : IEquatable<T>;
        int LastIndexOfAnyExcept(this ReadOnlySpan<T> span, T value0, T value1, T value2) where T : IEquatable<T>;
        int LastIndexOfAnyExcept(this ReadOnlySpan<T> span, ReadOnlySpan<T> values) where T : IEquatable<T>;
    }
  22. added
    api-approved
    and removed
    api-ready-for-review
    on Apr 12, 2022
  23. self-assigned this
    on Apr 12, 2022
  24. ghost added
    in-pr
    on Apr 13, 2022
  25. ghost removed
    in-pr
    on Apr 19, 2022
  26. ghost locked as resolved and limited conversation to collaborators on May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Memory

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions