Skip to content

Commit

Permalink
add Random.NextByte(Span) (#191)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Jul 1, 2024
1 parent 06f2189 commit df1887d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 6 deletions.
2 changes: 1 addition & 1 deletion apiCount.include.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
**API count: 296**
**API count: 297**
5 changes: 5 additions & 0 deletions api_list.include.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@
* `Task<String> ReadAsStringAsync(CancellationToken)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpcontent.readasstringasync#system-net-http-httpcontent-readasstringasync(system-threading-cancellationtoken))


#### Random

* `Void NextBytes(Span<Byte>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte))))


#### ReadOnlySpan<Char>

* `Boolean EndsWith(String, StringComparison)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.memoryextensions.endswith#system-memoryextensions-endswith-1(system-readonlyspan((-0))-system-readonlyspan((-0))))
Expand Down
29 changes: 24 additions & 5 deletions src/Consume/Consume.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Consume
typeof(List<string>).IsAssignableTo(typeof(string));
typeof(List<string>).IsAssignableTo(null);

var enumerable = (IEnumerable<string>) new List<string>
var enumerable = (IEnumerable<string>)new List<string>
{
"a",
"b"
Expand Down Expand Up @@ -245,7 +245,7 @@ void KeyValuePairDeconstruct(IEnumerable<KeyValuePair<string, string>> variables

#endif

#pragma warning disable ExperimentalMethod
#pragma warning disable ExperimentalMethod
static void ExperimentalMethodUsage() =>
ExperimentalMethod();

Expand All @@ -264,8 +264,12 @@ void CancellationTokenUnsafeRegister()
{
var source = new CancellationTokenSource();
var token = source.Token;
token.UnsafeRegister(state => {}, null);
token.UnsafeRegister((state, token) => {}, null);
token.UnsafeRegister(state =>
{
}, null);
token.UnsafeRegister((state, token) =>
{
}, null);
}

async Task ProcessWaitForExitAsync()
Expand All @@ -288,7 +292,9 @@ async Task StreamReaderReadLineAsync()

void WaitAsync()
{
var action = () => {};
var action = () =>
{
};
var func = () => 0;
new Task(action).WaitAsync(CancellationToken.None);
new Task(action).WaitAsync(TimeSpan.Zero);
Expand Down Expand Up @@ -451,6 +457,19 @@ void GetMemberWithSameMetadataDefinitionAs(MemberInfo info)
var result = typeof(string).GetMemberWithSameMetadataDefinitionAs(info);
}

#if FeatureMemory
void RandomNextBytesSpan()
{
// Create a new instance of Random
var random = new Random();

// Create a span of bytes
Span<byte> buffer = new byte[10];

// Fill the span with random bytes
random.NextBytes(buffer);
}
#endif
public void SortedList()
{
var list = new SortedList<int, char>();
Expand Down
27 changes: 27 additions & 0 deletions src/Polyfill/Polyfill_Random.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// <auto-generated />
#pragma warning disable

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Link = System.ComponentModel.DescriptionAttribute;

static partial class Polyfill
{
#if (NETSTANDARD || NETFRAMEWORK || NETCOREAPP2_0) && FeatureMemory
/// <summary>
/// Fills the elements of a specified span of bytes with random numbers.
/// </summary>
/// <param name="index">The array to be filled with random numbers.</param>
/// <remarks>Each element of the span of bytes is set to a random number greater than or equal to 0 and less than or equal to <see cref="byte.MaxValue"/>.</remarks>
[Link("https://learn.microsoft.com/en-us/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte)))")]
public static void NextBytes(
this Random target,
Span<byte> buffer)
{
var array = new byte[buffer.Length];
target.NextBytes(array);
array.CopyTo(buffer);
}
#endif
}
20 changes: 20 additions & 0 deletions src/Tests/PolyfillTests_Random.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
partial class PolyfillTests
{
#if FeatureMemory
[Test]
public void RandomNextBytesSpan()
{
// Create a new instance of Random
var random = new Random();

// Create a span of bytes
Span<byte> buffer = new byte[10];

// Fill the span with random bytes
random.NextBytes(buffer);

// Assert that the span is filled with random bytes
Assert.IsTrue(buffer.ToArray().Any(b => b != 0));
}
#endif
}

0 comments on commit df1887d

Please sign in to comment.