PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/GitCommands/System.cs

https://github.com/bugQ/gitextensions
C# | 214 lines | 142 code | 35 blank | 37 comment | 26 complexity | 6f8ec42a435dc23cb9dbb3b835530b3b MD5 | raw file
Possible License(s): GPL-3.0
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using JetBrains.Annotations;
  5. namespace System
  6. {
  7. public static class StringExtensions
  8. {
  9. /// <summary>'\n'</summary>
  10. static readonly char[] NewLineSeparator = new char[] { '\n' };
  11. public static string SkipStr(this string str, string toSkip)
  12. {
  13. if (str == null)
  14. return null;
  15. int idx;
  16. idx = str.IndexOf(toSkip);
  17. if (idx != -1)
  18. return str.Substring(idx + toSkip.Length);
  19. else
  20. return null;
  21. }
  22. public static String TakeUntilStr(this string str, String untilStr)
  23. {
  24. if (str == null)
  25. return null;
  26. int idx;
  27. idx = str.IndexOf(untilStr);
  28. if (idx != -1)
  29. return str.Substring(0, idx);
  30. else
  31. return str;
  32. }
  33. public static string CommonPrefix(this string s, string other)
  34. {
  35. if (s.IsNullOrEmpty() || other.IsNullOrEmpty())
  36. return string.Empty;
  37. int prefixLength = 0;
  38. foreach (char c in other)
  39. {
  40. if (s.Length <= prefixLength || s[prefixLength] != c)
  41. return s.Substring(0, prefixLength);
  42. prefixLength++;
  43. }
  44. return s;
  45. }
  46. public static bool IsNullOrEmpty(this string s)
  47. {
  48. return string.IsNullOrEmpty(s);
  49. }
  50. public static string Combine(this string left, string sep, string right)
  51. {
  52. if (left.IsNullOrEmpty())
  53. return right;
  54. else if (right.IsNullOrEmpty())
  55. return left;
  56. else
  57. return left + sep + right;
  58. }
  59. /// <summary>
  60. /// Quotes string if it is not null
  61. /// </summary>
  62. public static string Quote(this string s)
  63. {
  64. return s.Quote("\"");
  65. }
  66. public static string Quote(this string s, string quotationMark)
  67. {
  68. if (s == null)
  69. return string.Empty;
  70. return quotationMark + s + quotationMark;
  71. }
  72. /// <summary>
  73. /// Quotes string if it is not null and not empty
  74. /// </summary>
  75. /// <param name="s"></param>
  76. /// <returns></returns>
  77. public static string QuoteNE(this string s)
  78. {
  79. return s.IsNullOrEmpty() ? s : s.Quote("\"");
  80. }
  81. /// <summary>
  82. /// Adds parentheses if string is not null and not empty
  83. /// </summary>
  84. /// <param name="s"></param>
  85. /// <returns></returns>
  86. public static string AddParenthesesNE(this string s)
  87. {
  88. return s.IsNullOrEmpty() ? s : "(" + s + ")";
  89. }
  90. /// <summary>
  91. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  92. /// </summary>
  93. /// <param name="value">The string to test.</param>
  94. /// <remarks>
  95. /// This method is copied from .Net Framework 4.0 and should be deleted after leaving 3.5.
  96. /// </remarks>
  97. /// <returns>
  98. /// true if the value parameter is null or <see cref="string.Empty"/>, or if value consists exclusively of white-space characters.
  99. /// </returns>
  100. [Pure]
  101. public static bool IsNullOrWhiteSpace([CanBeNull] this string value)
  102. {
  103. return string.IsNullOrWhiteSpace(value);
  104. }
  105. /// <summary>Indicates whether the specified string is neither null, nor empty, nor has only whitespace.</summary>
  106. public static bool IsNotNullOrWhitespace([CanBeNull] this string value)
  107. {
  108. return !value.IsNullOrWhiteSpace();
  109. }
  110. /// <summary>
  111. /// Determines whether the beginning of this instance matches any of the specified strings.
  112. /// </summary>
  113. /// <param name="value"></param>
  114. /// <param name="starts">array of strings to compare</param>
  115. /// <returns>true if any starts element matches the beginning of this string; otherwise, false.</returns>
  116. public static bool StartsWithAny([CanBeNull] this string value, string[] starts)
  117. {
  118. return value != null && starts.Any(s => value.StartsWith(s));
  119. }
  120. public static string RemoveLines(this string value, Func<string, bool> shouldRemoveLine)
  121. {
  122. if (value.IsNullOrEmpty())
  123. return value;
  124. if (value[value.Length - 1] == '\n')
  125. value = value.Substring(0, value.Length - 1);
  126. StringBuilder sb = new StringBuilder();
  127. string[] lines = value.Split('\n');
  128. foreach (string line in lines)
  129. if (!shouldRemoveLine(line))
  130. sb.Append(line + '\n');
  131. return sb.ToString();
  132. }
  133. /// <summary>Split a string, removing empty entries, then trim whitespace.</summary>
  134. public static IEnumerable<string> SplitThenTrim(this string value, params string[] separator)
  135. {
  136. return value
  137. .Split(separator, StringSplitOptions.RemoveEmptyEntries)
  138. .Select(s => s.Trim());
  139. }
  140. /// <summary>Split a string, removing empty entries, then trim whitespace.</summary>
  141. public static IEnumerable<string> SplitThenTrim(this string value, params char[] separator)
  142. {
  143. return value
  144. .Split(separator, StringSplitOptions.RemoveEmptyEntries)
  145. .Select(s => s.Trim());
  146. }
  147. /// <summary>Split a string, delimited by line-breaks, excluding empty entries.</summary>
  148. public static string[] SplitLines(this string value)
  149. {
  150. return value.Split(NewLineSeparator);
  151. }
  152. /// <summary>Split a string, delimited by line-breaks, excluding empty entries; then trim whitespace.</summary>
  153. public static IEnumerable<string> SplitLinesThenTrim(this string value)
  154. {
  155. return value.SplitThenTrim(NewLineSeparator);
  156. }
  157. /// <summary>Gets the text after the last separator.
  158. /// If NO separator OR ends with separator, returns the original value.</summary>
  159. public static string SubstringAfterLastSafe(this string value, string separator)
  160. {// ex: "origin/master" -> "master"
  161. if (value.EndsWith(separator) || !value.Contains(separator))
  162. {// "origin/master/" OR "master" -> return original
  163. return value;
  164. }
  165. return value.Substring(1 + value.LastIndexOf(separator, StringComparison.InvariantCultureIgnoreCase));
  166. }
  167. public static string SubstringAfterFirst(this string value, string separator)
  168. {
  169. return value.Substring(1 + value.IndexOf(separator, StringComparison.InvariantCultureIgnoreCase));
  170. }
  171. }
  172. public static class BoolExtensions
  173. {
  174. public static string AsForce(this bool force)
  175. {
  176. return force ? " -f " : string.Empty;
  177. }
  178. }
  179. }