Skip to content

Proposal: Add ReadOnlySpan<char> overloads to CompareInfo #25428

Closed

Description

@ViktorHofer
namespace System.Globalization
{
    public partial class CompareInfo : IDeserializationCallback
    {
        public int IndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options = CompareOptions.None);
        public int LastIndexOf(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options = CompareOptions.None);
        public int Compare(ReadOnlySpan<char> source, ReadOnlySpan<char> value, CompareOptions options = CompareOptions.None);
        public bool IsSuffix(ReadOnlySpan<char> source, ReadOnlySpan<char> suffix, CompareOptions options = CompareOptions.None);
        public bool IsPrefix(ReadOnlySpan<char> source, ReadOnlySpan<char> prefix, CompareOptions options = CompareOptions.None);
        public static bool IsSortable(ReadOnlySpan<char> text);
    }
}

Rationale

While doing some Span optimizations in Regex I noticed that the current comparison extension methods on ROS don't support comparison with a specific culture: MemoryExtensions.CompareTo(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, StringComparison comparisonType).

With string I can do that in different ways either by string.Compare(string, string, CultureInfo, CompareOptions); or by StringComparer.Create(CultureInfo, bool ignoreCase).Compare(string, string).

cc @tarekgh @ahsonkhan

Activity

  1. tarekgh commented on Mar 13, 2018

    @tarekgh
    Member

    Is there a specific reason that we excluded a CultureInfo overload in the extension method?

    I don't think there was any specific reason to not expose this one. you cannot use the string for this comparison or your code is already using spans?

  2. tarekgh commented on Mar 13, 2018

    @tarekgh
    Member

    by the way, I believe we didn't expose any span API that takes CompareOptions either. If we are going to expose API takes culture, then we have to use CompareOptions that time.

  3. ViktorHofer commented on Mar 13, 2018

    @ViktorHofer
    MemberAuthor

    I don't think there was any specific reason to not expose this one.

    The current situation isn't ideal as I don't see a way to compare two strings with a specific culture and CompareOptions. In my case I need to allocate the string from the Span to do the comparison.

    you cannot use the string for this comparison or your code is already using spans?

    Correct, I'm switching to Span to reduce allocation.

    by the way, I believe we didn't expose any span API that takes CompareOptions either. If we are going to expose API takes culture, then we have to use CompareOptions that time.

    Agreed.

  4. tarekgh commented on Mar 13, 2018

    @tarekgh
    Member

    @ahsonkhan do you know any other workaround from the span side?

    @ViktorHofer do you have the original strings that you constructed the span from in the first place? if so, I believe you can use these strings without the allocation. otherwise, I would say just file API request issue but I am not sure if we still open to introduce more new APIs in 2.1.

  5. ViktorHofer commented on Mar 13, 2018

    @ViktorHofer
    MemberAuthor

    Not really in this layer where I'm operating. I will file an issue as I believe others will hit that also.

  6. changed the title [-]ROS comparison with specific culture[/-] [+]Proposal: Add support for ROS comparison with specified culture[/+] on Mar 13, 2018
  7. ViktorHofer commented on Mar 13, 2018

    @ViktorHofer
    MemberAuthor

    Reused this item for the proposal.

  8. ahsonkhan commented on Mar 13, 2018

    @ahsonkhan
    Contributor

    Should we also consider providing the other overloads on string like the ones that take bool and CultureInfo without CompareOptions:

    public static int Compare(string strA, string strB, bool ignoreCase)
    public static int Compare(string strA, string strB, bool ignoreCase, CultureInfo culture)

    What about the other APIs like Start/EndsWith? Consider adding those to the proposal as well.

    do you know any other workaround from the span side?

    There isn't really a workaround for this that doesn't end up re-allocating the string.

  9. ViktorHofer commented on Mar 13, 2018

    @ViktorHofer
    MemberAuthor

    Should we also consider providing the other overloads on string like the ones that take bool and CultureInfo without CompareOptions:

    I believe just using bool ignoreCase is legacy and not needed if you have a CompareOptions enum. @tarekgh can correct me here.

  10. tarekgh commented on Mar 13, 2018

    @tarekgh
    Member

    I don't think we need the APIs takes the bool ignore cases. but we may need to have IndexOf/LastIndexOf overloads that take culture and CompareOptions

  11. ViktorHofer commented on Mar 14, 2018

    @ViktorHofer
    MemberAuthor

    but we may need to have IndexOf/LastIndexOf overloads that take culture and CompareOptions

    We currently don't have such for string. So this will be entirely new APIs?

  12. tarekgh commented on Mar 14, 2018

    @tarekgh
    Member

    We currently don't have such for string. So this will be entirely new APIs?

    We have these exposed from CompareInfo class.

  13. ViktorHofer commented on Mar 14, 2018

    @ViktorHofer
    MemberAuthor

    What about the other APIs like Start/EndsWith? Consider adding those to the proposal as well.

    @tarekgh should we also add these? I see that these only have bool ignoreCase, CultureInfo culture overloads on string. Should we mimic them or can we also make use of CompareOptions here?

  14. tarekgh commented on Mar 14, 2018

    @tarekgh
    Member

    should we also add these? I see that these only have bool ignoreCase, CultureInfo culture overloads on string. Should we mimic them or can we also make use of CompareOptions here?

    The apis should look like:

     public static int IndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, CultureInfo culture, CompareOptions options);
     public static int LastIndexOf(this ReadOnlySpan<char> span, ReadOnlySpan<char> value, CultureInfo culture, CompareOptions options);
     public static bool IsPrefix(String, String, CultureInfo culture, CompareOptions options);
     public static bool IsSuffix(String, String, CultureInfo culture, CompareOptions options);

    The other option is to add these extension methods to CompareInfo (instead of the Span) and at that time we'll not pass CultureInfo.

    I prefer the CompareInfo option because this will be consistent with the strings APIs.

  15. tarekgh commented on Mar 14, 2018

    @tarekgh
    Member

    Also, I prefer to expose the proposed CompareTo from CompareInfo too with the name "Compare"

  16. 39 remaining items

  17. tarekgh commented on Jan 31, 2020

    @tarekgh
    Member

    @jskeet So far this is marked for netcoreapp 5.0 and not netstandard 2.0. Unfortunately, the only workaround for this has to allocate strings.

    @terrajobst is there a way we can add these new APIs to netsatndard 2.0?

  18. jkotas commented on Jan 31, 2020

    @jkotas
    Member

    @terrajobst is there a way we can add these new APIs to netsatndard 2.0?

    No. .NET Standard 2.0 is set in stone.

  19. jskeet commented on Jan 31, 2020

    @jskeet

    It can't be added to .NET Standard 2.0... but it could be added to the System.Memory nuget package. That's what I was hoping for :)

  20. jkotas commented on Jan 31, 2020

    @jkotas
    Member

    System.Memory is not a nuget package anymore. It was folded into runtime (aka inboxed). It cannot evolve.

    Also, adding this to System.Memory would mean using extension methods for this that is not the natural shape for these APIs and does not follow API design guidelines.

  21. transferred this issue fromdotnet/corefxon Jan 31, 2020
  22. added this to the 5.0 milestone on Jan 31, 2020
  23. added
    untriaged
    on Feb 23, 2020
  24. removed
    untriaged
    on Mar 3, 2020
  25. GrabYourPitchforks commented on Apr 15, 2020

    @GrabYourPitchforks
    Member

    Missed a few overloads on CompareInfo earlier:

    namespace System.Globalization
    {
        public sealed partial class CompareInfo : System.Runtime.Serialization.IDeserializationCallback
        {
            /* EXISTING APIS (partial list) */
            public int Compare(string? string1, string? string2, System.Globalization.CompareOptions options) { throw null; }
            public System.Globalization.SortKey GetSortKey(string source, System.Globalization.CompareOptions options) { throw null; }
            public int IndexOf(string source, char value, System.Globalization.CompareOptions options) { throw null; }
            public int IndexOf(string source, string value, System.Globalization.CompareOptions options) { throw null; }
            public bool IsPrefix(string source, string prefix, System.Globalization.CompareOptions options) { throw null; }
            public static bool IsSortable(char ch) { throw null; }
            public static bool IsSortable(string text) { throw null; }
            public bool IsSuffix(string source, string suffix, System.Globalization.CompareOptions options) { throw null; }
            public int LastIndexOf(string source, char value, System.Globalization.CompareOptions options) { throw null; }
            public int LastIndexOf(string source, string value, System.Globalization.CompareOptions options) { throw null; }
    
            /* ALREADY APPROVED APIS */
            public int Compare(System.ReadOnlySpan<char> string1, System.ReadOnlySpan<char> string2, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public int IndexOf(System.ReadOnlySpan<char> source, System.ReadOnlySpan<char> value, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public bool IsPrefix(System.ReadOnlySpan<char> source, System.ReadOnlySpan<char> prefix, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public static bool IsSortable(System.ReadOnlySpan<char> text) { throw null; }
            public bool IsSuffix(System.ReadOnlySpan<char> source, System.ReadOnlySpan<char> suffix, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public int LastIndexOf(System.ReadOnlySpan<char> source, System.ReadOnlySpan<char> value, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
    
            /* NEW PROPOSED APIS */
            public int GetSortKey(System.ReadOnlySpan<char> source, System.Span<byte> destination, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public int GetSortKeyLength(System.ReadOnlySpan<char> source, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public int IndexOf(System.ReadOnlySpan<char> source, System.Text.Rune value, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
            public static bool IsSortable(System.Text.Rune value) { throw null; }
            public int LastIndexOf(System.ReadOnlySpan<char> source, System.Text.Rune value, System.Globalization.CompareOptions options = System.Globalization.CompareOptions.None) { throw null; }
        }
    }

    Marked as blocking because it's blocking PR #1514.

    Related: #27912

  26. added
    blocking
    and removed
    api-approved
    on Apr 15, 2020
  27. GrabYourPitchforks commented on Apr 24, 2020

    @GrabYourPitchforks
    Member

    Approved over email. Feedback was to rename the destination parameter of GetSortKey to destination instead of sortKey. I've updated the proposal to reflect this.

  28. ghost locked as resolved and limited conversation to collaborators on Dec 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-System.Globalizationblocking

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions