PageRenderTime 57ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/GitCommands/System.cs

https://github.com/qgppl/gitextensions
C# | 236 lines | 151 code | 37 blank | 48 comment | 28 complexity | bdde8581261a944226d46498b7a30086 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. /// <summary>
  67. /// Quotes this string with the specified <paramref name="quotationMark"/>
  68. /// </summary>
  69. public static string Quote(this string s, string quotationMark)
  70. {
  71. if (s == null)
  72. return string.Empty;
  73. return quotationMark + s + quotationMark;
  74. }
  75. /// <summary>
  76. /// Quotes this string if it is not null and not empty
  77. /// </summary>
  78. /// <param name="s"></param>
  79. /// <returns></returns>
  80. public static string QuoteNE(this string s)
  81. {
  82. return s.IsNullOrEmpty() ? s : s.Quote("\"");
  83. }
  84. /// <summary>
  85. /// Adds parentheses if string is not null and not empty
  86. /// </summary>
  87. /// <param name="s"></param>
  88. /// <returns></returns>
  89. public static string AddParenthesesNE(this string s)
  90. {
  91. return s.IsNullOrEmpty() ? s : "(" + s + ")";
  92. }
  93. /// <summary>
  94. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  95. /// </summary>
  96. /// <param name="value">The string to test.</param>
  97. /// <remarks>
  98. /// This method is copied from .Net Framework 4.0 and should be deleted after leaving 3.5.
  99. /// </remarks>
  100. /// <returns>
  101. /// true if the value parameter is null or <see cref="string.Empty"/>, or if value consists exclusively of white-space characters.
  102. /// </returns>
  103. [Pure]
  104. public static bool IsNullOrWhiteSpace([CanBeNull] this string value)
  105. {
  106. return string.IsNullOrWhiteSpace(value);
  107. }
  108. /// <summary>Indicates whether the specified string is neither null, nor empty, nor has only whitespace.</summary>
  109. public static bool IsNotNullOrWhitespace([CanBeNull] this string value)
  110. {
  111. return !value.IsNullOrWhiteSpace();
  112. }
  113. /// <summary>
  114. /// Determines whether the beginning of this instance matches any of the specified strings.
  115. /// </summary>
  116. /// <param name="value"></param>
  117. /// <param name="starts">array of strings to compare</param>
  118. /// <returns>true if any starts element matches the beginning of this string; otherwise, false.</returns>
  119. public static bool StartsWithAny([CanBeNull] this string value, string[] starts)
  120. {
  121. return value != null && starts.Any(s => value.StartsWith(s));
  122. }
  123. public static string RemoveLines(this string value, Func<string, bool> shouldRemoveLine)
  124. {
  125. if (value.IsNullOrEmpty())
  126. return value;
  127. if (value[value.Length - 1] == '\n')
  128. value = value.Substring(0, value.Length - 1);
  129. StringBuilder sb = new StringBuilder();
  130. string[] lines = value.Split('\n');
  131. foreach (string line in lines)
  132. if (!shouldRemoveLine(line))
  133. sb.Append(line + '\n');
  134. return sb.ToString();
  135. }
  136. /// <summary>Split a string, removing empty entries, then trim whitespace.</summary>
  137. public static IEnumerable<string> SplitThenTrim(this string value, params string[] separator)
  138. {
  139. return value
  140. .Split(separator, StringSplitOptions.RemoveEmptyEntries)
  141. .Select(s => s.Trim());
  142. }
  143. /// <summary>Split a string, removing empty entries, then trim whitespace.</summary>
  144. public static IEnumerable<string> SplitThenTrim(this string value, params char[] separator)
  145. {
  146. return value
  147. .Split(separator, StringSplitOptions.RemoveEmptyEntries)
  148. .Select(s => s.Trim());
  149. }
  150. /// <summary>Split a string, delimited by line-breaks, excluding empty entries.</summary>
  151. public static string[] SplitLines(this string value)
  152. {
  153. return value.Split(NewLineSeparator, StringSplitOptions.RemoveEmptyEntries);
  154. }
  155. /// <summary>Split a string, delimited by line-breaks, excluding empty entries; then trim whitespace.</summary>
  156. public static IEnumerable<string> SplitLinesThenTrim(this string value)
  157. {
  158. return value.SplitThenTrim(NewLineSeparator);
  159. }
  160. /// <summary>Gets the text after the last separator.
  161. /// If NO separator OR ends with separator, returns the original value.</summary>
  162. public static string SubstringAfterLastSafe(this string value, string separator)
  163. {// ex: "origin/master" -> "master"
  164. if (value.EndsWith(separator) || !value.Contains(separator))
  165. {// "origin/master/" OR "master" -> return original
  166. return value;
  167. }
  168. return value.Substring(1 + value.LastIndexOf(separator, StringComparison.InvariantCultureIgnoreCase));
  169. }
  170. public static string SubstringAfterFirst(this string value, string separator)
  171. {
  172. return value.Substring(1 + value.IndexOf(separator, StringComparison.InvariantCultureIgnoreCase));
  173. }
  174. /// <summary>
  175. /// Shortens this string, that it will be no longer than the specified <paramref name="maxLength"/>.
  176. /// If this string is longer than the specified <paramref name="maxLength"/>, it'll be truncated to the length of <paramref name="maxLength"/>-3
  177. /// and the "..." will be appended to the end of the resulting string.
  178. /// </summary>
  179. public static string ShortenTo(this string str, int maxLength)
  180. {
  181. if (str.IsNullOrEmpty())
  182. return string.Empty;
  183. if (str.Length <= maxLength)
  184. return str;
  185. else
  186. return str.Substring(0, maxLength - 3) + "...";
  187. }
  188. }
  189. public static class BoolExtensions
  190. {
  191. /// <summary>
  192. /// Translates this bool value to the git command line force flag
  193. /// </summary>
  194. public static string AsForce(this bool force)
  195. {
  196. return force ? " -f " : string.Empty;
  197. }
  198. }
  199. }