1
0

SkipWhile.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Linq
  8. {
  9. public static partial class AsyncEnumerable
  10. {
  11. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  12. {
  13. if (source == null)
  14. throw Error.ArgumentNull(nameof(source));
  15. if (predicate == null)
  16. throw Error.ArgumentNull(nameof(predicate));
  17. return Create(Core);
  18. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  19. {
  20. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  21. {
  22. while (await e.MoveNextAsync())
  23. {
  24. var element = e.Current;
  25. if (!predicate(element))
  26. {
  27. yield return element;
  28. while (await e.MoveNextAsync())
  29. {
  30. yield return e.Current;
  31. }
  32. yield break;
  33. }
  34. }
  35. }
  36. }
  37. }
  38. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  39. {
  40. if (source == null)
  41. throw Error.ArgumentNull(nameof(source));
  42. if (predicate == null)
  43. throw Error.ArgumentNull(nameof(predicate));
  44. return Create(Core);
  45. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  46. {
  47. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  48. {
  49. var index = -1;
  50. while (await e.MoveNextAsync())
  51. {
  52. checked
  53. {
  54. index++;
  55. }
  56. var element = e.Current;
  57. if (!predicate(element, index))
  58. {
  59. yield return element;
  60. while (await e.MoveNextAsync())
  61. {
  62. yield return e.Current;
  63. }
  64. yield break;
  65. }
  66. }
  67. }
  68. }
  69. }
  70. internal static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  71. {
  72. if (source == null)
  73. throw Error.ArgumentNull(nameof(source));
  74. if (predicate == null)
  75. throw Error.ArgumentNull(nameof(predicate));
  76. return Create(Core);
  77. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  78. {
  79. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  80. {
  81. while (await e.MoveNextAsync())
  82. {
  83. var element = e.Current;
  84. if (!await predicate(element).ConfigureAwait(false))
  85. {
  86. yield return element;
  87. while (await e.MoveNextAsync())
  88. {
  89. yield return e.Current;
  90. }
  91. yield break;
  92. }
  93. }
  94. }
  95. }
  96. }
  97. #if !NO_DEEP_CANCELLATION
  98. internal static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate)
  99. {
  100. if (source == null)
  101. throw Error.ArgumentNull(nameof(source));
  102. if (predicate == null)
  103. throw Error.ArgumentNull(nameof(predicate));
  104. return Create(Core);
  105. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  106. {
  107. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  108. {
  109. while (await e.MoveNextAsync())
  110. {
  111. var element = e.Current;
  112. if (!await predicate(element, cancellationToken).ConfigureAwait(false))
  113. {
  114. yield return element;
  115. while (await e.MoveNextAsync())
  116. {
  117. yield return e.Current;
  118. }
  119. yield break;
  120. }
  121. }
  122. }
  123. }
  124. }
  125. #endif
  126. internal static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate)
  127. {
  128. if (source == null)
  129. throw Error.ArgumentNull(nameof(source));
  130. if (predicate == null)
  131. throw Error.ArgumentNull(nameof(predicate));
  132. return Create(Core);
  133. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  134. {
  135. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  136. {
  137. var index = -1;
  138. while (await e.MoveNextAsync())
  139. {
  140. checked
  141. {
  142. index++;
  143. }
  144. var element = e.Current;
  145. if (!await predicate(element, index).ConfigureAwait(false))
  146. {
  147. yield return element;
  148. while (await e.MoveNextAsync())
  149. {
  150. yield return e.Current;
  151. }
  152. yield break;
  153. }
  154. }
  155. }
  156. }
  157. }
  158. #if !NO_DEEP_CANCELLATION
  159. internal static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate)
  160. {
  161. if (source == null)
  162. throw Error.ArgumentNull(nameof(source));
  163. if (predicate == null)
  164. throw Error.ArgumentNull(nameof(predicate));
  165. return Create(Core);
  166. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  167. {
  168. await using (var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false))
  169. {
  170. var index = -1;
  171. while (await e.MoveNextAsync())
  172. {
  173. checked
  174. {
  175. index++;
  176. }
  177. var element = e.Current;
  178. if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
  179. {
  180. yield return element;
  181. while (await e.MoveNextAsync())
  182. {
  183. yield return e.Current;
  184. }
  185. yield break;
  186. }
  187. }
  188. }
  189. }
  190. }
  191. #endif
  192. }
  193. }