WebEqualComparer.cs 1.8 KB

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