Skip to content

CompareInfo.IsSuffix sometimes incorrectly splits collation elements and returns 'true' #43897

Closed

Description

@GrabYourPitchforks

Repro (.NET 5 on ICU):

var ci = CultureInfo.InvariantCulture.CompareInfo;
int idx = ci.IndexOf("\u0600x", "x", CompareOptions.None);
bool isSuffix = ci.IsSuffix("\u0600x", "x", CompareOptions.None);
Console.WriteLine($"idx = {idx}, isSuffix = {isSuffix}");

This sample prints idx = -1, isSuffix = True. These return values are inconsistent with respect to each other. If IsSuffix returns true, then IndexOf must return a non-negative value. It's the contrapositive of the code comment at:

* If there exists in set S any entry with the tuple values (startPos = <anything>, endPos = searchString.Length),
* we say "'searchString' ends with 'value'", and the expression "searchString.EndsWith(value, comparer)"
* should evaluate to true. If there exists no such entry in set S, then we say "'searchString' does not
* start with 'value'", and the expression "searchString.EndsWith(value, comparer)" should evaluate to false.

Activity

  1. added
    untriaged
    on Oct 27, 2020
  2. tarekgh commented on Oct 27, 2020

    @tarekgh
    Member
  3. removed
    untriaged
    on Oct 27, 2020
  4. added this to the 6.0.0 milestone on Oct 27, 2020
  5. GrabYourPitchforks commented on Oct 27, 2020

    @GrabYourPitchforks
    MemberAuthor

    Note: This sample uses CompareOptions.None. The other linked issue considers non-default CompareOptions arguments.

  6. tarekgh commented on Oct 27, 2020

    @tarekgh
    Member

    Note: This sample uses CompareOptions.None. The other linked issue considers non-default CompareOptions arguments.

    That is right. but the root cause the same which is coming from the optimization of this case. it is ok to keep both issues or close one and add the extra information to the other.

  7. adamsitnik commented on Oct 30, 2020

    @adamsitnik
    Member

    #40803 is not related to the "optimizaiton", this issue most probably is. I think it's best to keep both of them open until we fix them.

  8. adamsitnik commented on Oct 30, 2020

    @adamsitnik
    Member

    I've confirmed that usearch_first returns -1 for this particular case, while the first element returned by ucol_next(pSourceIterator) returns UCOL_IGNORABLE and hence the "optimized" version of the code assumes that the first character (\u0600) can be ignored and later returns true

    obraz

    obraz

    I've also tried it with .NET Core 3.1 on Windows to see what NLS has to say:

    obraz

    And for NLS it returns 1 and true.

    Which makes be confused.

    @GrabYourPitchforks @tarekgh In a perfect world, where there are no Unicode bugs, what should be the expected result and why?

  9. adamsitnik commented on Nov 4, 2020

    @adamsitnik
    Member

    @GrabYourPitchforks I've raised a question in the ICU bug tracker: https://unicode-org.atlassian.net/browse/ICU-21375

  10. tarekgh commented on Nov 4, 2020

    @tarekgh
    Member

    note that I expect this will happen with other codepoints (e.g. \u0601 too). I have some theory around these but let's wait if anyone officially reply the bug.

  11. adamsitnik commented on Nov 20, 2020

    @adamsitnik
    Member

    My question got answered by one of the ICU members:

    This looks like it's working as intended. U+0600 is one of many characters that map to a completely ignorable collation element: https://www.unicode.org/charts/collation/ --> https://www.unicode.org/charts/collation/chart_Ignored.html

    So StartsWith works as expected?

    I don't know why usearch_first() returns -1 for you. The StringSearch/SearchIterator classes have several options for how to deal with ignorables and combining marks etc. at the edges of a match. I suspect that there is optional behavior on top of collation element comparison responsible for rejecting the match.

    I am able to get -1 for a small repro without any custom settings. @tarekgh @GrabYourPitchforks should I file a bug for usearch_first then?

  12. tarekgh commented on Nov 20, 2020

    @tarekgh
    Member

    @adamsitnik I don't think this is a bug in ICU. I am seeing U+0600 is one of the characters that can be combined with the following characters which change the search operation https://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakProperty.txt. Looks by design to ICU. This is similar to \r\n case. That is my understanding but I cannot claim I am expert in that :-)

  13. 6 remaining items

  14. tarekgh commented on Aug 4, 2021

    @tarekgh
    Member

    I have looked deeply at this issue, and I am seeing the underlying problem is coming from ICU and not from our code.

    When using ICU and comparing the two strings x and \u0600x, it returns the result indicating the two strings are equal which is correct. The reason is the character \u0600 is ignorable character according to Unicode Standard. But when try to search for x in the string \u0600x using ICU APIs like usearch_search or usearch_last, it returns -1 which is wrong.

    I have reported the issue to ICU through the ticket. I mentioned the details in the ticket and the possible root cause of this problem.

    Note:

    In .NET, the problem shows up when doing:

    int idx = ci.IndexOf("\u0600x", "x", CompareOptions.None);

    but will not show up if we do

    int idx = ci.LastIndexOf("\u0600x", "x", CompareOptions.None);

    The reason is, in the first case we search forward which cause the processing encounter \u0600 first causing it to fallback to call ICU. In the second case we find the match (in our optimized code path) before hitting \u0600 and we'll not call ICU at all in such case.

  15. modified the milestones: 6.0.0, Future on Aug 4, 2021
  16. added
    tracking-external-issue
    on Aug 4, 2021
  17. ghost added
    in-pr
    on Aug 9, 2021
  18. ghost removed
    in-pr
    on Aug 17, 2021
  19. dotnet-policy-service commented on Feb 16, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  20. added
    backlog-cleanup-candidate
    on Feb 16, 2026
  21. dotnet-policy-service commented on Mar 2, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  22. removed this from the Future milestone on Mar 2, 2026
  23. locked and limited conversation to collaborators on Apr 1, 2026
  24. removed
    backlog-cleanup-candidate
    on Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Globalizationbugtracking-external-issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions