SkipWhile.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. return Core(source, predicate);
  26. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  27. {
  28. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  29. while (await e.MoveNextAsync())
  30. {
  31. var element = e.Current;
  32. if (!predicate(element))
  33. {
  34. yield return element;
  35. while (await e.MoveNextAsync())
  36. {
  37. yield return e.Current;
  38. }
  39. yield break;
  40. }
  41. }
  42. }
  43. }
  44. /// <summary>
  45. /// Bypasses elements in an async-enumerable sequence as long as a specified condition is true and then returns the remaining elements.
  46. /// The element's index is used in the logic of the predicate function.
  47. /// </summary>
  48. /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
  49. /// <param name="source">An async-enumerable sequence to return elements from.</param>
  50. /// <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>
  51. /// <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>
  52. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
  53. public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
  54. {
  55. if (source == null)
  56. throw Error.ArgumentNull(nameof(source));
  57. if (predicate == null)
  58. throw Error.ArgumentNull(nameof(predicate));
  59. return Core(source, predicate);
  60. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  61. {
  62. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  63. var index = -1;
  64. while (await e.MoveNextAsync())
  65. {
  66. checked
  67. {
  68. index++;
  69. }
  70. var element = e.Current;
  71. if (!predicate(element, index))
  72. {
  73. yield return element;
  74. while (await e.MoveNextAsync())
  75. {
  76. yield return e.Current;
  77. }
  78. yield break;
  79. }
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Bypasses elements in an async-enumerable sequence as long as a condition is true, and then returns the remaining elements.
  85. /// </summary>
  86. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  87. /// <param name="source">An async-enumerable sequence to return elements from.</param>
  88. /// <param name="predicate">An asynchronous function to test each element for a condition.</param>
  89. /// <returns>An async-enumerable sequence containing the elements in the source sequence starting at the first element that does not pass the test specified by the predicate.</returns>
  90. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
  91. [GenerateAsyncOverload]
  92. private static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
  93. {
  94. if (source == null)
  95. throw Error.ArgumentNull(nameof(source));
  96. if (predicate == null)
  97. throw Error.ArgumentNull(nameof(predicate));
  98. return Core(source, predicate);
  99. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  100. {
  101. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  102. while (await e.MoveNextAsync())
  103. {
  104. var element = e.Current;
  105. if (!await predicate(element).ConfigureAwait(false))
  106. {
  107. yield return element;
  108. while (await e.MoveNextAsync())
  109. {
  110. yield return e.Current;
  111. }
  112. yield break;
  113. }
  114. }
  115. }
  116. }
  117. #if !NO_DEEP_CANCELLATION
  118. [GenerateAsyncOverload]
  119. private static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate)
  120. {
  121. if (source == null)
  122. throw Error.ArgumentNull(nameof(source));
  123. if (predicate == null)
  124. throw Error.ArgumentNull(nameof(predicate));
  125. return Core(source, predicate);
  126. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  127. {
  128. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  129. while (await e.MoveNextAsync())
  130. {
  131. var element = e.Current;
  132. if (!await predicate(element, cancellationToken).ConfigureAwait(false))
  133. {
  134. yield return element;
  135. while (await e.MoveNextAsync())
  136. {
  137. yield return e.Current;
  138. }
  139. yield break;
  140. }
  141. }
  142. }
  143. }
  144. #endif
  145. /// <summary>
  146. /// Bypasses elements in an async-enumerable sequence as long as a condition is true, and then returns the remaining elements.
  147. /// The index of the element is used by the predicate.
  148. /// </summary>
  149. /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
  150. /// <param name="source">An async-enumerable sequence to return elements from.</param>
  151. /// <param name="predicate">An asynchronous function to test each element for a condition; the second parameter of the function represents the index of the element.</param>
  152. /// <returns>An async-enumerable sequence containing the elements in the source sequence starting at the first element that does not pass the test specified by the predicate.</returns>
  153. /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
  154. [GenerateAsyncOverload]
  155. private 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. return Core(source, predicate);
  162. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  163. {
  164. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  165. var index = -1;
  166. while (await e.MoveNextAsync())
  167. {
  168. checked
  169. {
  170. index++;
  171. }
  172. var element = e.Current;
  173. if (!await predicate(element, index).ConfigureAwait(false))
  174. {
  175. yield return element;
  176. while (await e.MoveNextAsync())
  177. {
  178. yield return e.Current;
  179. }
  180. yield break;
  181. }
  182. }
  183. }
  184. }
  185. #if !NO_DEEP_CANCELLATION
  186. [GenerateAsyncOverload]
  187. private static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate)
  188. {
  189. if (source == null)
  190. throw Error.ArgumentNull(nameof(source));
  191. if (predicate == null)
  192. throw Error.ArgumentNull(nameof(predicate));
  193. return Core(source, predicate);
  194. static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
  195. {
  196. await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
  197. var index = -1;
  198. while (await e.MoveNextAsync())
  199. {
  200. checked
  201. {
  202. index++;
  203. }
  204. var element = e.Current;
  205. if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
  206. {
  207. yield return element;
  208. while (await e.MoveNextAsync())
  209. {
  210. yield return e.Current;
  211. }
  212. yield break;
  213. }
  214. }
  215. }
  216. }
  217. #endif
  218. }
  219. }