StrUtils.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. static CultureInfo invariant = CultureInfo.InvariantCulture;
  35. private StrUtils () { }
  36. public static bool StartsWith (string str1, string str2)
  37. {
  38. return StartsWith (str1, str2, false);
  39. }
  40. public static bool StartsWith (string str1, string str2, bool ignore_case)
  41. {
  42. int l2 = str2.Length;
  43. if (l2 == 0)
  44. return true;
  45. int l1 = str1.Length;
  46. if (l2 > l1)
  47. return false;
  48. return (0 == String.Compare (str1, 0, str2, 0, l2, ignore_case, invariant));
  49. }
  50. public static bool EndsWith (string str1, string str2)
  51. {
  52. return EndsWith (str1, str2, false);
  53. }
  54. public static bool EndsWith (string str1, string str2, bool ignore_case)
  55. {
  56. int l2 = str2.Length;
  57. if (l2 == 0)
  58. return true;
  59. int l1 = str1.Length;
  60. if (l2 > l1)
  61. return false;
  62. return (0 == String.Compare (str1, l1 - l2, str2, 0, l2, ignore_case, invariant));
  63. }
  64. public static string EscapeQuotesAndBackslashes (string attributeValue)
  65. {
  66. StringBuilder sb = null;
  67. for (int i = 0; i < attributeValue.Length; i++) {
  68. char ch = attributeValue [i];
  69. if (ch == '\'' || ch == '"' || ch == '\\') {
  70. if (sb == null) {
  71. sb = new StringBuilder ();
  72. sb.Append (attributeValue.Substring (0, i));
  73. }
  74. sb.Append ('\\');
  75. sb.Append (ch);
  76. }
  77. else {
  78. if (sb != null)
  79. sb.Append (ch);
  80. }
  81. }
  82. if (sb != null)
  83. return sb.ToString ();
  84. return attributeValue;
  85. }
  86. public static bool IsNullOrEmpty (string value)
  87. {
  88. #if NET_2_0
  89. return String.IsNullOrEmpty (value);
  90. #else
  91. return value == null || value.Length == 0;
  92. #endif
  93. }
  94. public static string [] SplitRemoveEmptyEntries (string value, char [] separator)
  95. {
  96. #if NET_2_0
  97. return value.Split (separator, StringSplitOptions.RemoveEmptyEntries);
  98. #else
  99. string [] parts = value.Split (separator);
  100. int delta = 0;
  101. for (int i = 0; i < parts.Length; i++) {
  102. if (IsNullOrEmpty (parts [i])) {
  103. delta++;
  104. }
  105. else {
  106. if (delta > 0)
  107. parts [i - delta] = parts [i];
  108. }
  109. }
  110. if (delta == 0)
  111. return parts;
  112. string [] parts_copy = new string [parts.Length - delta];
  113. Array.Copy (parts, parts_copy, parts_copy.Length);
  114. return parts_copy;
  115. #endif
  116. }
  117. }
  118. }