Extensions.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Globalization;
  5. using System.Net;
  6. namespace Renci.SshNet.Common
  7. {
  8. /// <summary>
  9. /// Collection of different extension method
  10. /// </summary>
  11. internal static partial class Extensions
  12. {
  13. /// <summary>
  14. /// Checks whether a collection is the same as another collection
  15. /// </summary>
  16. /// <param name="value">The current instance object</param>
  17. /// <param name="compareList">The collection to compare with</param>
  18. /// <param name="comparer">The comparer object to use to compare each item in the collection. If null uses EqualityComparer(T).Default</param>
  19. /// <returns>True if the two collections contain all the same items in the same order</returns>
  20. internal static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList, IEqualityComparer<TSource> comparer)
  21. {
  22. if (value == compareList)
  23. return true;
  24. if (value == null || compareList == null)
  25. return false;
  26. if (comparer == null)
  27. {
  28. comparer = EqualityComparer<TSource>.Default;
  29. }
  30. var enumerator1 = value.GetEnumerator();
  31. var enumerator2 = compareList.GetEnumerator();
  32. var enum1HasValue = enumerator1.MoveNext();
  33. var enum2HasValue = enumerator2.MoveNext();
  34. try
  35. {
  36. while (enum1HasValue && enum2HasValue)
  37. {
  38. if (!comparer.Equals(enumerator1.Current, enumerator2.Current))
  39. {
  40. return false;
  41. }
  42. enum1HasValue = enumerator1.MoveNext();
  43. enum2HasValue = enumerator2.MoveNext();
  44. }
  45. return !(enum1HasValue || enum2HasValue);
  46. }
  47. finally
  48. {
  49. enumerator1.Dispose();
  50. enumerator2.Dispose();
  51. }
  52. }
  53. /// <summary>
  54. /// Checks whether a collection is the same as another collection
  55. /// </summary>
  56. /// <param name="value">The current instance object</param>
  57. /// <param name="compareList">The collection to compare with</param>
  58. /// <returns>True if the two collections contain all the same items in the same order</returns>
  59. internal static bool IsEqualTo<TSource>(this IEnumerable<TSource> value, IEnumerable<TSource> compareList)
  60. {
  61. return IsEqualTo(value, compareList, null);
  62. }
  63. #if SILVERLIGHT
  64. #else
  65. /// <summary>
  66. /// Prints out
  67. /// </summary>
  68. /// <param name="bytes">The bytes.</param>
  69. internal static void DebugPrint(this IEnumerable<byte> bytes)
  70. {
  71. foreach (var b in bytes)
  72. {
  73. Debug.Write(string.Format(CultureInfo.CurrentCulture, "0x{0:x2}, ", b));
  74. }
  75. Debug.WriteLine(string.Empty);
  76. }
  77. #endif
  78. /// <summary>
  79. /// Trims the leading zero from bytes array.
  80. /// </summary>
  81. /// <param name="data">The data.</param>
  82. /// <returns>Data without leading zeros.</returns>
  83. internal static IEnumerable<byte> TrimLeadingZero(this IEnumerable<byte> data)
  84. {
  85. var leadingZero = true;
  86. foreach (var item in data)
  87. {
  88. if (item == 0 & leadingZero)
  89. {
  90. continue;
  91. }
  92. leadingZero = false;
  93. yield return item;
  94. }
  95. }
  96. /// <summary>
  97. /// Creates an instance of the specified type using that type's default constructor.
  98. /// </summary>
  99. /// <typeparam name="T">The type to create.</typeparam>
  100. /// <param name="type">Type of the instance to create.</param>
  101. /// <returns>A reference to the newly created object.</returns>
  102. internal static T CreateInstance<T>(this Type type) where T : class
  103. {
  104. if (type == null)
  105. return null;
  106. return Activator.CreateInstance(type) as T;
  107. }
  108. /// <summary>
  109. /// Returns the specified 16-bit unsigned integer value as an array of bytes.
  110. /// </summary>
  111. /// <param name="value">The number to convert.</param>
  112. /// <returns>An array of bytes with length 2.</returns>
  113. internal static byte[] GetBytes(this UInt16 value)
  114. {
  115. return new[] {(byte) (value >> 8), (byte) (value & 0xFF)};
  116. }
  117. /// <summary>
  118. /// Returns the specified 32-bit unsigned integer value as an array of bytes.
  119. /// </summary>
  120. /// <param name="value">The number to convert.</param>
  121. /// <returns>An array of bytes with length 4.</returns>
  122. internal static byte[] GetBytes(this UInt32 value)
  123. {
  124. return new[] {(byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) (value & 0xFF)};
  125. }
  126. /// <summary>
  127. /// Returns the specified 64-bit unsigned integer value as an array of bytes.
  128. /// </summary>
  129. /// <param name="value">The number to convert.</param>
  130. /// <returns>An array of bytes with length 8.</returns>
  131. internal static byte[] GetBytes(this UInt64 value)
  132. {
  133. return new[]
  134. {
  135. (byte) (value >> 56), (byte) (value >> 48), (byte) (value >> 40), (byte) (value >> 32),
  136. (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) (value & 0xFF)
  137. };
  138. }
  139. /// <summary>
  140. /// Returns the specified 64-bit signed integer value as an array of bytes.
  141. /// </summary>
  142. /// <param name="value">The number to convert.</param>
  143. /// <returns>An array of bytes with length 8.</returns>
  144. internal static byte[] GetBytes(this Int64 value)
  145. {
  146. return new[]
  147. {
  148. (byte) (value >> 56), (byte) (value >> 48), (byte) (value >> 40), (byte) (value >> 32),
  149. (byte) (value >> 24), (byte) (value >> 16), (byte) (value >> 8), (byte) (value & 0xFF)
  150. };
  151. }
  152. internal static void ValidatePort(this uint value, string argument)
  153. {
  154. if (value > IPEndPoint.MaxPort)
  155. throw new ArgumentOutOfRangeException(argument,
  156. string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.",
  157. IPEndPoint.MaxPort));
  158. }
  159. internal static void ValidatePort(this int value, string argument)
  160. {
  161. if (value < IPEndPoint.MinPort)
  162. throw new ArgumentOutOfRangeException(argument,
  163. string.Format(CultureInfo.InvariantCulture, "Specified value cannot be less than {0}.",
  164. IPEndPoint.MinPort));
  165. if (value > IPEndPoint.MaxPort)
  166. throw new ArgumentOutOfRangeException(argument,
  167. string.Format(CultureInfo.InvariantCulture, "Specified value cannot be greater than {0}.",
  168. IPEndPoint.MaxPort));
  169. }
  170. }
  171. }