CaseInsensitiveHashCodeProvider.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // System.Collections.CaseInsensitiveHashCodeProvider.cs
  3. //
  4. // Authors:
  5. // Sergey Chaban ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Sebastien Pouliot <[email protected]>
  8. //
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Globalization;
  31. using System.Runtime.InteropServices;
  32. namespace System.Collections
  33. {
  34. [Serializable]
  35. [ComVisible(true)]
  36. [Obsolete ("Please use StringComparer instead.")]
  37. #if INSIDE_CORLIB
  38. public
  39. #else
  40. internal
  41. #endif
  42. class CaseInsensitiveHashCodeProvider : IHashCodeProvider
  43. {
  44. static readonly CaseInsensitiveHashCodeProvider singletonInvariant = new CaseInsensitiveHashCodeProvider (
  45. CultureInfo.InvariantCulture);
  46. static CaseInsensitiveHashCodeProvider singleton;
  47. static readonly object sync = new object ();
  48. TextInfo m_text; // must match MS name for serialization
  49. // Public instance constructor
  50. public CaseInsensitiveHashCodeProvider ()
  51. {
  52. CultureInfo culture = CultureInfo.CurrentCulture;
  53. if (!AreEqual (culture, CultureInfo.InvariantCulture))
  54. m_text = CultureInfo.CurrentCulture.TextInfo;
  55. }
  56. public CaseInsensitiveHashCodeProvider (CultureInfo culture)
  57. {
  58. if (culture == null)
  59. throw new ArgumentNullException ("culture");
  60. if (!AreEqual (culture, CultureInfo.InvariantCulture))
  61. m_text = culture.TextInfo;
  62. }
  63. //
  64. // Public static properties
  65. //
  66. public static CaseInsensitiveHashCodeProvider Default {
  67. get {
  68. // MS actually constructs a new instance on each call, for
  69. // performance reasons we're only constructing a new instance
  70. // if the CurrentCulture changes
  71. lock (sync) {
  72. if (singleton == null) {
  73. singleton = new CaseInsensitiveHashCodeProvider ();
  74. } else if (singleton.m_text == null) {
  75. if (!AreEqual (CultureInfo.CurrentCulture, CultureInfo.InvariantCulture))
  76. singleton = new CaseInsensitiveHashCodeProvider ();
  77. } else if (!AreEqual (singleton.m_text, CultureInfo.CurrentCulture)) {
  78. singleton = new CaseInsensitiveHashCodeProvider ();
  79. }
  80. return singleton;
  81. }
  82. }
  83. }
  84. static bool AreEqual (CultureInfo a, CultureInfo b)
  85. {
  86. #if !NET_2_1
  87. return a.LCID == b.LCID;
  88. #else
  89. return a.Name == b.Name;
  90. #endif
  91. }
  92. static bool AreEqual (TextInfo info, CultureInfo culture)
  93. {
  94. #if !NET_2_1
  95. return info.LCID == culture.LCID;
  96. #else
  97. return info.CultureName == culture.Name;
  98. #endif
  99. }
  100. public
  101. static CaseInsensitiveHashCodeProvider DefaultInvariant {
  102. get {
  103. return singletonInvariant;
  104. }
  105. }
  106. //
  107. // IHashCodeProvider
  108. //
  109. public int GetHashCode (object obj)
  110. {
  111. if (obj == null)
  112. throw new ArgumentNullException ("obj");
  113. string str = obj as string;
  114. if (str == null)
  115. return obj.GetHashCode ();
  116. int h = 0;
  117. char c;
  118. if ((m_text != null) && !AreEqual (m_text, CultureInfo.InvariantCulture)) {
  119. str = m_text.ToLower (str);
  120. for (int i = 0; i < str.Length; i++) {
  121. c = str [i];
  122. h = h * 31 + c;
  123. }
  124. } else {
  125. for (int i = 0; i < str.Length; i++) {
  126. c = Char.ToLower (str [i], CultureInfo.InvariantCulture);
  127. h = h * 31 + c;
  128. }
  129. }
  130. return h;
  131. }
  132. }
  133. }