CaseInsensitiveHashCodeProvider.cs 1.4 KB

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