Created
June 14, 2023 10:51
-
-
Save ufcpp/071785157dfb8402af27b443427f8b90 to your computer and use it in GitHub Desktop.
ひどい。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Globalization; | |
using System.Text.RegularExpressions; | |
using static System.Console; | |
var s1 = "a\u0301"; // á = a + ́ | |
var s2 = "\u00e1"; // á | |
// たとえ InvariantCulture でも。 | |
// <InvariantGlobalization>false</InvariantGlobalization> な限り。 | |
// (↑true のときは、StringComparison.Default が Ordinal になる効果もあり。) | |
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture; | |
// false | |
WriteLine(s1 == s2); | |
WriteLine(s1.Equals(s2)); | |
WriteLine(EqualityComparer<string>.Default.Equals(s1, s2)); | |
WriteLine(Regex.Match(s1, s2).Success); | |
WriteLine(new HashSet<string> { s1 }.Contains(s2)); | |
WriteLine(new Dictionary<string, int> { { s1, 0 } }.ContainsKey(s2)); | |
// false (Span 版は Ordinal 比較) | |
WriteLine(s1.AsSpan().StartsWith(s2)); | |
WriteLine(s1.AsSpan().IndexOf(s2) == 0); | |
// ちなみに CompareTo は StringComparison 指定ありのオーバーロードしかない。 | |
WriteLine("ここまで false / ここから true"); | |
// CurrentCulture 明示はしょうがない。 | |
WriteLine(StringComparer.CurrentCulture.Equals(s1, s2)); | |
WriteLine(StringComparer.CurrentCulture.Compare(s1, s2) == 0); | |
// EqualityComparer と挙動違うの邪悪。 | |
WriteLine(Comparer<string>.Default.Compare(s1, s2) == 0); | |
// Dictionary = EqualityComparer 依存 | |
// SortedDictionary = Comparer 依存 | |
WriteLine(new SortedDictionary<string, int> { { s1, 0 } }.ContainsKey(s2)); | |
// ReadOnlySpan 版は「悔い改めた結果」。 | |
// 過去に遡及して修正はできず… | |
WriteLine(s1.CompareTo(s2) == 0); | |
WriteLine(s1.StartsWith(s2)); | |
WriteLine(s1.IndexOf(s2) == 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment