You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
string.StartsWith(“--”) returns true for “/*” in case of mi and mi-NZ culture #43772
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
@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.
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()?
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.
@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.
@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.
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).
@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.
ghost locked as resolved and limited conversation to collaborators on Dec 6, 2020
Activity
Dotnet-GitSync-Bot commentedon Oct 23, 2020
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.
[-]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[/+]Paul-Dempsey commentedon Oct 23, 2020
For reference: https://stackoverflow.microsoft.com/questions/228673
GrabYourPitchforks commentedon Oct 23, 2020
@tarekgh @safern @krwq - behavior seems correct to me. See NLS-based example below. Thoughts?
Edit: See also Paul's response on the internal SO instance. Methods like
StartsWithandIndexOfare linguistic-aware by default, whereEqualsis Ordinal by default. Enabling analyzer rules CA1307 and CA1309 within the consuming application would have flagged these call sites.soocho-MSFT commentedon Oct 23, 2020
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()?
tarekgh commentedon Oct 23, 2020
@GrabYourPitchforks is correct. if you do:
means "--" for this culture has zero sort weight which will be part of any string.
because it is 2 different things.
Equalsdoes ordinal comparison whileComparedoes linguistic comparison. if you want consistency, just pass StringComparison.Ordinal to compare.GrabYourPitchforks commentedon Oct 23, 2020
@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.
soocho-MSFT commentedon Oct 23, 2020
@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.
soocho-MSFT commentedon Oct 23, 2020
@tarekgh @GrabYourPitchforks To close this, in mi or mi-NZ culture, "--" is treated as an empty string. Is this statement correct?
GrabYourPitchforks commentedon Oct 23, 2020
Yes, according to Windows's data.
Paul-Dempsey commentedon Oct 23, 2020
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:
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:
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).
GrabYourPitchforks commentedon Oct 23, 2020
@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.