StrUtils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // System.Web.Util.StrUtils
  3. //
  4. // Author(s):
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc, (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Globalization;
  31. using System.Text;
  32. namespace System.Web.Util {
  33. internal sealed class StrUtils
  34. {
  35. static CultureInfo invariant = CultureInfo.InvariantCulture;
  36. StrUtils () { }
  37. public static bool StartsWith (string str1, string str2)
  38. {
  39. return StartsWith (str1, str2, false);
  40. }
  41. public static bool StartsWith (string str1, string str2, bool ignore_case)
  42. {
  43. int l2 = str2.Length;
  44. if (l2 == 0)
  45. return true;
  46. int l1 = str1.Length;
  47. if (l2 > l1)
  48. return false;
  49. return (0 == String.Compare (str1, 0, str2, 0, l2, ignore_case, invariant));
  50. }
  51. public static bool EndsWith (string str1, string str2)
  52. {
  53. return EndsWith (str1, str2, false);
  54. }
  55. public static bool EndsWith (string str1, string str2, bool ignore_case)
  56. {
  57. int l2 = str2.Length;
  58. if (l2 == 0)
  59. return true;
  60. int l1 = str1.Length;
  61. if (l2 > l1)
  62. return false;
  63. return (0 == String.Compare (str1, l1 - l2, str2, 0, l2, ignore_case, invariant));
  64. }
  65. public static string EscapeQuotesAndBackslashes (string attributeValue)
  66. {
  67. StringBuilder sb = null;
  68. for (int i = 0; i < attributeValue.Length; i++) {
  69. char ch = attributeValue [i];
  70. if (ch == '\'' || ch == '"' || ch == '\\') {
  71. if (sb == null) {
  72. sb = new StringBuilder ();
  73. sb.Append (attributeValue.Substring (0, i));
  74. }
  75. sb.Append ('\\');
  76. sb.Append (ch);
  77. }
  78. else {
  79. if (sb != null)
  80. sb.Append (ch);
  81. }
  82. }
  83. if (sb != null)
  84. return sb.ToString ();
  85. return attributeValue;
  86. }
  87. public static bool IsNullOrEmpty (string value)
  88. {
  89. #if NET_2_0
  90. return String.IsNullOrEmpty (value);
  91. #else
  92. return value == null || value.Length == 0;
  93. #endif
  94. }
  95. public static string [] SplitRemoveEmptyEntries (string value, char [] separator)
  96. {
  97. #if NET_2_0
  98. return value.Split (separator, StringSplitOptions.RemoveEmptyEntries);
  99. #else
  100. string [] parts = value.Split (separator);
  101. int delta = 0;
  102. for (int i = 0; i < parts.Length; i++) {
  103. if (IsNullOrEmpty (parts [i])) {
  104. delta++;
  105. }
  106. else {
  107. if (delta > 0)
  108. parts [i - delta] = parts [i];
  109. }
  110. }
  111. if (delta == 0)
  112. return parts;
  113. string [] parts_copy = new string [parts.Length - delta];
  114. Array.Copy (parts, parts_copy, parts_copy.Length);
  115. return parts_copy;
  116. #endif
  117. }
  118. }
  119. }