PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/GitCommands/System.cs

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