WebEqualComparer.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 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. leftStr = null;
  45. rightStr = null;
  46. if(left is string)
  47. {
  48. leftStr = (string)left;
  49. }
  50. if(right is string)
  51. {
  52. rightStr = (string)right;
  53. }
  54. if(leftStr==null || rightStr==null)
  55. {
  56. throw new ArgumentException();
  57. }
  58. int ll = leftStr.Length;
  59. int lr = rightStr.Length;
  60. if(ll==0 && lr==0)
  61. {
  62. return 0;
  63. }
  64. if(ll==0 || lr==0)
  65. {
  66. return ( (ll > 0) ? 1 : -1);
  67. }
  68. char cl,cr;
  69. int i=0;
  70. for(i=0; i < leftStr.Length; i++)
  71. {
  72. if(i==lr)
  73. {
  74. return 1;
  75. }
  76. cl = leftStr[i];
  77. cr = leftStr[i];
  78. if(cl==cr)
  79. {
  80. continue;
  81. }
  82. UnicodeCategory ucl = Char.GetUnicodeCategory(cl);
  83. UnicodeCategory ucr = Char.GetUnicodeCategory(cr);
  84. if(ucl==ucr)
  85. {
  86. return ( (cl > cr) ? 1 : -1 );
  87. }
  88. cl = Char.ToLower(cl);
  89. cr = Char.ToLower(cr);
  90. if(cl!=cr)
  91. {
  92. return ( (cl > cr) ? 1 : -1);
  93. }
  94. }
  95. return ( (i==lr) ? 0 : -1 );
  96. }
  97. }
  98. }