Skip to content

string.StartsWith(“--”) returns true for “/*” in case of mi and mi-NZ culture #43772

Closed

Description

@soocho-MSFT
foreach (var ci in CultureInfo.GetCultures(CultureTypes.AllCultures))
{
    System.Threading.Thread.CurrentThread.CurrentCulture = ci;
    System.Threading.Thread.CurrentThread.CurrentUICulture = ci;

    Console.WriteLine("1 " + "/*".StartsWith("--").ToString() + "\t" + ci.ToString());
    Console.WriteLine("2 " + "/*".EndsWith("--").ToString() + "\t" + ci.ToString());
    Console.WriteLine("3 " + string.Compare("/*", "--").ToString() + "\t" + ci.ToString());
    Console.WriteLine("4 " + "/*".Equals("--").ToString() + "\t" + ci.ToString());
    Console.WriteLine("5 " + "/*".IndexOf("--").ToString() + "\t" + ci.ToString());
}

...
1 False mgo-CM
2 False mgo-CM
3 1     mgo-CM
4 False mgo-CM
5 -1    mgo-CM
1 True  mi
2 True  mi
3 1     mi
4 False mi
5 0     mi
1 True  mi-NZ
2 True  mi-NZ
3 1     mi-NZ
4 False mi-NZ
5 0     mi-NZ
1 False mk
2 False mk
3 1     mk
4 False mk
5 -1    mk
...

For mi and mi-NZ, .StartsWith() and .EndsWith() is returning true while .Compare() and .Equals() returns different result.

Is this a bug in .NET or something expected?

Activity

  1. added
    untriaged
    on Oct 23, 2020
  2. Dotnet-GitSync-Bot commented on Oct 23, 2020

    @Dotnet-GitSync-Bot
    Collaborator

    I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

  3. changed the title [-]string.StartsWith(“--”) returns true for “/*” in case mi and mi-NZ culture[/-] [+]string.StartsWith(“--”) returns true for “/*” in case of mi and mi-NZ culture[/+] on Oct 23, 2020
  4. GrabYourPitchforks commented on Oct 23, 2020

    @GrabYourPitchforks
    Member

    @tarekgh @safern @krwq - behavior seems correct to me. See NLS-based example below. Thoughts?

    PS C:\Users\levib> $ci = [System.Globalization.CultureInfo]::GetCultureInfo("mi-NZ")
    PS C:\Users\levib> $ci
    
    LCID             Name             DisplayName
    ----             ----             -----------
    1153             mi-NZ            Maori (New Zealand)
    
    
    PS C:\Users\levib> $ci.CompareInfo.IsPrefix("/*", "--")
    True

    Edit: See also Paul's response on the internal SO instance. Methods like StartsWith and IndexOf are linguistic-aware by default, where Equals is Ordinal by default. Enabling analyzer rules CA1307 and CA1309 within the consuming application would have flagged these call sites.

  5. soocho-MSFT commented on Oct 23, 2020

    @soocho-MSFT
    Author

    The results are same after updating the .Equals() to use StringComparison.CurrentCulture.
    Do you know why .StartsWith() or .IsPrefix() returns different result from .Compare() or .Equals()?

  6. tarekgh commented on Oct 23, 2020

    @tarekgh
    Member

    @GrabYourPitchforks is correct. if you do:

    ❯ $ci.CompareInfo.GetSortKey("/*").KeyData
    7
    53
    7
    45
    1
    1
    1
    1
    0
    
    
    ❯ $ci.CompareInfo.GetSortKey("--").KeyData
    1
    1
    1
    1
    0

    means "--" for this culture has zero sort weight which will be part of any string.

    .Compare() and .Equals() returns different result.

    because it is 2 different things. Equals does ordinal comparison while Compare does linguistic comparison. if you want consistency, just pass StringComparison.Ordinal to compare.

  7. added
    question
    and removed
    untriaged
    on Oct 23, 2020
  8. GrabYourPitchforks commented on Oct 23, 2020

    @GrabYourPitchforks
    Member

    @soocho-MSFT Long story short is that .NET got its start as a GUI framework, so many of the very early string behaviors are based on the belief that linguistic-aware behavior is the correct default, as application flow would be driven by a human. There are some inconsistencies here, as you've seen. But changing the defaults now would be significantly breaking, and I don't think there's the appetite to do that. I think the best course of action would be to enable these Roslyn analyzer rules (or their FxCop equivalents) in your code, which should alert you to any similar problems.

  9. soocho-MSFT commented on Oct 23, 2020

    @soocho-MSFT
    Author

    @GrabYourPitchforks .Compare() and .Equals() are returning same result here, but the behavior differs between .IsPrefix() (.StartsWith()) and .Compare(). Just wanted to understand where the difference comes from.

  10. soocho-MSFT commented on Oct 23, 2020

    @soocho-MSFT
    Author

    @tarekgh @GrabYourPitchforks To close this, in mi or mi-NZ culture, "--" is treated as an empty string. Is this statement correct?

  11. GrabYourPitchforks commented on Oct 23, 2020

    @GrabYourPitchforks
    Member

    To close this, in mi or mi-NZ culture, "--" is treated as an empty string. Is this statement correct?

    Yes, according to Windows's data.

  12. Paul-Dempsey commented on Oct 23, 2020

    @Paul-Dempsey

    That's an assumption that the Windows data (and/or CLDR) is correct :-) I would generally expect the treatment of these characters to be consistent with en-NZ. Collation order often originates from the base region's phone book or bilbiographic ordering, and I would expect Mori and English to have been defined to collate sensibly in a mixed context. I haven't done the check to see if this is the case (guessing it is so).

    I understand the legacy foot-gun that the original design has handed users can't be changed. We could however document these aspects far better than we do. The documentation for these simply says:

    This method performs a word (case-sensitive and culture-sensitive) comparison using the current culture.

    but they DO NOT give adequate notice and explanation of the surprising and unintuitive consequences of that. For StartsWith and EndsWith, this can be particularly strange in this case because the content is all non-word characters.

    The documentation further leads users astray with poor code examples:

    Take for the example in the current StartsWith documentation. If you look at the code example, you can see that it could very well fall victim to this unintuitive behavior, very similar to the code in this thread:

       if (item.Trim().StartsWith("<"))

    This should be an Ordinal comparison in order to not fall victim to a collation that gives zero weight to '<', or collates it together with similar looking symbols such as '<' (U+003C).

  13. GrabYourPitchforks commented on Oct 23, 2020

    @GrabYourPitchforks
    Member

    @Paul-Dempsey Those are good points. I already offline pointed OP at the appropriate internal contacts for double-checking whether the Windows globalization data is correct. If the data is incorrect and Windows issues an update, .NET will pick up the fixes automatically. (We're just a thin wrapper around the OS.)

    There's an internal discussion forming on how we can improve developer education and the developer experience here. One of the things that we highlighted was that there's probably a bunch of sample code that follows bad patterns. I'm a little sad - though honestly not at all surprised - that you managed to find an example so quickly. :) It's good evidence that we need a multi-pronged approach to this: one that spans tutorials, documentation, and compile-time checks.

  14. ghost locked as resolved and limited conversation to collaborators on Dec 6, 2020
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.Globalizationquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions