Skip to content

Commit 0d7e8c5

Browse files
authored
Add Stack.TryPeek (#315)
1 parent 15c5bed commit 0d7e8c5

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 525**
1+
**API count: 526**

api_list.include.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@
339339
* `bool Contains<T>(Span<T>, T) where T : IEquatable<T>` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.contains#system-memoryextensions-contains-1(system-span((-0))-0))
340340

341341

342+
#### Stack<T>
343+
344+
* `bool TryPeek<T>(Stack<T>, T)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.trypeek)
345+
346+
342347
#### Stream
343348

344349
* `Task CopyToAsync(Stream, Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copytoasync#system-io-stream-copytoasync(system-io-stream-system-threading-cancellationtoken))

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ The package targets `netstandard2.0` and is designed to support the following ru
1212
* `net5.0`, `net6.0`, `net7.0`, `net8.0`, `net9.0`, `net10.0`
1313

1414

15-
**API count: 525**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
15+
**API count: 526**<!-- singleLineInclude: apiCount. path: /apiCount.include.md -->
1616

1717

1818
**See [Milestones](../../milestones?state=closed) for release notes.**
@@ -813,6 +813,11 @@ The class `Polyfill` includes the following extension methods:
813813
* `bool Contains<T>(Span<T>, T) where T : IEquatable<T>` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.contains#system-memoryextensions-contains-1(system-span((-0))-0))
814814

815815

816+
#### Stack<T>
817+
818+
* `bool TryPeek<T>(Stack<T>, T)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.trypeek)
819+
820+
816821
#### Stream
817822

818823
* `Task CopyToAsync(Stream, Stream, CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.io.stream.copytoasync#system-io-stream-copytoasync(system-io-stream-system-threading-cancellationtoken))

src/Consume/Consume.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,12 @@ void Span_Methods()
548548

549549
#endif
550550

551+
void Stack_Methods()
552+
{
553+
var stack = new Stack<char>();
554+
stack.TryPeek(out var ch);
555+
}
556+
551557
async Task Stream_Methods()
552558
{
553559
var input = new byte[] { 1, 2 };

src/Polyfill/Polyfill_Stack.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// <auto-generated />
2+
3+
#pragma warning disable
4+
5+
namespace Polyfills;
6+
7+
using System.Collections.Generic;
8+
using System.Diagnostics.CodeAnalysis;
9+
10+
static partial class Polyfill
11+
{
12+
#if NETSTANDARD2_0 || NETFRAMEWORK
13+
14+
/// <summary>
15+
/// Returns a value that indicates whether there is an object at the top of the <see cref="Stack{T}"/>, and if one is present, copies it to the result parameter. The object is not removed from the <see cref="Stack{T}"/>.
16+
/// </summary>
17+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.stack-1.trypeek
18+
public static bool TryPeek<T>(this Stack<T> target, [MaybeNullWhen(false)] out T result)
19+
{
20+
if (target.Count > 0)
21+
{
22+
result = target.Peek();
23+
return true;
24+
}
25+
26+
result = default;
27+
return false;
28+
}
29+
30+
#endif
31+
}

src/Tests/PolyfillTests_Stack.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
partial class PolyfillTests
2+
{
3+
[Test]
4+
public void Stack_TryPeek()
5+
{
6+
var stack = new Stack<int>();
7+
8+
// Test when stack is empty
9+
var result = stack.TryPeek(out var value);
10+
Assert.IsFalse(result);
11+
Assert.AreEqual(0, value);
12+
13+
// Test when stack has elements
14+
stack.Push(1);
15+
stack.Push(2);
16+
stack.Push(3);
17+
18+
result = stack.TryPeek(out value);
19+
Assert.IsTrue(result);
20+
// The top element should be 3
21+
Assert.AreEqual(3, value);
22+
23+
// Ensure the stack is not modified
24+
Assert.AreEqual(3, stack.Count);
25+
}
26+
}

0 commit comments

Comments
 (0)