SkipWhile.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT 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. /// <summary>
  12. /// Bypasses elements in an async-enumerable sequence as long as a specified condition is true and then returns the remaining elements.
  13. /// </summary>
  14. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  15. /// <param name="source">An async-enumerable sequence to return elements from.</param>
  16. /// <param name="predicate">A function to test each element for a condition.</param>
  17. /// <returns>An async-enumerable sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
  18. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  19. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
  20. {
  21. if (source == null)
  22. throw Error.ArgumentNull(nameof(source));
  23. if (predicate == null)
  24. throw Error.ArgumentNull(nameof(predicate));
  25. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  26. return Core();
  27. async IAsyncEnumerable<TSource> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  28. #else
  29. return Create(Core);
  30. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  31. #endif
  32. {
  33. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  34. while (await e.MoveNextAsync())
  35. {
  36. var element = e.Current;
  37. if (!predicate(element))
  38. {
  39. yield return element;
  40. while (await e.MoveNextAsync())
  41. {
  42. yield return e.Current;
  43. }
  44. yield break;
  45. }
  46. }
  47. }
  48. }
  49. /// <summary>
  50. /// Bypasses elements in an async-enumerable sequence as long as a specified condition is true and then returns the remaining elements.
  51. /// The element's index is used in the logic of the predicate function.
  52. /// </summary>
  53. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  54. /// <param name="source">An async-enumerable sequence to return elements from.</param>
  55. /// <param name="predicate">A function to test each element for a condition; the second parameter of the function represents the index of the source element.</param>
  56. /// <returns>An async-enumerable sequence that contains the elements from the input sequence starting at the first element in the linear series that does not pass the test specified by predicate.</returns>
  57. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  58. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  59. {
  60. if (source == null)
  61. throw Error.ArgumentNull(nameof(source));
  62. if (predicate == null)
  63. throw Error.ArgumentNull(nameof(predicate));
  64. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  65. return Core();
  66. async IAsyncEnumerable<TSource> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  67. #else
  68. return Create(Core);
  69. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  70. #endif
  71. {
  72. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  73. var index = -1;
  74. while (await e.MoveNextAsync())
  75. {
  76. checked
  77. {
  78. index++;
  79. }
  80. var element = e.Current;
  81. if (!predicate(element, index))
  82. {
  83. yield return element;
  84. while (await e.MoveNextAsync())
  85. {
  86. yield return e.Current;
  87. }
  88. yield break;
  89. }
  90. }
  91. }
  92. }
  93. internal static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  94. {
  95. if (source == null)
  96. throw Error.ArgumentNull(nameof(source));
  97. if (predicate == null)
  98. throw Error.ArgumentNull(nameof(predicate));
  99. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  100. return Core();
  101. async IAsyncEnumerable<TSource> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  102. #else
  103. return Create(Core);
  104. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  105. #endif
  106. {
  107. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  108. while (await e.MoveNextAsync())
  109. {
  110. var element = e.Current;
  111. if (!await predicate(element).ConfigureAwait(false))
  112. {
  113. yield return element;
  114. while (await e.MoveNextAsync())
  115. {
  116. yield return e.Current;
  117. }
  118. yield break;
  119. }
  120. }
  121. }
  122. }
  123. #if !NO_DEEP_CANCELLATION
  124. internal static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate)
  125. {
  126. if (source == null)
  127. throw Error.ArgumentNull(nameof(source));
  128. if (predicate == null)
  129. throw Error.ArgumentNull(nameof(predicate));
  130. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  131. return Core();
  132. async IAsyncEnumerable<TSource> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  133. #else
  134. return Create(Core);
  135. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  136. #endif
  137. {
  138. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  139. while (await e.MoveNextAsync())
  140. {
  141. var element = e.Current;
  142. if (!await predicate(element, cancellationToken).ConfigureAwait(false))
  143. {
  144. yield return element;
  145. while (await e.MoveNextAsync())
  146. {
  147. yield return e.Current;
  148. }
  149. yield break;
  150. }
  151. }
  152. }
  153. }
  154. #endif
  155. internal static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate)
  156. {
  157. if (source == null)
  158. throw Error.ArgumentNull(nameof(source));
  159. if (predicate == null)
  160. throw Error.ArgumentNull(nameof(predicate));
  161. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  162. return Core();
  163. async IAsyncEnumerable<TSource> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  164. #else
  165. return Create(Core);
  166. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  167. #endif
  168. {
  169. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  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).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. #if !NO_DEEP_CANCELLATION
  191. internal static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate)
  192. {
  193. if (source == null)
  194. throw Error.ArgumentNull(nameof(source));
  195. if (predicate == null)
  196. throw Error.ArgumentNull(nameof(predicate));
  197. #if HAS_ASYNC_ENUMERABLE_CANCELLATION
  198. return Core();
  199. async IAsyncEnumerable<TSource> Core([System.Runtime.CompilerServices.EnumeratorCancellation]CancellationToken cancellationToken = default)
  200. #else
  201. return Create(Core);
  202. async IAsyncEnumerator<TSource> Core(CancellationToken cancellationToken)
  203. #endif
  204. {
  205. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  206. var index = -1;
  207. while (await e.MoveNextAsync())
  208. {
  209. checked
  210. {
  211. index++;
  212. }
  213. var element = e.Current;
  214. if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
  215. {
  216. yield return element;
  217. while (await e.MoveNextAsync())
  218. {
  219. yield return e.Current;
  220. }
  221. yield break;
  222. }
  223. }
  224. }
  225. }
  226. #endif
  227. }
  228. }