PageRenderTime 44ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/GitCommands/System.cs

https://github.com/PKRoma/gitextensions
C# | 149 lines | 100 code | 28 blank | 21 comment | 26 complexity | d08919d2cc310463cb8ccec86f505763 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System.Linq;
  2. using System.Text;
  3. using JetBrains.Annotations;
  4. namespace System
  5. {
  6. public static class StringExtensions
  7. {
  8. public static string SkipStr(this string str, string toSkip)
  9. {
  10. if (str == null)
  11. return null;
  12. int idx;
  13. idx = str.IndexOf(toSkip);
  14. if (idx != -1)
  15. return str.Substring(idx + toSkip.Length);
  16. else
  17. return null;
  18. }
  19. public static String TakeUntilStr(this string str, String untilStr)
  20. {
  21. if (str == null)
  22. return null;
  23. int idx;
  24. idx = str.IndexOf(untilStr);
  25. if (idx != -1)
  26. return str.Substring(0, idx);
  27. else
  28. return str;
  29. }
  30. public static string CommonPrefix(this string s, string other)
  31. {
  32. if (s.IsNullOrEmpty() || other.IsNullOrEmpty())
  33. return string.Empty;
  34. int prefixLength = 0;
  35. foreach (char c in other)
  36. {
  37. if (s.Length <= prefixLength || s[prefixLength] != c)
  38. return s.Substring(0, prefixLength);
  39. prefixLength++;
  40. }
  41. return s;
  42. }
  43. public static bool IsNullOrEmpty(this string s)
  44. {
  45. return string.IsNullOrEmpty(s);
  46. }
  47. public static string Combine(this string left, string sep, string right)
  48. {
  49. if (left.IsNullOrEmpty())
  50. return right;
  51. else if (right.IsNullOrEmpty())
  52. return left;
  53. else
  54. return left + sep + right;
  55. }
  56. public static string Quote(this string s)
  57. {
  58. return s.Quote("\"");
  59. }
  60. public static string Quote(this string s, string quotationMark)
  61. {
  62. if (s == null)
  63. return string.Empty;
  64. return quotationMark + s + quotationMark;
  65. }
  66. /// <summary>
  67. /// Quotes string if it is not null and not empty
  68. /// </summary>
  69. /// <param name="s"></param>
  70. /// <returns></returns>
  71. public static string QuoteNE(this string s)
  72. {
  73. return s.IsNullOrEmpty() ? s : s.Quote("\"");
  74. }
  75. /// <summary>
  76. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  77. /// </summary>
  78. /// <param name="value">The string to test.</param>
  79. /// <remarks>
  80. /// This method is copied from .Net Framework 4.0 and should be deleted after leaving 3.5.
  81. /// </remarks>
  82. /// <returns>
  83. /// true if the value parameter is null or <see cref="string.Empty"/>, or if value consists exclusively of white-space characters.
  84. /// </returns>
  85. [Pure]
  86. public static bool IsNullOrWhiteSpace([CanBeNull] this string value)
  87. {
  88. return value == null || value.All(Char.IsWhiteSpace);
  89. }
  90. /// <summary>
  91. /// Determines whether the beginning of this instance matches any of the specified strings.
  92. /// </summary>
  93. /// <param name="value"></param>
  94. /// <param name="starts">array of strings to compare</param>
  95. /// <returns>true if any starts element matches the beginning of this string; otherwise, false.</returns>
  96. public static bool StartsWithAny([CanBeNull] this string value, string[] starts)
  97. {
  98. return value != null && starts.Any(s => value.StartsWith(s));
  99. }
  100. public static string RemoveLines(this string value, Func<string, bool> shouldRemoveLine)
  101. {
  102. if (value.IsNullOrEmpty())
  103. return value;
  104. if (value[value.Length - 1] == '\n')
  105. value = value.Substring(0, value.Length - 1);
  106. StringBuilder sb = new StringBuilder();
  107. string[] lines = value.Split('\n');
  108. foreach (string line in lines)
  109. if (!shouldRemoveLine(line))
  110. sb.Append(line + '\n');
  111. return sb.ToString();
  112. }
  113. }
  114. public static class BoolExtensions
  115. {
  116. public static string AsForce(this bool force)
  117. {
  118. return force ? " -f " : string.Empty;
  119. }
  120. }
  121. }