CaseInsensitiveHashCodeProvider.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // System.Collections.CaseInsensitiveHashCodeProvider
  3. //
  4. // Author:
  5. // Sergey Chaban ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Globalization;
  10. namespace System.Collections {
  11. [Serializable]
  12. public class CaseInsensitiveHashCodeProvider : IHashCodeProvider {
  13. private static CaseInsensitiveHashCodeProvider singleton;
  14. // Class constructor
  15. static CaseInsensitiveHashCodeProvider ()
  16. {
  17. singleton=new CaseInsensitiveHashCodeProvider ();
  18. }
  19. // Public instance constructor
  20. public CaseInsensitiveHashCodeProvider ()
  21. {
  22. }
  23. [MonoTODO]
  24. public CaseInsensitiveHashCodeProvider (CultureInfo culture)
  25. {
  26. throw new NotImplementedException ();
  27. }
  28. //
  29. // Public static properties
  30. //
  31. public static CaseInsensitiveHashCodeProvider Default {
  32. get {
  33. return singleton;
  34. }
  35. }
  36. //
  37. // Instance methods
  38. //
  39. //
  40. // IHashCodeProvider
  41. //
  42. public int GetHashCode (object obj)
  43. {
  44. if (obj == null) {
  45. throw new ArgumentNullException ("obj is null");
  46. }
  47. string str = obj as string;
  48. if (str == null)
  49. return obj.GetHashCode ();
  50. int h = 0;
  51. char c;
  52. if (str.Length > 0) {
  53. for (int i = 0;i<str.Length;i++) {
  54. c = str [i];
  55. if (Char.IsLetter (c))
  56. c = Char.ToLower (c);
  57. h = h * 31 + c;
  58. }
  59. }
  60. return h;
  61. }
  62. } // CaseInsensitiveHashCodeProvider
  63. }