WebEqualComparer.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // System.Web.Util.WebEqualComparer.cs
  3. //
  4. // Authors:
  5. // Gaurav Vaish ([email protected], [email protected])
  6. //
  7. // (c) Gaurav Vaish 2001
  8. //
  9. using System;
  10. using System.Globalization;
  11. using System.Collections;
  12. namespace System.Web.Util
  13. {
  14. internal class WebEqualComparer : IComparer
  15. {
  16. private static IComparer defC;
  17. public WebEqualComparer()
  18. {
  19. }
  20. public static IComparer Default
  21. {
  22. get
  23. {
  24. if(defC == null)
  25. {
  26. defC = new WebEqualComparer();
  27. }
  28. return defC;
  29. }
  30. }
  31. /// <summary>
  32. /// To compare two strings
  33. /// </summary>
  34. /// <remarks>
  35. /// Cannot apply String.Compare(..) since I am at web
  36. /// </remarks>
  37. int IComparer.Compare(object left, object right)
  38. {
  39. string leftStr, rightStr;
  40. leftStr = null;
  41. rightStr = null;
  42. if(left is string)
  43. {
  44. leftStr = (string)left;
  45. }
  46. if(right is string)
  47. {
  48. rightStr = (string)right;
  49. }
  50. if(leftStr==null || rightStr==null)
  51. {
  52. throw new ArgumentException();
  53. }
  54. int ll = leftStr.Length;
  55. int lr = rightStr.Length;
  56. if(ll==0 && lr==0)
  57. {
  58. return 0;
  59. }
  60. if(ll==0 || lr==0)
  61. {
  62. return ( (ll > 0) ? 1 : -1);
  63. }
  64. char cl,cr;
  65. int i=0;
  66. for(i=0; i < leftStr.Length; i++)
  67. {
  68. if(i==lr)
  69. {
  70. return 1;
  71. }
  72. cl = leftStr[i];
  73. cr = leftStr[i];
  74. if(cl==cr)
  75. {
  76. continue;
  77. }
  78. UnicodeCategory ucl = Char.GetUnicodeCategory(cl);
  79. UnicodeCategory ucr = Char.GetUnicodeCategory(cr);
  80. if(ucl==ucr)
  81. {
  82. return ( (cl > cr) ? 1 : -1 );
  83. }
  84. cl = Char.ToLower(cl);
  85. cr = Char.ToLower(cr);
  86. if(cl!=cr)
  87. {
  88. return ( (cl > cr) ? 1 : -1);
  89. }
  90. }
  91. return ( (i==lr) ? 0 : -1 );
  92. }
  93. }
  94. }