5 ms·
Nice improvement. I think one of the mistakes in .NET was to not have ordinal string comparison as the default for all string operations. Sometimes people forge
by AtNightWeCode 5y ago
Nice improvement. I think one of the mistakes in .NET was to not have ordinal string comparison as the default for all string operations. Sometimes people forget it and it causes security and performance issues.
| Method | Mean | Error | StdDev |
|--------------- |----------:|---------:|---------:|
| IndexOfOrdinal | 47.71 ns | 0.963 ns | 2.344 ns |
| IndexOfDefault | 201.93 ns | 3.893 ns | 4.923 ns |
- afdsafsfasd 5y agoWhat is an "ordinal string comparison"? To contrast, what is a non-ordinal string comparison?
- ChrisSD 5y agoA string is encoded in memory as an array of numbers so in this case "ordinal" means that it simply compares the two arrays number by number. A non-ordinal string comparison may take into account other things such as iterating by actual characters and comparing them in language and culture-aware ways. This is useful for ordering things by name when showing a list to users.
- Thiez 5y agoOrdinal compares the literal bytes. The other comparison variants have additional features, e.g. case-insensitivity.
- deleted 5y ago[deleted]
- AtNightWeCode 5y agoOrdinal is plain binary comparison. The default comparison takes language specifics into account. For instance, in German there is a letter called Eszett (ß) that stands for "ss". IndexOf("ss") will find that.
- dgellow 5y agoIn Germany's German "ss" and "ß" are two different things, ß is its own letter and its transliteration is "ss" or "sz". But it really is its own thing and is not equivalent to "ss". In the past "SS" was the capitalized version of "ß", but that's not officially the case anymore, you now have a capitalized ẞ [0], even if it isn't used in practice (or if it is, really not often). It's not the same for all versions of German though. For example in Swiss German ß is not used and is replaced by "ss". I do not know about Austrian German but I would not be surprised if they have some specificities around the letter ß. In short: it's complicated :) [0]: https://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F https://de.wikipedia.org/wiki/Gro%C3%9Fes_%C3%9F
- Sammi 5y agoSuch a Anglo centrist view. Rest of the world be damned? Luckily a not all the .Net creators were English speakers.
- ygra 5y agoIt's probably an unfortunate decision either way: For interacting with things the user types the ordinal approach is wrong, but for low-level handling of text the ordinal approach is faster. In both cases you basically have to know your exact needs and choose the correct variant. You could have the parameter not being optional and thus force every user of that API to make that decision, but that is probably fairly quickly perceived as annoying.
- AtNightWeCode 5y agoOrdinal is the default comparison for most .NET string methods. Most linters will force you to make an active choice. And it is not only a performance thing since you open up for all these Unicode attacks.
- torginus 5y agoNot an Anglo. Still think it's dumb. Most of the strings I manipulate are either english, or not even natural languages. Having to write Ordinal for Regex and string comparisons everywhere sucks. Especially considering that if you don't, it will depend on the current thread's Culture setting, which can lead to odd bugs when you call code from different contexts.
- alkonaut 5y agoThe default comparison should be a culture neutral comparison. Just like the default number formatting unless specified should default to "NumberFormatInfo.InvariantInfo" rather than "Thread.CurrentThread.CurrentCulture". If I want to compare or print in my own language or the system language (I very rarely do) then I'm happy to specify that in code. And I wish the API forced me to, because I some times forget and I make code that breaks when it's run on a different machine. Which is almost never what I want.
- valleyjo 5y agoThe .Net analyzers have a feature to check for StringComparison use when available. I tuned it on for my team after a few bugs occurred from forgetting it.