{"id":921,"date":"2009-09-23T08:51:23","date_gmt":"2009-09-23T07:51:23","guid":{"rendered":"http:\/\/soci.hu\/blog\/?p=921"},"modified":"2009-09-23T09:06:59","modified_gmt":"2009-09-23T08:06:59","slug":"invariant-vs-ordinal-ujra","status":"publish","type":"post","link":"https:\/\/soci.hu\/blog\/index.php\/2009\/09\/23\/invariant-vs-ordinal-ujra\/","title":{"rendered":"INVARIANT vs. ORDINAL \u00fajra"},"content":{"rendered":"<p>A kor\u00e1bban \u00edrt <a href=\"http:\/\/soci.hu\/blog\/index.php\/2009\/09\/22\/comparison-confusion-invariant-vs-ordinal\/\">cikkhez<\/a> kapcsol\u00f3d\u00f3 anyag, ebb\u0151l m\u00e1r jobban \u00e1tj\u00f6n a k\u00fcl\u00f6nbs\u00e9g:<br \/>\n<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms973919.aspx\">New Recommendations for Using Strings in Microsoft .NET 2.0<\/a>.<\/p>\n<p>    *  DO: Use StringComparison.Ordinal or OrdinalIgnoreCase for comparisons as your safe default for culture-agnostic string matching.<br \/>\n    * DO: Use StringComparison.Ordinal and OrdinalIgnoreCase comparisons for increased speed.<br \/>\n    * DO: Use StringComparison.CurrentCulture-based string operations when displaying the output to the user.<br \/>\n    * DO: Switch current use of string operations based on the invariant culture to use the non-linguistic StringComparison.Ordinal or StringComparison.OrdinalIgnoreCase when the comparison is linguistically irrelevant (symbolic, for example).<br \/>\n    * DO: Use ToUpperInvariant rather than ToLowerInvariant when normalizing strings for comparison.<br \/>\n    * DON&#8217;T: Use overloads for string operations that don&#8217;t explicitly or implicitly specify the string comparison mechanism.<br \/>\n    * DON&#8217;T: Use StringComparison.InvariantCulture-based string operations in most cases; one of the few exceptions would be persisting linguistically meaningful but culturally-agnostic data.<\/p>\n<p>Ebb\u0151l az j\u00f6n le, hogy haszn\u00e1ld vagy az ordinalt, vagy a &#8220;kultur\u00e1lt&#8221; megold\u00e1st, de ha lehet ne az invariantot.<\/p>\n<p># Ordinal comparisons are string comparisons in which each byte of each string is compared without linguistic interpretation. This is essentially a C runtime strcmp. Thus, &#8220;windows&#8221; would not match &#8220;Windows.&#8221; Where the context dictates that strings should be matched exactly, or demands conservative matching policy, this comparison should be used. Additionally, ordinal comparisons are the fastest because they apply no linguistic rules when determining a result.<\/p>\n<p># Case insensitive ordinal comparisons are the next most conservative, and ignore most casing. Thus, &#8220;windows&#8221; would match &#8220;Windows.&#8221; When dealing with ASCII characters, this policy is equivalent to that of StringComparison.Ordinal, but with the usual ASCII casing ignored. Thus, any character in [A, Z] (\\u0041-\\u005A) matches the corresponding one in [a,z] (\\u0061-\\007A). Casing outside the ASCII range uses the invariant culture&#8217;s tables&#8230;<\/p>\n<p>Oridinal, case sensitive bin\u00e1ris \u00f6sszehasonl\u00edt\u00e1s, gyors, ok.<br \/>\nOridinal, case insensitive ascii karakterek eset\u00e9n egyenl\u0151nek tekinti a kis-nagybet\u0171ket, m\u00e1s karakterek eset\u00e9n \u00e1tv\u00e1lt az invariantra. Praktikusan szerintem ez azt jelenti, hogy ordinal case insensitive == invariant case insensitive.<br \/>\nUpdate: Jogos az el\u0151bbi, mert: &#8220;Comparisons made using OrdinalIgnoreCase are behaviorally the composition of two calls: calling ToUpperInvariant on both string arguments, and doing an Ordinal comparison.&#8221;<\/p>\n<p>K\u00f6zelebbr\u0151l:<\/p>\n<p>using System;<\/p>\n<p>class Program<br \/>\n{<br \/>\n    static void Main(string[] args)<br \/>\n    {<br \/>\n        string kisalma = &#8220;alma&#8221;;<br \/>\n        string nagyalma = &#8220;Alma&#8221;;<br \/>\n        Compare(kisalma, nagyalma);<br \/>\n        Console.WriteLine();<br \/>\n        kisalma = &#8220;\u0151lma&#8221;;<br \/>\n        nagyalma = &#8220;\u0150lma&#8221;;<br \/>\n        Compare(kisalma, nagyalma);<br \/>\n    }<\/p>\n<p>    private static void Compare(string a, string b)<br \/>\n    {<br \/>\n        Test(a, b, StringComparison.CurrentCulture);<br \/>\n        Test(a, b, StringComparison.CurrentCultureIgnoreCase);<br \/>\n        Test(a, b, StringComparison.InvariantCulture);<br \/>\n        Test(a, b, StringComparison.InvariantCultureIgnoreCase);<br \/>\n        Test(a, b, StringComparison.Ordinal);<br \/>\n        Test(a, b, StringComparison.OrdinalIgnoreCase);<br \/>\n    }<\/p>\n<p>    private static void Test(string a, string b, StringComparison c)<br \/>\n    {<br \/>\n        int compRes = string.Compare(a, b, c);<\/p>\n<p>        Console.WriteLine(&#8220;{0}, {1} {2} {3}&#8221;, c, a,<br \/>\n            compRes == 0 ? &#8220;==&#8221; : (compRes < 0 ? \"<\": \">&#8220;),<br \/>\n            b);<br \/>\n    }<br \/>\n}<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nCurrentCulture, alma &lt; Alma\r\nCurrentCultureIgnoreCase, alma == Alma\r\nInvariantCulture, alma &lt; Alma\r\nInvariantCultureIgnoreCase, alma == Alma\r\nOrdinal, alma &gt; Alma\r\nOrdinalIgnoreCase, alma == Alma\r\n\r\nCurrentCulture, \u0151lma &lt; \u0150lma\r\nCurrentCultureIgnoreCase, \u0151lma == \u0150lma\r\nInvariantCulture, \u0151lma &lt; \u0150lma\r\nInvariantCultureIgnoreCase, \u0151lma == \u0150lma\r\nOrdinal, \u0151lma &gt; \u0150lma\r\nOrdinalIgnoreCase, \u0151lma == \u0150lma\r\n<\/pre>\n<p>Nem teljes a teszt, de l\u00e1that\u00f3, hogy az ordinal m\u00e1sk\u00e9nt sorrendez, mint az invariant, nem nyelvi szempontok szerint, ez\u00e9rt is nem val\u00f3 GUI-hoz, de tudja, hogy a kis \u0151-nek a nagy \u0150 a p\u00e1rja. Az invariant se el\u00e9g okos persze, mivel \u00e1ltal\u00e1nos, nem ismeri egy adott nyelv szab\u00e1lyait, pl.<\/p>\n<p>Thread.CurrentThread.CurrentCulture = new CultureInfo(&#8220;hu-hu&#8221;);<br \/>\na = &#8220;cukor&#8221;;<br \/>\nb = &#8220;csoki&#8221;;<br \/>\nCompare(a, b);<\/p>\n<p>CurrentCulture, cukor < csoki\nCurrentCultureIgnoreCase, cukor < csoki\nInvariantCulture, cukor > csoki<br \/>\nInvariantCultureIgnoreCase, cukor > csoki<br \/>\nOrdinal, cukor > csoki<br \/>\nOrdinalIgnoreCase, cukor > csoki<\/p>\n<p>A cukor ut\u00e1n van a csoki magyarban, mivel van cs\u00e9 bet\u0171nk. Az invariant \u00e9rtelemszer\u0171en nem tud err\u0151l, ez\u00e9rt a csokit c\u00e9, es, okinak olvassa, \u00e9s \u00edgy is rendez.<\/p>\n<p>Ezek ut\u00e1n egyet kell \u00e9rtsek a cikkel, haszn\u00e1ljuk kultur\u00e1lt kult\u00far\u00e1t nyelvi rendez\u00e9shez, f\u00e1jl el\u00e9r\u00e9si utakhoz, ojjektum nevekhez, stb. oridinalt, az invariantot meg felejts\u00fck el.<br \/>\nLegal\u00e1bbis ma \u00edgy l\u00e1tom. Comments welcome.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A kor\u00e1bban \u00edrt cikkhez kapcsol\u00f3d\u00f3 anyag, ebb\u0151l m\u00e1r jobban \u00e1tj\u00f6n a k\u00fcl\u00f6nbs\u00e9g: New Recommendations for Using Strings in Microsoft .NET 2.0. *&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,10,4],"tags":[],"class_list":["post-921","post","type-post","status-publish","format-standard","hentry","category-net","category-c","category-szakmai-elet"],"_links":{"self":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=921"}],"version-history":[{"count":2,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/921\/revisions"}],"predecessor-version":[{"id":923,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/posts\/921\/revisions\/923"}],"wp:attachment":[{"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/soci.hu\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}