CaseInsensitiveHashCodeProvider.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #if NET_2_0
  36. [ComVisible(true)]
  37. [Obsolete ("Please use StringComparer instead.")]
  38. #endif
  39. #if INSIDE_CORLIB
  40. public
  41. #else
  42. internal
  43. #endif
  44. class CaseInsensitiveHashCodeProvider : IHashCodeProvider
  45. {
  46. static readonly CaseInsensitiveHashCodeProvider singletonInvariant = new CaseInsensitiveHashCodeProvider (
  47. CultureInfo.InvariantCulture);
  48. static CaseInsensitiveHashCodeProvider singleton;
  49. static readonly object sync = new object ();
  50. TextInfo m_text; // must match MS name for serialization
  51. // Public instance constructor
  52. public CaseInsensitiveHashCodeProvider ()
  53. {
  54. CultureInfo culture = CultureInfo.CurrentCulture;
  55. if (culture.LCID != CultureInfo.InvariantCulture.LCID)
  56. m_text = CultureInfo.CurrentCulture.TextInfo;
  57. }
  58. public CaseInsensitiveHashCodeProvider (CultureInfo culture)
  59. {
  60. if (culture == null)
  61. throw new ArgumentNullException ("culture");
  62. if (culture.LCID != CultureInfo.InvariantCulture.LCID)
  63. m_text = culture.TextInfo;
  64. }
  65. //
  66. // Public static properties
  67. //
  68. public static CaseInsensitiveHashCodeProvider Default {
  69. get {
  70. // MS actually constructs a new instance on each call, for
  71. // performance reasons we're only constructing a new instance
  72. // if the CurrentCulture changes
  73. lock (sync) {
  74. if (singleton == null) {
  75. singleton = new CaseInsensitiveHashCodeProvider ();
  76. } else if (singleton.m_text == null) {
  77. if (CultureInfo.CurrentCulture.LCID != CultureInfo.InvariantCulture.LCID)
  78. singleton = new CaseInsensitiveHashCodeProvider ();
  79. } else if (singleton.m_text.LCID != CultureInfo.CurrentCulture.LCID) {
  80. singleton = new CaseInsensitiveHashCodeProvider ();
  81. }
  82. return singleton;
  83. }
  84. }
  85. }
  86. #if NET_1_1
  87. public
  88. #else
  89. internal
  90. #endif
  91. static CaseInsensitiveHashCodeProvider DefaultInvariant {
  92. get {
  93. return singletonInvariant;
  94. }
  95. }
  96. //
  97. // IHashCodeProvider
  98. //
  99. public int GetHashCode (object obj)
  100. {
  101. if (obj == null)
  102. throw new ArgumentNullException ("obj");
  103. string str = obj as string;
  104. if (str == null)
  105. return obj.GetHashCode ();
  106. int h = 0;
  107. char c;
  108. if ((m_text != null) && (m_text.LCID != CultureInfo.InvariantCulture.LCID)) {
  109. str = m_text.ToLower (str);
  110. for (int i = 0; i < str.Length; i++) {
  111. c = str [i];
  112. h = h * 31 + c;
  113. }
  114. } else {
  115. for (int i = 0; i < str.Length; i++) {
  116. c = Char.ToLowerInvariant (str [i]);
  117. h = h * 31 + c;
  118. }
  119. }
  120. return h;
  121. }
  122. }
  123. }