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

/GitCommands/System.cs

https://github.com/eisnerd/gitextensions
C# | 141 lines | 100 code | 23 blank | 18 comment | 19 complexity | 746621c6bbd72dff7a49a29be522ee02 MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0
  1. using System.Linq;
  2. using JetBrains.Annotations;
  3. namespace System
  4. {
  5. public class Tuple<T1, T2>
  6. {
  7. public Tuple(T1 item1, T2 item2)
  8. {
  9. Item1 = item1;
  10. Item2 = item2;
  11. }
  12. public T1 Item1 { get; private set; }
  13. public T2 Item2 { get; private set; }
  14. }
  15. public static class Tuple
  16. {
  17. public static Tuple<T1, T2> Create<T1, T2>(T1 item1, T2 item2)
  18. {
  19. return new Tuple<T1, T2>(item1, item2);
  20. }
  21. }
  22. public static class StringExtensions
  23. {
  24. public static string SkipStr(this string str, string toSkip)
  25. {
  26. if (str == null)
  27. return null;
  28. int idx;
  29. idx = str.IndexOf(toSkip);
  30. if (idx != -1)
  31. return str.Substring(idx + toSkip.Length);
  32. else
  33. return null;
  34. }
  35. public static String TakeUntilStr(this string str, String untilStr)
  36. {
  37. if (str == null)
  38. return null;
  39. int idx;
  40. idx = str.IndexOf(untilStr);
  41. if (idx != -1)
  42. return str.Substring(0, idx);
  43. else
  44. return str;
  45. }
  46. public static bool IsNullOrEmpty(this string s)
  47. {
  48. return string.IsNullOrEmpty(s);
  49. }
  50. public static string Join(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. /// Indicates whether a specified string is null, empty, or consists only of white-space characters.
  71. /// </summary>
  72. /// <param name="value">The string to test.</param>
  73. /// <remarks>
  74. /// This method is copied from .Net Framework 4.0 and should be deleted after leaving 3.5.
  75. /// </remarks>
  76. /// <returns>
  77. /// true if the value parameter is null or <see cref="string.Empty"/>, or if value consists exclusively of white-space characters.
  78. /// </returns>
  79. [Pure]
  80. public static bool IsNullOrWhiteSpace([CanBeNull] this string value)
  81. {
  82. return value == null || value.All(Char.IsWhiteSpace);
  83. }
  84. /// <summary>
  85. /// Determines whether the beginning of this instance matches any of the specified strings.
  86. /// </summary>
  87. /// <param name="value"></param>
  88. /// <param name="starts">array of strings to compare</param>
  89. /// <returns>true if any starts element matches the beginning of this string; otherwise, false.</returns>
  90. public static bool StartsWithAny([CanBeNull] this string value, string[] starts)
  91. {
  92. return value != null && starts.Any(s => value.StartsWith(s));
  93. }
  94. }
  95. public static class BoolExtensions
  96. {
  97. public static string AsForce(this bool force)
  98. {
  99. return force ? " -f " : string.Empty;
  100. }
  101. }
  102. public static class StreamExtensions
  103. {
  104. //copied from http://stackoverflow.com/a/1253049/1399492
  105. //it can be removed after move to .net 4
  106. public static void CopyTo(this System.IO.Stream src, System.IO.Stream dest)
  107. {
  108. int size = (src.CanSeek) ? Math.Min((int)(src.Length - src.Position), 0x2000) : 0x2000;
  109. byte[] buffer = new byte[size];
  110. int n;
  111. do
  112. {
  113. n = src.Read(buffer, 0, buffer.Length);
  114. dest.Write(buffer, 0, n);
  115. } while (n != 0);
  116. }
  117. }
  118. }