| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT License.
- // See the LICENSE file in the project root for more information.
- using System.Collections.Generic;
- using System.Threading;
- using System.Threading.Tasks;
- namespace System.Linq
- {
- public static partial class AsyncEnumerable
- {
- #if INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.skipwhile?view=net-9.0-pp#system-linq-asyncenumerable-skipwhile-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-boolean)))
- /// <summary>
- /// Bypasses elements in an async-enumerable sequence as long as a specified condition is true and then returns the remaining elements.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to return elements from.</param>
- /// <param name="predicate">A function to test each element for a condition.</param>
- /// <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>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
- public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (predicate == null)
- throw Error.ArgumentNull(nameof(predicate));
- return Core(source, predicate);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, bool> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- while (await e.MoveNextAsync())
- {
- var element = e.Current;
- if (!predicate(element))
- {
- yield return element;
- while (await e.MoveNextAsync())
- {
- yield return e.Current;
- }
- yield break;
- }
- }
- }
- }
- // https://learn.microsoft.com/en-us/dotnet/api/system.linq.asyncenumerable.skipwhile?view=net-9.0-pp#system-linq-asyncenumerable-skipwhile-1(system-collections-generic-iasyncenumerable((-0))-system-func((-0-system-int32-system-boolean)))
- /// <summary>
- /// Bypasses elements in an async-enumerable sequence as long as a specified condition is true and then returns the remaining elements.
- /// The element's index is used in the logic of the predicate function.
- /// </summary>
- /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to return elements from.</param>
- /// <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>
- /// <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>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
- public static IAsyncEnumerable<TSource> SkipWhile<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (predicate == null)
- throw Error.ArgumentNull(nameof(predicate));
- return Core(source, predicate);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, bool> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- var index = -1;
- while (await e.MoveNextAsync())
- {
- checked
- {
- index++;
- }
- var element = e.Current;
- if (!predicate(element, index))
- {
- yield return element;
- while (await e.MoveNextAsync())
- {
- yield return e.Current;
- }
- yield break;
- }
- }
- }
- }
- #endif // INCLUDE_SYSTEM_LINQ_ASYNCENUMERABLE_DUPLICATES
- /// <summary>
- /// Bypasses elements in an async-enumerable sequence as long as a condition is true, and then returns the remaining elements.
- /// </summary>
- /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to return elements from.</param>
- /// <param name="predicate">An asynchronous function to test each element for a condition.</param>
- /// <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>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
- [GenerateAsyncOverload]
- [Obsolete("Use SkipWhile. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the SkipWhileAwait functionality now exists as overloads of SkipWhile. You will need to modify your callback to take an additional CancellationToken argument.")]
- private static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (predicate == null)
- throw Error.ArgumentNull(nameof(predicate));
- return Core(source, predicate);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- while (await e.MoveNextAsync())
- {
- var element = e.Current;
- if (!await predicate(element).ConfigureAwait(false))
- {
- yield return element;
- while (await e.MoveNextAsync())
- {
- yield return e.Current;
- }
- yield break;
- }
- }
- }
- }
- #if !NO_DEEP_CANCELLATION
- [GenerateAsyncOverload]
- [Obsolete("Use SkipWhile. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the SkipWhileAwaitWithCancellation functionality now exists as overloads of SkipWhile.")]
- private static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (predicate == null)
- throw Error.ArgumentNull(nameof(predicate));
- return Core(source, predicate);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, CancellationToken, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- while (await e.MoveNextAsync())
- {
- var element = e.Current;
- if (!await predicate(element, cancellationToken).ConfigureAwait(false))
- {
- yield return element;
- while (await e.MoveNextAsync())
- {
- yield return e.Current;
- }
- yield break;
- }
- }
- }
- }
- #endif
- /// <summary>
- /// Bypasses elements in an async-enumerable sequence as long as a condition is true, and then returns the remaining elements.
- /// The index of the element is used by the predicate.
- /// </summary>
- /// <typeparam name="TSource">The type of elements in the source sequence.</typeparam>
- /// <param name="source">An async-enumerable sequence to return elements from.</param>
- /// <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>
- /// <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>
- /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is <see langword="null"/>.</exception>
- [GenerateAsyncOverload]
- [Obsolete("Use SkipWhile. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the SkipWhileAwait functionality now exists as overloads of SkipWhile. You will need to modify your callback to take an additional CancellationToken argument.")]
- private static IAsyncEnumerable<TSource> SkipWhileAwaitCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (predicate == null)
- throw Error.ArgumentNull(nameof(predicate));
- return Core(source, predicate);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- var index = -1;
- while (await e.MoveNextAsync())
- {
- checked
- {
- index++;
- }
- var element = e.Current;
- if (!await predicate(element, index).ConfigureAwait(false))
- {
- yield return element;
- while (await e.MoveNextAsync())
- {
- yield return e.Current;
- }
- yield break;
- }
- }
- }
- }
- #if !NO_DEEP_CANCELLATION
- [GenerateAsyncOverload]
- [Obsolete("Use SkipWhile. IAsyncEnumerable LINQ is now in System.Linq.AsyncEnumerable, and the SkipWhileAwaitWithCancellation functionality now exists as overloads of SkipWhile.")]
- private static IAsyncEnumerable<TSource> SkipWhileAwaitWithCancellationCore<TSource>(this IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate)
- {
- if (source == null)
- throw Error.ArgumentNull(nameof(source));
- if (predicate == null)
- throw Error.ArgumentNull(nameof(predicate));
- return Core(source, predicate);
- static async IAsyncEnumerable<TSource> Core(IAsyncEnumerable<TSource> source, Func<TSource, int, CancellationToken, ValueTask<bool>> predicate, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
- {
- await using var e = source.GetConfiguredAsyncEnumerator(cancellationToken, false);
- var index = -1;
- while (await e.MoveNextAsync())
- {
- checked
- {
- index++;
- }
- var element = e.Current;
- if (!await predicate(element, index, cancellationToken).ConfigureAwait(false))
- {
- yield return element;
- while (await e.MoveNextAsync())
- {
- yield return e.Current;
- }
- yield break;
- }
- }
- }
- }
- #endif
- }
- }
|