| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- //
- // System.Collections.CaseInsensitiveHashCodeProvider
- //
- // Author:
- // Sergey Chaban ([email protected])
- //
- using System;
- using System.Collections;
- using System.Globalization;
- namespace System.Collections {
- [Serializable]
- public class CaseInsensitiveHashCodeProvider : IHashCodeProvider {
- private static CaseInsensitiveHashCodeProvider singleton;
- private static CaseInsensitiveHashCodeProvider singletonInvariant;
-
- CultureInfo culture;
- // Class constructor
- static CaseInsensitiveHashCodeProvider ()
- {
- singleton = new CaseInsensitiveHashCodeProvider ();
- singletonInvariant = new CaseInsensitiveHashCodeProvider (CultureInfo.InvariantCulture);
- }
- // Public instance constructor
- public CaseInsensitiveHashCodeProvider ()
- {
- }
- public CaseInsensitiveHashCodeProvider (CultureInfo culture)
- {
- this.culture = culture;
- }
- //
- // Public static properties
- //
- public static CaseInsensitiveHashCodeProvider Default {
- get {
- return singleton;
- }
- }
- #if NET_1_1
- public static CaseInsensitiveHashCodeProvider DefaultInvariant {
- get {
- return singletonInvariant;
- }
- }
- #endif
- //
- // Instance methods
- //
- //
- // IHashCodeProvider
- //
- public int GetHashCode (object obj)
- {
- if (obj == null) {
- throw new ArgumentNullException ("obj is null");
- }
- string str = obj as string;
- if (str == null)
- return obj.GetHashCode ();
- int h = 0;
- char c;
- int length = str.Length;
- if (culture != null) {
- for (int i = 0;i<length;i++) {
- c = Char.ToLower (str [i], culture);
- h = h * 31 + c;
- }
- }
- else {
- for (int i = 0;i<length;i++) {
- c = Char.ToLower (str [i]);
- h = h * 31 + c;
- }
- }
- return h;
- }
- } // CaseInsensitiveHashCodeProvider
- }
|