2 // System.Collections.CaseInsensitiveHashCodeProvider.cs
5 // Sergey Chaban (serge@wildwestsoftware.com)
6 // Andreas Nahr (ClassDevelopment@A-SoftTech.com)
7 // Sebastien Pouliot <sebastien@ximian.com>
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Collections
39 [Obsolete ("Please use StringComparer instead.")]
46 class CaseInsensitiveHashCodeProvider : IHashCodeProvider
48 static readonly CaseInsensitiveHashCodeProvider singletonInvariant = new CaseInsensitiveHashCodeProvider (
49 CultureInfo.InvariantCulture);
50 static CaseInsensitiveHashCodeProvider singleton;
51 static readonly object sync = new object ();
53 TextInfo m_text; // must match MS name for serialization
55 // Public instance constructor
56 public CaseInsensitiveHashCodeProvider ()
58 CultureInfo culture = CultureInfo.CurrentCulture;
59 if (culture.LCID != CultureInfo.InvariantCulture.LCID)
60 m_text = CultureInfo.CurrentCulture.TextInfo;
63 public CaseInsensitiveHashCodeProvider (CultureInfo culture)
66 throw new ArgumentNullException ("culture");
67 if (culture.LCID != CultureInfo.InvariantCulture.LCID)
68 m_text = culture.TextInfo;
72 // Public static properties
75 public static CaseInsensitiveHashCodeProvider Default {
77 // MS actually constructs a new instance on each call, for
78 // performance reasons we're only constructing a new instance
79 // if the CurrentCulture changes
81 if (singleton == null) {
82 singleton = new CaseInsensitiveHashCodeProvider ();
83 } else if (singleton.m_text == null) {
84 if (CultureInfo.CurrentCulture.LCID != CultureInfo.InvariantCulture.LCID)
85 singleton = new CaseInsensitiveHashCodeProvider ();
86 } else if (singleton.m_text.LCID != CultureInfo.CurrentCulture.LCID) {
87 singleton = new CaseInsensitiveHashCodeProvider ();
99 static CaseInsensitiveHashCodeProvider DefaultInvariant {
101 return singletonInvariant;
109 public int GetHashCode (object obj)
112 throw new ArgumentNullException ("obj");
114 string str = obj as string;
117 return obj.GetHashCode ();
122 if ((m_text != null) && (m_text.LCID != CultureInfo.InvariantCulture.LCID)) {
123 str = m_text.ToLower (str);
124 for (int i = 0; i < str.Length; i++) {
129 for (int i = 0; i < str.Length; i++) {
130 c = Char.ToLowerInvariant (str [i]);