1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-22 13:08:48 +00:00

Add .NET Standard 2.1 target, reintroduce Unity support

This commit is contained in:
Oliver Booth 2022-05-05 18:00:38 +01:00
parent 2d51f65834
commit f257aebc28
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
83 changed files with 3170 additions and 95 deletions

View File

@ -33,6 +33,7 @@ jobs:
run: |
mkdir build
dotnet pack X10D -p:SymbolPackageFormat=snupkg --include-symbols --include-source -o build -p:VersionSuffix='nightly' -p:BuildNumber=${{ github.run_number }}
dotnet pack X10D.Unity -p:SymbolPackageFormat=snupkg --include-symbols --include-source -o build -p:VersionSuffix='prerelease' -p:BuildNumber=${{ github.run_number }}
- name: Push NuGet Package to GitHub
run: dotnet nuget push "build/*" --source "github" --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate

View File

@ -32,6 +32,7 @@ jobs:
run: |
mkdir build
dotnet pack X10D -p:SymbolPackageFormat=snupkg --include-symbols --include-source -o build -p:VersionSuffix='prerelease' -p:BuildNumber=${{ github.run_number }}
dotnet pack X10D.Unity -p:SymbolPackageFormat=snupkg --include-symbols --include-source -o build -p:VersionSuffix='prerelease' -p:BuildNumber=${{ github.run_number }}
- name: Push NuGet Package to GitHub
run: dotnet nuget push "build/*" --source "github" --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate

View File

@ -32,6 +32,7 @@ jobs:
run: |
mkdir build
dotnet pack X10D -p:SymbolPackageFormat=snupkg --include-symbols --include-source -o build
dotnet pack X10D.Unity -p:SymbolPackageFormat=snupkg --include-symbols --include-source -o build
- name: Push NuGet Package to GitHub
run: dotnet nuget push "build/*" --source "github" --api-key ${{ secrets.GITHUB_TOKEN }} --skip-duplicate

View File

@ -32,5 +32,4 @@ jobs:
run: dotnet build -c Debug
- name: Run Source Validation
run: dotnet run --project X10D.SourceValidator ./X10D/src
run: dotnet run --project X10D.SourceValidator ./X10D/src ./X10D.Unity/src

View File

@ -1,64 +1,68 @@
using System.Text;
var directories = new Stack<string>(Directory.GetDirectories(args[0]));
var problems = 0;
var files = 0;
while (directories.Count > 0)
foreach (string arg in args)
{
string path = Path.GetFullPath(directories.Pop());
var directories = new Stack<string>(Directory.GetDirectories(arg));
var files = 0;
foreach (string directory in Directory.EnumerateDirectories(path))
while (directories.Count > 0)
{
directories.Push(directory);
}
string path = Path.GetFullPath(directories.Pop());
foreach (string file in Directory.EnumerateFiles(path, "*.cs"))
{
files++;
await using var stream = File.OpenRead(file);
using var reader = new StreamReader(stream, Encoding.UTF8);
var blankLine = false;
var lineNumber = 1;
while (await reader.ReadLineAsync() is { } line)
foreach (string directory in Directory.EnumerateDirectories(path))
{
if (string.IsNullOrWhiteSpace(line))
directories.Push(directory);
}
foreach (string file in Directory.EnumerateFiles(path, "*.cs"))
{
files++;
await using var stream = File.OpenRead(file);
using var reader = new StreamReader(stream, Encoding.UTF8);
var blankLine = false;
var lineNumber = 1;
while (await reader.ReadLineAsync() is { } line)
{
if (blankLine)
if (string.IsNullOrWhiteSpace(line))
{
if (blankLine)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine($"{file}({lineNumber}): Double blank line");
Console.ResetColor();
problems++;
}
blankLine = true;
}
else
{
blankLine = false;
}
if (line.Length > 130)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine($"{file}({lineNumber}): Double blank line");
Console.Out.WriteLine($"{file}({lineNumber}): Line is too long ({line.Length})");
Console.ResetColor();
problems++;
}
else if (line.Length > 0 && char.IsWhiteSpace(line[^1]))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine($"{file}({lineNumber}): Line contains trailing whitespace");
Console.ResetColor();
problems++;
}
blankLine = true;
lineNumber++;
}
else
{
blankLine = false;
}
if (line.Length > 130)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine($"{file}({lineNumber}): Line is too long ({line.Length})");
Console.ResetColor();
problems++;
}
else if (line.Length > 0 && char.IsWhiteSpace(line[^1]))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Out.WriteLine($"{file}({lineNumber}): Line contains trailing whitespace");
Console.ResetColor();
problems++;
}
lineNumber++;
}
}
Console.Out.WriteLine($"Finished scanning {files} files, {problems} problems encountered.");
}
Console.Out.WriteLine($"Finished scanning {files} files, {problems} problems encountered.");
return problems;

View File

@ -0,0 +1,65 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<LangVersion>10.0</LangVersion>
<Optimize>true</Optimize>
<ImplicitUsings>true</ImplicitUsings>
<Authors>Oliver Booth</Authors>
<NeutralLanguage>en</NeutralLanguage>
<RepositoryUrl>https://github.com/oliverbooth/X10D</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Description>Extension methods on crack.</Description>
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>
<PackageIcon>icon.png</PackageIcon>
<PackageIconUrl />
<PackageTags>dotnet extension-methods</PackageTags>
<CodeAnalysisTreatWarningsAsErrors>true</CodeAnalysisTreatWarningsAsErrors>
<VersionPrefix>3.1.0</VersionPrefix>
<Nullable>enable</Nullable>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(VersionSuffix)' != '' And '$(BuildNumber)' == ''">
<Version>$(VersionPrefix)-$(VersionSuffix)</Version>
<AssemblyVersion>$(VersionPrefix).0</AssemblyVersion>
<FileVersion>$(VersionPrefix).0</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(VersionSuffix)' != '' And '$(BuildNumber)' != ''">
<Version>$(VersionPrefix)-$(VersionSuffix).$(BuildNumber)</Version>
<AssemblyVersion>$(VersionPrefix).$(BuildNumber)</AssemblyVersion>
<FileVersion>$(VersionPrefix).$(BuildNumber)</FileVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(VersionSuffix)' == ''">
<Version>$(VersionPrefix)</Version>
<AssemblyVersion>$(VersionPrefix).0</AssemblyVersion>
<FileVersion>$(VersionPrefix).0</FileVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Unity3D.SDK" Version="2020.3.13.1" PrivateAssets="All" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\X10D\X10D.csproj" />
</ItemGroup>
<ItemGroup>
<None Include="..\icon.png">
<Pack>True</Pack>
<PackagePath />
</None>
<None Include="..\LICENSE.md">
<Pack>True</Pack>
<PackagePath />
</None>
<None Include="..\CHANGELOG.md">
<Pack>True</Pack>
<PackagePath />
</None>
</ItemGroup>
</Project>

View File

@ -0,0 +1,95 @@
using UnityEngine;
using X10D.Core;
using Random = System.Random;
namespace X10D.Unity.Drawing;
/// <summary>
/// Extension methods for <see cref="System.Random" />.
/// </summary>
public static class RandomExtensions
{
/// <summary>
/// Returns an HDR color of random components for red, green, and blue.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Color" /> whose red, green, and blue components are all random, and whose alpha is 255</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColorRgb(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
float r = seededRandom.NextSingle();
float g = seededRandom.NextSingle();
float b = seededRandom.NextSingle();
return new Color(r, g, b, 1.0f);
}
/// <summary>
/// Returns an HDR color composed of random components for apha, red, green, and blue.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Color" /> whose alpha, red, green, and blue components are all random.</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColorArgb(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
float a = seededRandom.NextSingle();
float r = seededRandom.NextSingle();
float g = seededRandom.NextSingle();
float b = seededRandom.NextSingle();
return new Color(r, g, b, a);
}
/// <summary>
/// Returns a color of random components for red, green, and blue.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Color" /> whose red, green, and blue components are all random, and whose alpha is 255</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color32 NextColor32Rgb(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int rgb = random.Next();
var r = (byte)(rgb >> 16 & 0xFF);
var g = (byte)(rgb >> 8 & 0xFF);
var b = (byte)(rgb & 0xFF);
return new Color32(r, g, b, 0xFF);
}
/// <summary>
/// Returns a color composed of random components for apha, red, green, and blue.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Color" /> whose alpha, red, green, and blue components are all random.</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color32 NextColor32Argb(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int argb = random.Next();
var a = (byte)(argb >> 24 & 0xFF);
var r = (byte)(argb >> 16 & 0xFF);
var g = (byte)(argb >> 8 & 0xFF);
var b = (byte)(argb & 0xFF);
return new Color32(r, g, b, a);
}
}

View File

@ -0,0 +1,252 @@
using UnityEngine;
namespace X10D.Unity;
/// <summary>
/// Extension methods for <see cref="GameObject" />.
/// </summary>
public static class GameObjectExtensions
{
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at another game object.
/// </summary>
/// <param name="gameObject">The game object whose rotation will be changed.</param>
/// <param name="target">The game object to look at.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="target" /> is <see langword="null" />.</para>
/// </exception>
public static void LookAt(this GameObject gameObject, GameObject target)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
gameObject.transform.LookAt(target.transform);
}
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at <paramref name="target" />.
/// </summary>
/// <param name="gameObject">The game object whose rotation will be changed.</param>
/// <param name="target">The point to look at.</param>
/// <exception cref="ArgumentNullException"><paramref name="gameObject" /> is <see langword="null" />.</exception>
public static void LookAt(this GameObject gameObject, Vector3 target)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
gameObject.transform.LookAt(target);
}
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at a specified transform.
/// </summary>
/// <param name="gameObject">The game object whose rotation will be changed.</param>
/// <param name="target">The transform to look at.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="target" /> is <see langword="null" />.</para>
/// </exception>
public static void LookAt(this GameObject gameObject, Transform target)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
gameObject.transform.LookAt(target);
}
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at another game object.
/// </summary>
/// <param name="gameObject">The game object whose rotation will be changed.</param>
/// <param name="target">The game object to look at.</param>
/// <param name="worldUp">A vector specifying the upward direction.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="target" /> is <see langword="null" />.</para>
/// </exception>
public static void LookAt(this GameObject gameObject, GameObject target, Vector3 worldUp)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
gameObject.transform.LookAt(target.transform, worldUp);
}
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at <paramref name="target" />.
/// </summary>
/// <param name="gameObject">The game object whose rotation will be changed.</param>
/// <param name="target">The point to look at.</param>
/// <param name="worldUp">A vector specifying the upward direction.</param>
/// <exception cref="ArgumentNullException"><paramref name="gameObject" /> is <see langword="null" />.</exception>
public static void LookAt(this GameObject gameObject, Vector3 target, Vector3 worldUp)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
gameObject.transform.LookAt(target, worldUp);
}
/// <summary>
/// Rotates the transform component of this game object so the forward vector points at a specified transform.
/// </summary>
/// <param name="gameObject">The game object whose rotation will be changed.</param>
/// <param name="target">The transform to look at.</param>
/// <param name="worldUp">A vector specifying the upward direction.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="target" /> is <see langword="null" />.</para>
/// </exception>
public static void LookAt(this GameObject gameObject, Transform target, Vector3 worldUp)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
gameObject.transform.LookAt(target, worldUp);
}
/// <summary>
/// Sets the parent of this game object.
/// </summary>
/// <param name="gameObject">The game object whose parent to change.</param>
/// <param name="parent">The new parent.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="parent" /> is <see langword="null" />.</para>
/// </exception>
public static void SetParent(this GameObject gameObject, GameObject parent)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
gameObject.transform.SetParent(parent.transform);
}
/// <summary>
/// Sets the parent of this game object.
/// </summary>
/// <param name="gameObject">The game object whose parent to change.</param>
/// <param name="parent">The new parent.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="parent" /> is <see langword="null" />.</para>
/// </exception>
public static void SetParent(this GameObject gameObject, Transform parent)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
gameObject.transform.SetParent(parent);
}
/// <summary>
/// Sets the parent of this game object.
/// </summary>
/// <param name="gameObject">The game object whose parent to change.</param>
/// <param name="parent">The new parent.</param>
/// <param name="worldPositionStays">
/// <see langword="true" /> to modify the parent-relative position, scale and rotation such that the object keeps the same
/// world space position, rotation and scale as before; otherwise, <see langword="false" />.
/// </param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="parent" /> is <see langword="null" />.</para>
/// </exception>
public static void SetParent(this GameObject gameObject, GameObject parent, bool worldPositionStays)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
gameObject.transform.SetParent(parent.transform, worldPositionStays);
}
/// <summary>
/// Sets the parent of this game object.
/// </summary>
/// <param name="gameObject">The game object whose parent to change.</param>
/// <param name="parent">The new parent.</param>
/// <param name="worldPositionStays">
/// <see langword="true" /> to modify the parent-relative position, scale and rotation such that the object keeps the same
/// world space position, rotation and scale as before; otherwise, <see langword="false" />.
/// </param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="gameObject" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="parent" /> is <see langword="null" />.</para>
/// </exception>
public static void SetParent(this GameObject gameObject, Transform parent, bool worldPositionStays)
{
if (gameObject == null)
{
throw new ArgumentNullException(nameof(gameObject));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
gameObject.transform.SetParent(parent, worldPositionStays);
}
}

View File

@ -0,0 +1,118 @@
using UnityEngine;
using X10D.Core;
using Random = System.Random;
namespace X10D.Unity.Numerics;
/// <summary>
/// Extension methods for <see cref="System.Random" />.
/// </summary>
public static class RandomExtensions
{
/// <summary>
/// Returns a randomly generated rotation as represented by a <see cref="Quaternion" />.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>
/// A <see cref="Quaternion" /> constructed from 3 random single-precision floating point numbers representing the
/// yaw, pitch, and roll.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotation(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
float x = seededRandom.NextSingle(0, 360);
float y = seededRandom.NextSingle(0, 360);
float z = seededRandom.NextSingle(0, 360);
return Quaternion.Euler(x, y, z);
}
/// <summary>
/// Returns a randomly generated rotation with uniform distribution.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A <see cref="Quaternion" /> constructed with uniform distribution.</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotationUniform(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
float normal, w, x, y, z;
do
{
w = seededRandom.NextSingle(-1f, 1f);
x = seededRandom.NextSingle(-1f, 1f);
y = seededRandom.NextSingle(-1f, 1f);
z = seededRandom.NextSingle(-1f, 1f);
normal = (w * w) + (x * x) + (y * y) + (z * z);
} while (normal is 0f or > 1f);
normal = MathF.Sqrt(normal);
return new Quaternion(x / normal, y / normal, z / normal, w / normal);
}
/// <summary>
/// Returns a <see cref="Vector2" /> with magnitude 1 whose components indicate a random point on the unit circle.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance</param>
/// <returns>
/// A <see cref="Vector2" /> whose <see cref="Vector2.Length()" /> returns 1, and whose components indicate a random
/// point on the unit circle.
/// </returns>
public static Vector2 NextUnitVector2(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
// no need to construct a seeded random here, since we only call Next once
float angle = random.NextSingle(0, MathF.PI * 2.0f);
float x = MathF.Cos(angle);
float y = MathF.Sin(angle);
return new Vector2(x, y);
}
/// <summary>
/// Returns a <see cref="Vector3" /> with magnitude 1 whose components indicate a random point on the unit sphere.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance</param>
/// <returns>
/// A <see cref="Vector3" /> whose <see cref="Vector3.Length()" /> returns 1, and whose components indicate a random
/// point on the unit sphere.
/// </returns>
public static Vector3 NextUnitVector3(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
int seed = random.Next();
var seededRandom = new Random(seed);
float angle = seededRandom.NextSingle(0, MathF.PI * 2.0f);
float z = seededRandom.NextSingle(-1, 1);
float mp = MathF.Sqrt(1 - (z * z));
float x = mp * MathF.Cos(angle);
float y = mp * MathF.Sin(angle);
return new Vector3(x, y, z);
}
}

View File

@ -0,0 +1,114 @@
using UnityEngine;
namespace X10D.Unity;
/// <summary>
/// Extension methods for <see cref="Transform" />.
/// </summary>
public static class TransformExtensions
{
/// <summary>
/// Rotates this transform so the forward vector points at another game object.
/// </summary>
/// <param name="transform">The transform whose rotation will be changed.</param>
/// <param name="target">The game object to look at.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="transform" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="target" /> is <see langword="null" />.</para>
/// </exception>
public static void LookAt(this Transform transform, GameObject target)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
transform.LookAt(target.transform);
}
/// <summary>
/// Rotates this transform so the forward vector points at another game object.
/// </summary>
/// <param name="transform">The transform whose rotation will be changed.</param>
/// <param name="target">The game object to look at.</param>
/// <param name="worldUp">A vector specifying the upward direction.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="transform" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="target" /> is <see langword="null" />.</para>
/// </exception>
public static void LookAt(this Transform transform, GameObject target, Vector3 worldUp)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
transform.LookAt(target.transform, worldUp);
}
/// <summary>
/// Sets the parent of this transform.
/// </summary>
/// <param name="transform">The transform whose parent to change.</param>
/// <param name="parent">The new parent.</param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="transform" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="parent" /> is <see langword="null" />.</para>
/// </exception>
public static void SetParent(this Transform transform, GameObject parent)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
transform.transform.SetParent(parent.transform);
}
/// <summary>
/// Sets the parent of this transform.
/// </summary>
/// <param name="transform">The transform whose parent to change.</param>
/// <param name="parent">The new parent.</param>
/// <param name="worldPositionStays">
/// <see langword="true" /> to modify the parent-relative position, scale and rotation such that the object keeps the same
/// world space position, rotation and scale as before; otherwise, <see langword="false" />.
/// </param>
/// <exception cref="ArgumentNullException">
/// <para><paramref name="transform" /> is <see langword="null" />.</para>
/// -or-
/// <para><paramref name="parent" /> is <see langword="null" />.</para>
/// </exception>
public static void SetParent(this Transform transform, GameObject parent, bool worldPositionStays)
{
if (transform == null)
{
throw new ArgumentNullException(nameof(transform));
}
if (parent == null)
{
throw new ArgumentNullException(nameof(parent));
}
transform.SetParent(parent.transform, worldPositionStays);
}
}

View File

@ -20,6 +20,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "X10D.SourceValidator", "X10D.SourceValidator\X10D.SourceValidator.csproj", "{84750149-9068-4780-AFDE-CDA1AC57007D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "X10D.Unity", "X10D.Unity\X10D.Unity.csproj", "{7EAB3F09-A9FD-4334-B4DB-0394DD0C6568}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -38,6 +40,10 @@ Global
{84750149-9068-4780-AFDE-CDA1AC57007D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84750149-9068-4780-AFDE-CDA1AC57007D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84750149-9068-4780-AFDE-CDA1AC57007D}.Release|Any CPU.Build.0 = Release|Any CPU
{7EAB3F09-A9FD-4334-B4DB-0394DD0C6568}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7EAB3F09-A9FD-4334-B4DB-0394DD0C6568}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EAB3F09-A9FD-4334-B4DB-0394DD0C6568}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EAB3F09-A9FD-4334-B4DB-0394DD0C6568}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
<LangVersion>10.0</LangVersion>
<Optimize>true</Optimize>
<ImplicitUsings>true</ImplicitUsings>

View File

@ -17,7 +17,14 @@ public static class ArrayExtensions
[Pure]
public static IReadOnlyCollection<T> AsReadOnly<T>(this T[] array)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(array);
#else
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
#endif
return Array.AsReadOnly(array);
}
@ -42,7 +49,14 @@ public static class ArrayExtensions
/// <exception cref="ArgumentNullException"><paramref name="array" /> is <see langword="null" />.</exception>
public static void Clear<T>(this T?[] array, Range range)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(array);
#else
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
#endif
int index = range.Start.Value;
int end = range.End.Value;
@ -71,7 +85,14 @@ public static class ArrayExtensions
/// </exception>
public static void Clear<T>(this T?[] array, int index, int length)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(array);
#else
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
#endif
if (length == 0 || array.Length == 0)
{

View File

@ -18,7 +18,14 @@ public static class BoolListExtensions
[Pure]
public static byte PackByte(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 8)
{
@ -45,7 +52,14 @@ public static class BoolListExtensions
[Pure]
public static short PackInt16(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 16)
{
@ -72,7 +86,14 @@ public static class BoolListExtensions
[Pure]
public static int PackInt32(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 32)
{
@ -99,7 +120,14 @@ public static class BoolListExtensions
[Pure]
public static long PackInt64(this IReadOnlyList<bool> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source.Count > 64)
{

View File

@ -34,8 +34,22 @@ public static class DictionaryExtensions
Func<TKey, TValue, TValue> updateValueFactory)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(dictionary);
#else
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(updateValueFactory);
#else
if (updateValueFactory is null)
{
throw new ArgumentNullException(nameof(updateValueFactory));
}
#endif
if (dictionary.ContainsKey(key))
{
@ -77,9 +91,30 @@ public static class DictionaryExtensions
Func<TKey, TValue> addValueFactory, Func<TKey, TValue, TValue> updateValueFactory)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(dictionary);
#else
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(addValueFactory);
#else
if (addValueFactory is null)
{
throw new ArgumentNullException(nameof(addValueFactory));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(updateValueFactory);
#else
if (updateValueFactory is null)
{
throw new ArgumentNullException(nameof(updateValueFactory));
}
#endif
if (dictionary.ContainsKey(key))
{
@ -127,9 +162,30 @@ public static class DictionaryExtensions
Func<TKey, TArg, TValue> addValueFactory, Func<TKey, TValue, TArg, TValue> updateValueFactory, TArg factoryArgument)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(dictionary);
#else
if (dictionary is null)
{
throw new ArgumentNullException(nameof(dictionary));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(addValueFactory);
#else
if (addValueFactory is null)
{
throw new ArgumentNullException(nameof(addValueFactory));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(updateValueFactory);
#else
if (updateValueFactory is null)
{
throw new ArgumentNullException(nameof(updateValueFactory));
}
#endif
if (dictionary.ContainsKey(key))
{
@ -155,7 +211,14 @@ public static class DictionaryExtensions
[Pure]
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
static string SanitizeValue(string? value)
{
@ -195,8 +258,22 @@ public static class DictionaryExtensions
public static string ToConnectionString<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source,
Func<TValue, string?> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(selector);
#else
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
#endif
static string SanitizeValue(string? value)
{
@ -242,9 +319,30 @@ public static class DictionaryExtensions
Func<TKey, string> keySelector, Func<TValue, string?> valueSelector)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(keySelector);
#else
if (keySelector is null)
{
throw new ArgumentNullException(nameof(keySelector));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(valueSelector);
#else
if (valueSelector is null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
#endif
static string SanitizeValue(string? value)
{
@ -276,7 +374,14 @@ public static class DictionaryExtensions
public static string ToGetParameters<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
static string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
{
@ -308,8 +413,22 @@ public static class DictionaryExtensions
Func<TValue, string?> selector)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(selector);
#else
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
#endif
// can't static here because of 'selector' parameter
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)
@ -347,9 +466,30 @@ public static class DictionaryExtensions
Func<TKey, string> keySelector, Func<TValue, string?> valueSelector)
where TKey : notnull
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(keySelector);
#else
if (keySelector is null)
{
throw new ArgumentNullException(nameof(keySelector));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(valueSelector);
#else
if (valueSelector is null)
{
throw new ArgumentNullException(nameof(valueSelector));
}
#endif
// can't static here because of selector parameters
string GetQueryParameter(KeyValuePair<TKey, TValue> pair)

View File

@ -139,7 +139,14 @@ public static class EnumerableExtensions
[Pure]
public static IReadOnlyCollection<T> Shuffled<T>(this IEnumerable<T> source, Random? random = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
var list = new List<T>(source);
list.Shuffle(random);

View File

@ -17,7 +17,14 @@ public static class ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static void Fill<T>(this IList<T> source, T value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
for (var i = 0; i < source.Count; i++)
{
@ -44,7 +51,14 @@ public static class ListExtensions
/// </exception>
public static void Fill<T>(this IList<T> source, T value, int startIndex, int count)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (startIndex < 0)
{
@ -78,8 +92,8 @@ public static class ListExtensions
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The source collection from which to draw.</param>
/// <param name="random">
/// The <see cref="System.Random" /> instance to use for the shuffling. If <see langword="null" /> is specified,
/// <see cref="System.Random.Shared" /> is used.
/// The <see cref="System.Random" /> instance to use for the shuffling. If <see langword="null" /> is specified, a shared
/// instance is used.
/// </param>
/// <returns>A random element of type <typeparamref name="T" /> from <paramref name="source" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
@ -92,9 +106,16 @@ public static class ListExtensions
[Pure]
public static T Random<T>(this IReadOnlyList<T> source, Random? random = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
random ??= System.Random.Shared;
random ??= RandomExtensions.GetShared();
return random.NextFrom(source);
}
@ -104,15 +125,22 @@ public static class ListExtensions
/// <typeparam name="T">The element type.</typeparam>
/// <param name="source">The <see cref="IList{T}" /> to shuffle.</param>
/// <param name="random">
/// The <see cref="System.Random" /> instance to use for the shuffling. If <see langword="null" /> is specified,
/// <see cref="System.Random.Shared" /> is used.
/// The <see cref="System.Random" /> instance to use for the shuffling. If <see langword="null" /> is specified, a shared
/// instance is used.
/// </param>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static void Shuffle<T>(this IList<T> source, Random? random = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
random ??= System.Random.Shared;
random ??= RandomExtensions.GetShared();
int count = source.Count;
while (count > 0)

View File

@ -20,7 +20,11 @@ public static class EnumExtensions
public static T Next<T>(this T value)
where T : struct, Enum
{
#if NET5_0_OR_GREATER
T[] values = Enum.GetValues<T>();
#else
T[] values = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
#endif
int index = Array.IndexOf(values, value) + 1;
index %= values.Length;
return values[index];
@ -40,7 +44,11 @@ public static class EnumExtensions
public static T NextUnchecked<T>(this T value)
where T : struct, Enum
{
#if NET5_0_OR_GREATER
T[] values = Enum.GetValues<T>();
#else
T[] values = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
#endif
int index = Array.IndexOf(values, value) + 1;
return values[index];
}
@ -58,7 +66,11 @@ public static class EnumExtensions
public static T Previous<T>(this T value)
where T : struct, Enum
{
#if NET5_0_OR_GREATER
T[] values = Enum.GetValues<T>();
#else
T[] values = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
#endif
int index = Array.IndexOf(values, value) - 1;
int length = values.Length;
@ -82,7 +94,11 @@ public static class EnumExtensions
public static T PreviousUnchecked<T>(this T value)
where T : struct, Enum
{
#if NET5_0_OR_GREATER
T[] values = Enum.GetValues<T>();
#else
T[] values = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
#endif
int index = Array.IndexOf(values, value) - 1;
return values[index];
}

View File

@ -16,7 +16,7 @@ public static class Extensions
/// An array of type <typeparamref name="T" /> with length 1, whose only element is <paramref name="value" />.
/// </returns>
[Pure]
public static T?[] AsArrayValue<T>(this T? value)
public static T[] AsArrayValue<T>(this T value)
{
return new[] {value};
}
@ -30,7 +30,7 @@ public static class Extensions
/// An enumerable collection of type <typeparamref name="T" />, whose only element is <paramref name="value" />.
/// </returns>
[Pure]
public static IEnumerable<T?> AsEnumerableValue<T>(this T? value)
public static IEnumerable<T> AsEnumerableValue<T>(this T value)
{
yield return value;
}

View File

@ -9,6 +9,8 @@ namespace X10D.Core;
/// </summary>
public static class RandomExtensions
{
private static readonly Random Shared = new();
/// <summary>
/// Returns a random value that defined in a specified enum.
/// </summary>
@ -21,7 +23,14 @@ public static class RandomExtensions
public static T Next<T>(this Random random)
where T : struct, Enum
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
var values = Enum.GetValues(typeof(T));
return (T)values.GetValue(random.Next(values.Length))!;
@ -41,7 +50,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static bool NextBoolean(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
return random.NextDouble() >= 0.5;
}
@ -61,7 +77,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static double NextDouble(this Random random, double maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
if (maxValue < 0)
{
@ -90,7 +113,14 @@ public static class RandomExtensions
/// </exception>
public static double NextDouble(this Random random, double minValue, double maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
if (maxValue < minValue)
{
@ -121,8 +151,22 @@ public static class RandomExtensions
/// </example>
public static T NextFrom<T>(this Random random, IEnumerable<T> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (source is T[] array)
{
@ -147,7 +191,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static byte NextByte(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
return random.NextByte(byte.MaxValue);
}
@ -168,7 +219,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static byte NextByte(this Random random, byte maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
return random.NextByte(0, maxValue);
}
@ -194,7 +252,14 @@ public static class RandomExtensions
/// </exception>
public static byte NextByte(this Random random, byte minValue, byte maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
return (byte)random.Next(minValue, maxValue);
}
@ -209,7 +274,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static short NextInt16(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
return random.NextInt16(short.MaxValue);
}
@ -231,7 +303,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentOutOfRangeException"><paramref name="maxValue" /> is less than 0.</exception>
public static short NextInt16(this Random random, short maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
if (maxValue < 0)
{
@ -262,11 +341,36 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static short NextInt16(this Random random, short minValue, short maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
return (short)random.Next(minValue, maxValue);
}
#if !NET6_0_OR_GREATER
/// <summary>
/// Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.
/// </summary>
/// <param name="random">The <see cref="System.Random" /> instance.</param>
/// <returns>A single-precision floating point number that is greater than or equal to 0.0, and less than 1.0.</returns>
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static float NextSingle(this Random random)
{
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
return (float)random.NextDouble();
}
#endif
/// <summary>
/// Returns a non-negative random single-precision floating point number that is less than the specified maximum.
/// </summary>
@ -282,7 +386,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentException"><paramref name="maxValue" /> is less than 0.</exception>
public static float NextSingle(this Random random, float maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
if (maxValue < 0)
{
@ -311,7 +422,14 @@ public static class RandomExtensions
/// </exception>
public static float NextSingle(this Random random, float minValue, float maxValue)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
if (maxValue < minValue)
{
@ -339,8 +457,22 @@ public static class RandomExtensions
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is less than 0.</exception>
public static string NextString(this Random random, IReadOnlyList<char> source, int length)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (length < 0)
{
@ -365,4 +497,13 @@ public static class RandomExtensions
return builder.ToString();
}
internal static Random GetShared()
{
#if NET6_0_OR_GREATER
return Random.Shared;
#else
return Shared;
#endif
}
}

View File

@ -15,7 +15,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColorRgb(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
int rgb = random.Next();
return Color.FromArgb(0xFF, (byte)(rgb >> 16 & 0xFF), (byte)(rgb >> 8 & 0xFF), (byte)(rgb & 0xFF));
@ -29,7 +36,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Color NextColorArgb(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
int argb = random.Next();
return Color.FromArgb(argb);

View File

@ -1,5 +1,6 @@
using System.Buffers.Binary;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
namespace X10D.IO;
@ -55,8 +56,11 @@ public static class DoubleExtensions
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this double value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteDoubleBigEndian(destination, value)
: BinaryPrimitives.TryWriteDoubleLittleEndian(destination, value);
if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian))
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
}
return MemoryMarshal.TryWrite(destination, ref value);
}
}

View File

@ -29,7 +29,14 @@ public static class FileInfoExtensions
public static byte[] GetHash<T>(this FileInfo value)
where T : HashAlgorithm
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
using FileStream stream = value.OpenRead();
return stream.GetHash<T>();
@ -62,7 +69,14 @@ public static class FileInfoExtensions
public static bool TryWriteHash<T>(this FileInfo value, Span<byte> destination, out int bytesWritten)
where T : HashAlgorithm
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
using FileStream stream = value.OpenRead();
return stream.TryWriteHash<T>(destination, out bytesWritten);

View File

@ -19,7 +19,14 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static string AsString(this IReadOnlyList<byte> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToString(source.ToArray());
}
@ -47,7 +54,14 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double ToDouble(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToDouble(source.ToArray(), startIndex);
}
@ -72,7 +86,14 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static short ToInt16(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToInt16(source.ToArray(), startIndex);
}
@ -97,7 +118,14 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int ToInt32(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToInt32(source.ToArray(), startIndex);
}
@ -122,7 +150,14 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long ToInt64(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToInt64(source.ToArray(), startIndex);
}
@ -149,7 +184,14 @@ public static class ListOfByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float ToSingle(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToSingle(source.ToArray(), startIndex);
}
@ -167,8 +209,22 @@ public static class ListOfByteExtensions
/// </exception>
public static string ToString(this IReadOnlyList<byte> source, Encoding encoding)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(encoding);
#else
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
#endif
return encoding.GetString(source.ToArray());
}
@ -195,7 +251,14 @@ public static class ListOfByteExtensions
[CLSCompliant(false)]
public static ushort ToUInt16(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToUInt16(source.ToArray(), startIndex);
}
@ -222,7 +285,14 @@ public static class ListOfByteExtensions
[CLSCompliant(false)]
public static uint ToUInt32(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToUInt32(source.ToArray(), startIndex);
}
@ -249,7 +319,14 @@ public static class ListOfByteExtensions
[CLSCompliant(false)]
public static ulong ToUInt64(this IReadOnlyList<byte> source, int startIndex)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return BitConverter.ToUInt64(source.ToArray(), startIndex);
}

View File

@ -1,5 +1,6 @@
using System.Buffers.Binary;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
namespace X10D.IO;
@ -55,8 +56,11 @@ public static class SingleExtensions
/// <returns><see langword="true" /> if the conversion was successful; otherwise, <see langword="false" />.</returns>
public static bool TryWriteBytes(this float value, Span<byte> destination, Endianness endianness)
{
return endianness == Endianness.BigEndian
? BinaryPrimitives.TryWriteSingleBigEndian(destination, value)
: BinaryPrimitives.TryWriteSingleLittleEndian(destination, value);
if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian))
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value));
}
return MemoryMarshal.TryWrite(destination, ref value);
}
}

View File

@ -1,5 +1,6 @@
using System.Buffers.Binary;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
namespace X10D.IO;
@ -31,10 +32,14 @@ public static class StreamExtensions
public static byte[] GetHash<T>(this Stream stream)
where T : HashAlgorithm
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
if (!stream.CanRead)
{
@ -81,15 +86,26 @@ public static class StreamExtensions
/// <returns>A decimal value read from the stream.</returns>
public static decimal ReadDecimal(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
const int decimalSize = sizeof(decimal);
const int int32Size = sizeof(int);
@ -129,22 +145,38 @@ public static class StreamExtensions
/// <returns>A double-precision floating point value read from the stream.</returns>
public static double ReadDouble(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(double)];
stream.Read(buffer);
return endianness == Endianness.LittleEndian
? BinaryPrimitives.ReadDoubleLittleEndian(buffer)
: BinaryPrimitives.ReadDoubleBigEndian(buffer);
var value = MemoryMarshal.Read<double>(buffer);
if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian))
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
}
return value;
}
/// <summary>
@ -167,15 +199,26 @@ public static class StreamExtensions
/// <returns>An two-byte unsigned integer read from the stream.</returns>
public static short ReadInt16(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(short)];
stream.Read(buffer);
@ -205,15 +248,26 @@ public static class StreamExtensions
/// <returns>An four-byte unsigned integer read from the stream.</returns>
public static int ReadInt32(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(int)];
stream.Read(buffer);
@ -243,15 +297,26 @@ public static class StreamExtensions
/// <returns>An eight-byte unsigned integer read from the stream.</returns>
public static long ReadInt64(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(long)];
stream.Read(buffer);
@ -281,22 +346,38 @@ public static class StreamExtensions
/// <returns>A single-precision floating point value read from the stream.</returns>
public static double ReadSingle(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(float)];
stream.Read(buffer);
return endianness == Endianness.LittleEndian
? BinaryPrimitives.ReadSingleLittleEndian(buffer)
: BinaryPrimitives.ReadSingleBigEndian(buffer);
var value = MemoryMarshal.Read<float>(buffer);
if (BitConverter.IsLittleEndian == (endianness == Endianness.BigEndian))
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value));
}
return value;
}
/// <summary>
@ -321,15 +402,26 @@ public static class StreamExtensions
[CLSCompliant(false)]
public static ushort ReadUInt16(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(ushort)];
stream.Read(buffer);
@ -361,15 +453,26 @@ public static class StreamExtensions
[CLSCompliant(false)]
public static uint ReadUInt32(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(uint)];
stream.Read(buffer);
@ -401,15 +504,26 @@ public static class StreamExtensions
[CLSCompliant(false)]
public static ulong ReadUInt64(this Stream stream, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(ulong)];
stream.Read(buffer);
@ -445,10 +559,14 @@ public static class StreamExtensions
public static bool TryWriteHash<T>(this Stream stream, Span<byte> destination, out int bytesWritten)
where T : HashAlgorithm
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
if (!stream.CanRead)
{
@ -504,12 +622,26 @@ public static class StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, short value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(short)];
@ -548,12 +680,26 @@ public static class StreamExtensions
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, int value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -593,12 +739,26 @@ public static class StreamExtensions
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, long value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(long)];
@ -640,12 +800,26 @@ public static class StreamExtensions
[CLSCompliant(false)]
public static int Write(this Stream stream, ushort value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(ushort)];
@ -687,12 +861,26 @@ public static class StreamExtensions
[CLSCompliant(false)]
public static int Write(this Stream stream, uint value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(uint)];
@ -734,12 +922,26 @@ public static class StreamExtensions
[CLSCompliant(false)]
public static int Write(this Stream stream, ulong value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(ulong)];
@ -766,22 +968,54 @@ public static class StreamExtensions
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, float value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(float)];
if (endianness == Endianness.LittleEndian)
{
#if NET5_0_OR_GREATER
BinaryPrimitives.WriteSingleLittleEndian(buffer, value);
#else
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value));
}
System.Runtime.InteropServices.MemoryMarshal.Write(buffer, ref value);
#endif
}
else
{
#if NET5_0_OR_GREATER
BinaryPrimitives.WriteSingleBigEndian(buffer, value);
#else
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.SingleToInt32Bits(value));
}
System.Runtime.InteropServices.MemoryMarshal.Write(buffer, ref value);
#endif
}
return stream.WriteInternal(buffer);
@ -798,22 +1032,54 @@ public static class StreamExtensions
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, double value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
Span<byte> buffer = stackalloc byte[sizeof(double)];
if (endianness == Endianness.LittleEndian)
{
#if NET5_0_OR_GREATER
BinaryPrimitives.WriteDoubleLittleEndian(buffer, value);
#else
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
}
System.Runtime.InteropServices.MemoryMarshal.Write(buffer, ref value);
#endif
}
else
{
#if NET5_0_OR_GREATER
BinaryPrimitives.WriteDoubleBigEndian(buffer, value);
#else
if (BitConverter.IsLittleEndian)
{
value = BinaryPrimitives.ReverseEndianness(BitConverter.DoubleToInt64Bits(value));
}
System.Runtime.InteropServices.MemoryMarshal.Write(buffer, ref value);
#endif
}
return stream.WriteInternal(buffer);
@ -830,12 +1096,26 @@ public static class StreamExtensions
/// <exception cref="ArgumentNullException"><paramref name="stream" /> is <see langword="null" />.</exception>
public static int Write(this Stream stream, decimal value, Endianness endianness)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(stream);
#else
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
#endif
#if NET5_0_OR_GREATER
if (!Enum.IsDefined(endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#else
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#endif
int[] bits = decimal.GetBits(value);
long preWritePosition = stream.Position;

View File

@ -15,7 +15,14 @@ public static class ByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static byte Product(this IEnumerable<byte> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate((byte)1, (current, value) => (byte)(current * value));
}
@ -29,7 +36,14 @@ public static class ByteExtensions
[CLSCompliant(false)]
public static sbyte Product(this IEnumerable<sbyte> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate((sbyte)1, (current, value) => (sbyte)(current * value));
}
@ -45,7 +59,14 @@ public static class ByteExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static byte Product<TSource>(this IEnumerable<TSource> source, Func<TSource, byte> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}
@ -62,7 +83,14 @@ public static class ByteExtensions
[CLSCompliant(false)]
public static sbyte Product<TSource>(this IEnumerable<TSource> source, Func<TSource, sbyte> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}

View File

@ -13,7 +13,14 @@ public static class DecimalExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static decimal Product(this IEnumerable<decimal> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1m, (current, value) => (current * value));
}
@ -29,7 +36,14 @@ public static class DecimalExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static decimal Product<TSource>(this IEnumerable<TSource> source, Func<TSource, decimal> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}

View File

@ -13,7 +13,14 @@ public static class DoubleExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double Product(this IEnumerable<double> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1.0, (current, value) => (current * value));
}
@ -29,7 +36,14 @@ public static class DoubleExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double Product<TSource>(this IEnumerable<TSource> source, Func<TSource, double> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}

View File

@ -15,7 +15,14 @@ public static class Int32Extensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int Product(this IEnumerable<int> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1, (current, value) => current * value);
}
@ -29,7 +36,14 @@ public static class Int32Extensions
[CLSCompliant(false)]
public static uint Product(this IEnumerable<uint> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1u, (current, value) => current * value);
}
@ -45,7 +59,14 @@ public static class Int32Extensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int Product<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}
@ -62,7 +83,14 @@ public static class Int32Extensions
[CLSCompliant(false)]
public static uint Product<TSource>(this IEnumerable<TSource> source, Func<TSource, uint> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}

View File

@ -15,7 +15,14 @@ public static class Int64Extensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long Product(this IEnumerable<long> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1L, (current, value) => current * value);
}
@ -29,7 +36,14 @@ public static class Int64Extensions
[CLSCompliant(false)]
public static ulong Product(this IEnumerable<ulong> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1UL, (current, value) => current * value);
}
@ -45,7 +59,14 @@ public static class Int64Extensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long Product<TSource>(this IEnumerable<TSource> source, Func<TSource, long> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}
@ -62,7 +83,14 @@ public static class Int64Extensions
[CLSCompliant(false)]
public static ulong Product<TSource>(this IEnumerable<TSource> source, Func<TSource, ulong> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}

View File

@ -21,7 +21,14 @@ public static class ReadOnlySpanExtensions
[Pure]
public static bool All<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(predicate);
#else
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (source.IsEmpty)
{
@ -53,7 +60,14 @@ public static class ReadOnlySpanExtensions
[Pure]
public static bool Any<TSource>(this ReadOnlySpan<TSource> source, Predicate<TSource> predicate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(predicate);
#else
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (source.IsEmpty)
{

View File

@ -13,7 +13,14 @@ public static class SingleExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float Product(this IEnumerable<float> source)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Aggregate(1f, (current, value) => (current * value));
}
@ -29,7 +36,14 @@ public static class SingleExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float Product<TSource>(this IEnumerable<TSource> source, Func<TSource, float> selector)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
return source.Select(selector).Product();
}

View File

@ -21,7 +21,14 @@ public static class SpanExtensions
[Pure]
public static bool All<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(predicate);
#else
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (source.IsEmpty)
{
@ -53,7 +60,14 @@ public static class SpanExtensions
[Pure]
public static bool Any<TSource>(this Span<TSource> source, Predicate<TSource> predicate)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(predicate);
#else
if (predicate is null)
{
throw new ArgumentNullException(nameof(predicate));
}
#endif
if (source.IsEmpty)
{

View File

@ -19,7 +19,11 @@ public static class ByteExtensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte DigitalRoot(this byte value)
{
int root = value % 9;
@ -32,7 +36,11 @@ public static class ByteExtensions
/// <param name="value">The value whose factorial to compute.</param>
/// <returns>The factorial of <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Factorial(this byte value)
{
if (value == 0)
@ -58,7 +66,11 @@ public static class ByteExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this byte value)
{
return value % 2 == 0;
@ -73,7 +85,11 @@ public static class ByteExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this byte value)
{
return !value.IsEven();
@ -101,7 +117,11 @@ public static class ByteExtensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this byte value)
{
return ((long)value).MultiplicativePersistence();

View File

@ -48,14 +48,25 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool Between<T1, T2, T3>(this T1 value, T2 lower, T3 upper,
InclusiveOptions inclusiveOptions = InclusiveOptions.None)
where T1 : IComparable<T2>, IComparable<T3>
where T2 : IComparable<T3>
where T3 : IComparable<T2>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
if (lower.GreaterThan(upper))
{
@ -102,11 +113,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static T Clamp<T>(this T value, T lower, T upper)
where T : IComparable<T>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
if (lower.GreaterThan(upper))
{
@ -141,11 +163,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool GreaterThan<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.CompareTo(other) > 0;
}
@ -173,11 +206,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool GreaterThanOrEqualTo<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.CompareTo(other) >= 0;
}
@ -205,11 +249,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool LessThan<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.CompareTo(other) < 0;
}
@ -237,11 +292,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool LessThanOrEqualTo<T1, T2>(this T1 value, T2 other)
where T1 : IComparable<T2>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.CompareTo(other) <= 0;
}
@ -268,11 +334,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static T Max<T>(this T value, T other)
where T : IComparable<T>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.GreaterThan(other) ? value : other;
}
@ -299,11 +376,22 @@ public static class ComparableExtensions
/// </example>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static T Min<T>(this T value, T other)
where T : IComparable<T>
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.LessThan(other) ? value : other;
}

View File

@ -9,6 +9,7 @@ namespace X10D.Math;
/// </summary>
public static class DecimalExtensions
{
#if NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Returns the complex square root of this decimal number.
/// </summary>
@ -20,6 +21,7 @@ public static class DecimalExtensions
{
return Complex.Sqrt((double)value);
}
#endif
/// <summary>
/// Returns a value indicating whether the current value is evenly divisible by 2.
@ -30,7 +32,11 @@ public static class DecimalExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this decimal value)
{
return value % 2.0m == 0.0m;
@ -45,7 +51,11 @@ public static class DecimalExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this decimal value)
{
return !value.IsEven();
@ -57,7 +67,11 @@ public static class DecimalExtensions
/// <param name="value">The value to round.</param>
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static decimal Round(this decimal value)
{
return value.Round(1.0m);
@ -70,7 +84,11 @@ public static class DecimalExtensions
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static decimal Round(this decimal value, decimal nearest)
{
return System.Math.Round(value / nearest) * nearest;
@ -104,7 +122,11 @@ public static class DecimalExtensions
/// </list>
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this decimal value)
{
return System.Math.Sign(value);
@ -138,13 +160,12 @@ public static class DecimalExtensions
/// </list>
/// </returns>
/// <exception cref="ArgumentException"><paramref name="value" /> is negative.</exception>
/// <remarks>
/// For negative input, this method returns <see cref="double.NaN" />. To receive a complex number, see
/// <see cref="Numerics.DoubleExtensions.ComplexSqrt" />.
/// </remarks>
/// <seealso cref="Numerics.DoubleExtensions.ComplexSqrt" />
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static decimal Sqrt(this decimal value)
{
switch (value)

View File

@ -20,7 +20,11 @@ public static class DoubleExtensions
/// is equal to <see cref="double.NaN" />, less than -1, or greater than 1, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Acos(this double value)
{
return System.Math.Acos(value);
@ -38,7 +42,11 @@ public static class DoubleExtensions
/// <paramref name="value" /> is less than 1 or equal to <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Acosh(this double value)
{
return System.Math.Acosh(value);
@ -56,7 +64,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Asin(this double value)
{
return System.Math.Asin(value);
@ -74,7 +86,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Asinh(this double value)
{
return System.Math.Asinh(value);
@ -91,7 +107,11 @@ public static class DoubleExtensions
/// <paramref name="value" /> is equal to <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Atan(this double value)
{
return System.Math.Atan(value);
@ -110,12 +130,17 @@ public static class DoubleExtensions
/// <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Atanh(this double value)
{
return System.Math.Atanh(value);
}
#if NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Returns the complex square root of this double-precision floating-point number.
/// </summary>
@ -141,6 +166,7 @@ public static class DoubleExtensions
return new Complex(0, System.Math.Sqrt(-value));
}
}
#endif
/// <summary>
/// Returns the cosine of the specified angle.
@ -152,7 +178,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Cos(this double value)
{
return System.Math.Cos(value);
@ -169,7 +199,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Cosh(this double value)
{
return System.Math.Cosh(value);
@ -181,7 +215,11 @@ public static class DoubleExtensions
/// <param name="value">The angle in degrees to convert.</param>
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double DegreesToRadians(this double value)
{
return value * (System.Math.PI / 180.0);
@ -196,7 +234,11 @@ public static class DoubleExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this double value)
{
return System.Math.Abs(value % 2.0) < double.Epsilon;
@ -211,7 +253,11 @@ public static class DoubleExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this double value)
{
return !value.IsEven();
@ -223,7 +269,11 @@ public static class DoubleExtensions
/// <param name="value">The angle in radians to convert.</param>
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double RadiansToDegrees(this double value)
{
return value * (180.0 / System.Math.PI);
@ -235,7 +285,11 @@ public static class DoubleExtensions
/// <param name="value">The value to round.</param>
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Round(this double value)
{
return value.Round(1.0);
@ -248,7 +302,11 @@ public static class DoubleExtensions
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Round(this double value, double nearest)
{
return System.Math.Round(value / nearest) * nearest;
@ -264,7 +322,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Sin(this double value)
{
return System.Math.Sin(value);
@ -280,7 +342,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Sinh(this double value)
{
return System.Math.Sinh(value);
@ -315,7 +381,11 @@ public static class DoubleExtensions
/// </returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is equal to <see cref="double.NaN" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this double value)
{
return System.Math.Sign(value);
@ -348,15 +418,14 @@ public static class DoubleExtensions
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// For negative input, this method returns <see cref="double.NaN" />. To receive a complex number, see
/// <see cref="Numerics.DoubleExtensions.ComplexSqrt" />.
/// </remarks>
/// <seealso cref="Numerics.DoubleExtensions.ComplexSqrt" />
/// <author>SLenik https://stackoverflow.com/a/6755197/1467293</author>
/// <license>CC BY-SA 3.0</license>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Sqrt(this double value)
{
switch (value)
@ -395,7 +464,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Tan(this double value)
{
return System.Math.Tan(value);
@ -412,7 +485,11 @@ public static class DoubleExtensions
/// <see cref="double.NaN" />, this method returns <see cref="double.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Tanh(this double value)
{
return System.Math.Tanh(value);

View File

@ -18,7 +18,11 @@ public static class Int16Extensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short DigitalRoot(this short value)
{
short root = System.Math.Abs(value).Mod(9);
@ -32,7 +36,11 @@ public static class Int16Extensions
/// <returns>The factorial of <paramref name="value" />.</returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is less than 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Factorial(this short value)
{
if (value < 0)
@ -63,7 +71,11 @@ public static class Int16Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this short value)
{
return (value & 1) == 0;
@ -78,7 +90,11 @@ public static class Int16Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this short value)
{
return !value.IsEven();
@ -92,7 +108,11 @@ public static class Int16Extensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this short value)
{
return ((long)value).IsPrime();
@ -113,7 +133,11 @@ public static class Int16Extensions
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short Mod(this short dividend, short divisor)
{
int r = dividend % divisor;
@ -129,7 +153,11 @@ public static class Int16Extensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this short value)
{
return ((long)value).MultiplicativePersistence();
@ -163,7 +191,11 @@ public static class Int16Extensions
/// </list>
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this short value)
{
return System.Math.Sign(value);

View File

@ -18,7 +18,11 @@ public static class Int32Extensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int DigitalRoot(this int value)
{
int root = System.Math.Abs(value).Mod(9);
@ -32,7 +36,11 @@ public static class Int32Extensions
/// <returns>The factorial of <paramref name="value" />.</returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is less than 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Factorial(this int value)
{
if (value < 0)
@ -63,7 +71,11 @@ public static class Int32Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this int value)
{
return (value & 1) == 0;
@ -78,7 +90,11 @@ public static class Int32Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this int value)
{
return !value.IsEven();
@ -92,7 +108,11 @@ public static class Int32Extensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this int value)
{
return ((long)value).IsPrime();
@ -113,7 +133,11 @@ public static class Int32Extensions
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Mod(this int dividend, int divisor)
{
int r = dividend % divisor;
@ -129,7 +153,11 @@ public static class Int32Extensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this int value)
{
return ((long)value).MultiplicativePersistence();
@ -163,7 +191,11 @@ public static class Int32Extensions
/// </list>
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this int value)
{
return System.Math.Sign(value);

View File

@ -18,7 +18,11 @@ public static class Int64Extensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long DigitalRoot(this long value)
{
long root = System.Math.Abs(value).Mod(9L);
@ -32,7 +36,11 @@ public static class Int64Extensions
/// <returns>The factorial of <paramref name="value" />.</returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is less than 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Factorial(this long value)
{
if (value < 0)
@ -63,7 +71,11 @@ public static class Int64Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this long value)
{
return (value & 1) == 0;
@ -78,7 +90,11 @@ public static class Int64Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this long value)
{
return !value.IsEven();
@ -92,7 +108,11 @@ public static class Int64Extensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this long value)
{
switch (value)
@ -138,7 +158,11 @@ public static class Int64Extensions
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Mod(this long dividend, long divisor)
{
long r = dividend % divisor;
@ -154,7 +178,11 @@ public static class Int64Extensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this long value)
{
var persistence = 0;
@ -219,7 +247,11 @@ public static class Int64Extensions
/// </list>
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this long value)
{
return System.Math.Sign(value);

View File

@ -18,7 +18,11 @@ public static class MathUtility
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Lerp(float value, float target, float alpha)
{
// rookie mistake: a + t * (b - a)
@ -36,7 +40,11 @@ public static class MathUtility
/// The interpolation result as determined by <c>(1 - alpha) * value + alpha * target</c>.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static double Lerp(double value, double target, double alpha)
{
// rookie mistake: a + t * (b - a)

View File

@ -19,7 +19,11 @@ public static class SByteExtensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static sbyte DigitalRoot(this sbyte value)
{
int root = System.Math.Abs(value).Mod(9);
@ -33,7 +37,11 @@ public static class SByteExtensions
/// <returns>The factorial of <paramref name="value" />.</returns>
/// <exception cref="ArithmeticException"><paramref name="value" /> is less than 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long Factorial(this sbyte value)
{
if (value < 0)
@ -64,7 +72,11 @@ public static class SByteExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this sbyte value)
{
return value % 2 == 0;
@ -79,7 +91,11 @@ public static class SByteExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this sbyte value)
{
return !value.IsEven();
@ -93,7 +109,11 @@ public static class SByteExtensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this sbyte value)
{
return ((long)value).IsPrime();
@ -114,7 +134,11 @@ public static class SByteExtensions
/// <author>ShreevatsaR, https://stackoverflow.com/a/1082938/1467293</author>
/// <license>CC-BY-SA 2.5</license>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static sbyte Mod(this sbyte dividend, sbyte divisor)
{
int r = dividend % divisor;
@ -130,7 +154,11 @@ public static class SByteExtensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this sbyte value)
{
return ((long)value).MultiplicativePersistence();
@ -164,7 +192,11 @@ public static class SByteExtensions
/// </list>
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this sbyte value)
{
return System.Math.Sign(value);

View File

@ -20,7 +20,11 @@ public static class SingleExtensions
/// is equal to <see cref="float.NaN" />, less than -1, or greater than 1, <see cref="float.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Acos(this float value)
{
return MathF.Acos(value);
@ -38,7 +42,11 @@ public static class SingleExtensions
/// <paramref name="value" /> is less than 1 or equal to <see cref="float.NaN" />, <see cref="float.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Acosh(this float value)
{
return MathF.Acosh(value);
@ -56,7 +64,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Asin(this float value)
{
return MathF.Asin(value);
@ -74,7 +86,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" />, <see cref="float.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Asinh(this float value)
{
return MathF.Asinh(value);
@ -91,7 +107,11 @@ public static class SingleExtensions
/// <paramref name="value" /> is equal to <see cref="float.NaN" />, <see cref="float.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Atan(this float value)
{
return MathF.Atan(value);
@ -110,12 +130,17 @@ public static class SingleExtensions
/// <see cref="float.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Atanh(this float value)
{
return MathF.Atanh(value);
}
#if NETCOREAPP3_0_OR_GREATER
/// <summary>
/// Returns the complex square root of this single-precision floating-point number.
/// </summary>
@ -141,6 +166,7 @@ public static class SingleExtensions
return new Complex(0, MathF.Sqrt(-value));
}
}
#endif
/// <summary>
/// Returns the cosine of the specified angle.
@ -152,7 +178,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Cos(this float value)
{
return MathF.Cos(value);
@ -169,7 +199,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" />, <see cref="double.NaN" /> is returned.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Cosh(this float value)
{
return MathF.Cosh(value);
@ -181,7 +215,11 @@ public static class SingleExtensions
/// <param name="value">The angle in degrees to convert.</param>
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float DegreesToRadians(this float value)
{
return value * (MathF.PI / 180.0f);
@ -196,7 +234,11 @@ public static class SingleExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this float value)
{
return value % 2 == 0;
@ -211,7 +253,11 @@ public static class SingleExtensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this float value)
{
return !value.IsEven();
@ -223,7 +269,11 @@ public static class SingleExtensions
/// <param name="value">The angle in radians to convert.</param>
/// <returns>The result of π * <paramref name="value" /> / 180.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float RadiansToDegrees(this float value)
{
return value * (180.0f / MathF.PI);
@ -235,7 +285,11 @@ public static class SingleExtensions
/// <param name="value">The value to round.</param>
/// <returns><paramref name="value" /> rounded to the nearest whole number.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Round(this float value)
{
return value.Round(1.0f);
@ -248,7 +302,11 @@ public static class SingleExtensions
/// <param name="nearest">The nearest multiple to which <paramref name="value" /> should be rounded.</param>
/// <returns><paramref name="value" /> rounded to the nearest multiple of <paramref name="nearest" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Round(this float value, float nearest)
{
return MathF.Round(value / nearest) * nearest;
@ -282,7 +340,11 @@ public static class SingleExtensions
/// </list>
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Sign(this float value)
{
return MathF.Sign(value);
@ -315,15 +377,14 @@ public static class SingleExtensions
/// </item>
/// </list>
/// </returns>
/// <remarks>
/// For negative input, this method returns <see cref="float.NaN" />. To receive a complex number, see
/// <see cref="Numerics.SingleExtensions.ComplexSqrt" />.
/// </remarks>
/// <seealso cref="Numerics.SingleExtensions.ComplexSqrt" />
/// <author>SLenik https://stackoverflow.com/a/6755197/1467293</author>
/// <license>CC BY-SA 3.0</license>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Sqrt(this float value)
{
switch (value)
@ -362,7 +423,11 @@ public static class SingleExtensions
/// <see cref="double.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Sin(this float value)
{
return MathF.Sin(value);
@ -378,7 +443,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Sinh(this float value)
{
return MathF.Sinh(value);
@ -394,7 +463,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Tan(this float value)
{
return MathF.Sin(value);
@ -411,7 +484,11 @@ public static class SingleExtensions
/// <see cref="float.NaN" />, this method returns <see cref="float.NaN" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static float Tanh(this float value)
{
return MathF.Tanh(value);

View File

@ -19,7 +19,11 @@ public static class UInt16Extensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ushort DigitalRoot(this ushort value)
{
var root = (ushort)(value % 9);
@ -32,7 +36,11 @@ public static class UInt16Extensions
/// <param name="value">The value whose factorial to compute.</param>
/// <returns>The factorial of <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Factorial(this ushort value)
{
if (value == 0)
@ -58,7 +66,11 @@ public static class UInt16Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this ushort value)
{
return (value & 1) == 0;
@ -72,7 +84,11 @@ public static class UInt16Extensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this ushort value)
{
return ((ulong)value).IsPrime();
@ -87,7 +103,11 @@ public static class UInt16Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this ushort value)
{
return !value.IsEven();
@ -102,7 +122,11 @@ public static class UInt16Extensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this ushort value)
{
return ((ulong)value).MultiplicativePersistence();

View File

@ -19,7 +19,11 @@ public static class UInt32Extensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static uint DigitalRoot(this uint value)
{
uint root = value % 9;
@ -32,7 +36,11 @@ public static class UInt32Extensions
/// <param name="value">The value whose factorial to compute.</param>
/// <returns>The factorial of <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Factorial(this uint value)
{
if (value == 0)
@ -58,7 +66,11 @@ public static class UInt32Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this uint value)
{
return (value & 1) == 0;
@ -72,7 +84,11 @@ public static class UInt32Extensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this uint value)
{
return ((ulong)value).IsPrime();
@ -87,7 +103,11 @@ public static class UInt32Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this uint value)
{
return !value.IsEven();
@ -102,7 +122,11 @@ public static class UInt32Extensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this uint value)
{
return ((ulong)value).MultiplicativePersistence();

View File

@ -19,7 +19,11 @@ public static class UInt64Extensions
/// <para>For example, the digital root of 239 is 5: <c>2 + 3 + 9 = 14</c>, then <c>1 + 4 = 5</c>.</para>
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong DigitalRoot(this ulong value)
{
ulong root = value % 9;
@ -32,7 +36,11 @@ public static class UInt64Extensions
/// <param name="value">The value whose factorial to compute.</param>
/// <returns>The factorial of <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong Factorial(this ulong value)
{
if (value == 0)
@ -58,7 +66,11 @@ public static class UInt64Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsEven(this ulong value)
{
return (value & 1) == 0;
@ -72,7 +84,11 @@ public static class UInt64Extensions
/// <see langword="true" /> if <paramref name="value" /> is prime; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPrime(this ulong value)
{
switch (value)
@ -112,7 +128,11 @@ public static class UInt64Extensions
/// otherwise.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsOdd(this ulong value)
{
return !value.IsEven();
@ -127,7 +147,11 @@ public static class UInt64Extensions
/// Multiplicative persistence is defined as the recursive digital product until that product is a single digit.
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int MultiplicativePersistence(this ulong value)
{
var persistence = 0;

View File

@ -22,10 +22,21 @@ public static class EndPointExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="endPoint" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string GetHost(this EndPoint endPoint)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(endPoint);
#else
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
#endif
return endPoint switch
{
@ -48,10 +59,21 @@ public static class EndPointExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="endPoint" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int GetPort(this EndPoint endPoint)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(endPoint);
#else
if (endPoint is null)
{
throw new ArgumentNullException(nameof(endPoint));
}
#endif
return endPoint switch
{

View File

@ -19,10 +19,21 @@ public static class IPAddressExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="address" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsIPv4(this IPAddress address)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(address);
#else
if (address is null)
{
throw new ArgumentNullException(nameof(address));
}
#endif
return address.AddressFamily == AddressFamily.InterNetwork;
}
@ -36,10 +47,21 @@ public static class IPAddressExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="address" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsIPv6(this IPAddress address)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(address);
#else
if (address is null)
{
throw new ArgumentNullException(nameof(address));
}
#endif
return address.AddressFamily == AddressFamily.InterNetworkV6;
}

View File

@ -15,7 +15,11 @@ public static class Int16Extensions
/// <param name="value">The value to convert, expressed in host byte order.</param>
/// <returns>An integer value, expressed in network byte order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short HostToNetworkOrder(this short value)
{
return IPAddress.HostToNetworkOrder(value);
@ -27,7 +31,11 @@ public static class Int16Extensions
/// <param name="value">The value to convert, expressed in network byte order.</param>
/// <returns>An integer value, expressed in host byte order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short NetworkToHostOrder(this short value)
{
return IPAddress.NetworkToHostOrder(value);

View File

@ -15,7 +15,11 @@ public static class Int32Extensions
/// <param name="value">The value to convert, expressed in host byte order.</param>
/// <returns>An integer value, expressed in network byte order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int HostToNetworkOrder(this int value)
{
return IPAddress.HostToNetworkOrder(value);
@ -27,7 +31,11 @@ public static class Int32Extensions
/// <param name="value">The value to convert, expressed in network byte order.</param>
/// <returns>An integer value, expressed in host byte order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int NetworkToHostOrder(this int value)
{
return IPAddress.NetworkToHostOrder(value);

View File

@ -15,7 +15,11 @@ public static class Int64Extensions
/// <param name="value">The value to convert, expressed in host byte order.</param>
/// <returns>An integer value, expressed in network byte order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long HostToNetworkOrder(this long value)
{
return IPAddress.HostToNetworkOrder(value);
@ -27,7 +31,11 @@ public static class Int64Extensions
/// <param name="value">The value to convert, expressed in network byte order.</param>
/// <returns>An integer value, expressed in host byte order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long NetworkToHostOrder(this long value)
{
return IPAddress.NetworkToHostOrder(value);

View File

@ -18,7 +18,11 @@ public static class ByteExtensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte RotateLeft(this byte value, int count)
{
count = count.Mod(8);
@ -34,7 +38,11 @@ public static class ByteExtensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte RotateRight(this byte value, int count)
{
count = count.Mod(8);

View File

@ -17,7 +17,11 @@ public static class Int16Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short RotateLeft(this short value, int count)
{
var unsigned = unchecked((ushort)value);
@ -33,7 +37,11 @@ public static class Int16Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static short RotateRight(this short value, int count)
{
var unsigned = unchecked((ushort)value);

View File

@ -17,7 +17,11 @@ public static class Int32Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int RotateLeft(this int value, int count)
{
var unsigned = unchecked((uint)value);
@ -33,7 +37,11 @@ public static class Int32Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int RotateRight(this int value, int count)
{
var unsigned = unchecked((uint)value);

View File

@ -17,7 +17,11 @@ public static class Int64Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long RotateLeft(this long value, int count)
{
var unsigned = unchecked((ulong)value);
@ -33,7 +37,11 @@ public static class Int64Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long RotateRight(this long value, int count)
{
var unsigned = unchecked((ulong)value);

View File

@ -19,7 +19,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotation(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
int seed = random.Next();
var seededRandom = new Random(seed);
@ -39,7 +46,14 @@ public static class RandomExtensions
/// <exception cref="ArgumentNullException"><paramref name="random" /> is <see langword="null" />.</exception>
public static Quaternion NextRotationUniform(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
int seed = random.Next();
var seededRandom = new Random(seed);
@ -68,7 +82,14 @@ public static class RandomExtensions
/// </returns>
public static Vector2 NextUnitVector2(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
// no need to construct a seeded random here, since we only call Next once
@ -89,7 +110,14 @@ public static class RandomExtensions
/// </returns>
public static Vector3 NextUnitVector3(this Random random)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(random);
#else
if (random is null)
{
throw new ArgumentNullException(nameof(random));
}
#endif
int seed = random.Next();
var seededRandom = new Random(seed);

View File

@ -18,7 +18,11 @@ public static class SByteExtensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static sbyte RotateLeft(this sbyte value, int count)
{
var signed = unchecked((byte)value);
@ -34,7 +38,11 @@ public static class SByteExtensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static sbyte RotateRight(this sbyte value, int count)
{
var signed = unchecked((byte)value);

View File

@ -18,7 +18,11 @@ public static class UInt16Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ushort RotateLeft(this ushort value, int count)
{
return (ushort)((ushort)(value << count) | (ushort)(value >> (16 - count)));
@ -33,7 +37,11 @@ public static class UInt16Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ushort RotateRight(this ushort value, int count)
{
return (ushort)((ushort)(value >> count) | (ushort)(value << (16 - count)));

View File

@ -1,5 +1,4 @@
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace X10D.Numerics;
@ -19,10 +18,14 @@ public static class UInt32Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static uint RotateLeft(this uint value, int count)
{
return BitOperations.RotateLeft(value, count);
return (value << count) | (value >> (32 - count));
}
/// <summary>
@ -34,9 +37,13 @@ public static class UInt32Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static uint RotateRight(this uint value, int count)
{
return BitOperations.RotateRight(value, count);
return (value >> count) | (value << (32 - count));
}
}

View File

@ -1,5 +1,4 @@
using System.Diagnostics.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace X10D.Numerics;
@ -19,10 +18,14 @@ public static class UInt64Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong RotateLeft(this ulong value, int count)
{
return BitOperations.RotateLeft(value, count);
return (value << count) | (value >> (64 - count));
}
/// <summary>
@ -34,9 +37,13 @@ public static class UInt64Extensions
/// </param>
/// <returns>The rotated value.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static ulong RotateRight(this ulong value, int count)
{
return BitOperations.RotateRight(value, count);
return (value >> count) | (value << (32 - count));
}
}

View File

@ -21,11 +21,22 @@ public static class MemberInfoExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool HasCustomAttribute<T>(this MemberInfo member)
where T : Attribute
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(member);
#else
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
#endif
return member.HasCustomAttribute(typeof(T));
}
@ -41,11 +52,29 @@ public static class MemberInfoExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="member" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool HasCustomAttribute(this MemberInfo member, Type attribute)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(member);
#else
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(attribute);
#else
if (attribute is null)
{
throw new ArgumentNullException(nameof(attribute));
}
#endif
if (!attribute.Inherits<Attribute>())
{
@ -75,8 +104,22 @@ public static class MemberInfoExtensions
Func<TAttribute, TReturn> selector)
where TAttribute : Attribute
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(member);
#else
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(selector);
#else
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
#endif
return member.SelectFromCustomAttribute(selector, default);
}
@ -101,8 +144,22 @@ public static class MemberInfoExtensions
Func<TAttribute, TReturn> selector, TReturn? defaultValue)
where TAttribute : Attribute
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(member);
#else
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(selector);
#else
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
#endif
return member.GetCustomAttribute<TAttribute>() is { } attribute
? selector(attribute)

View File

@ -17,10 +17,21 @@ public static class TypeExtensions
/// <returns><see langword="true" /> if the current exists on the type; otherwise, <see langword="false" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool Implements<T>(this Type value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.Implements(typeof(T));
}
@ -37,11 +48,29 @@ public static class TypeExtensions
/// <para><paramref name="interfaceType" /> is <see langword="null" />.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool Implements(this Type value, Type interfaceType)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(interfaceType);
#else
if (interfaceType is null)
{
throw new ArgumentNullException(nameof(interfaceType));
}
#endif
if (!interfaceType.IsInterface)
{
@ -66,11 +95,22 @@ public static class TypeExtensions
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException"><paramref name="value" /> is not a class.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool Inherits<T>(this Type value)
where T : class
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return value.Inherits(typeof(T));
}
@ -95,11 +135,29 @@ public static class TypeExtensions
/// <para><paramref name="type" /> is not a class.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool Inherits(this Type value, Type type)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(type);
#else
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
#endif
if (!value.IsClass)
{

View File

@ -17,7 +17,11 @@ public static class CharExtensions
/// A <see cref="string" /> composed of <paramref name="value" /> repeated <paramref name="count" /> times.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Repeat(this char value, int count)
{
return count switch

View File

@ -1,4 +1,5 @@
using System.Diagnostics.Contracts;
#if NET5_0_OR_GREATER
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Text.Json;
@ -23,3 +24,4 @@ public static class Extensions
return JsonSerializer.Serialize(value, options);
}
}
#endif

View File

@ -1,4 +1,5 @@
using System.Diagnostics.Contracts;
#if NETCOREAPP3_0_OR_GREATER
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Text;
@ -44,3 +45,4 @@ public static class RuneExtensions
return Encoding.UTF8.GetString(buffer);
}
}
#endif

View File

@ -2,7 +2,9 @@
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
using System.Text;
#if NET5_0_OR_GREATER
using System.Text.Json;
#endif
using X10D.Collections;
using X10D.Core;
using X10D.IO;
@ -23,7 +25,11 @@ public static class StringExtensions
/// <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
[return: NotNullIfNotNull("value")]
public static string? AsNullIfEmpty(this string? value)
{
@ -40,7 +46,11 @@ public static class StringExtensions
/// whitespace; otherwise, <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
[return: NotNullIfNotNull("value")]
public static string? AsNullIfWhiteSpace(this string? value)
{
@ -54,10 +64,21 @@ public static class StringExtensions
/// <returns>The plain text string representation of <paramref name="value" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Base64Decode(this string value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return Convert.FromBase64String(value).ToString(Encoding.ASCII);
}
@ -69,10 +90,21 @@ public static class StringExtensions
/// <returns>The string representation, in base 64, of <paramref name="value" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Base64Encode(this string value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
return Convert.ToBase64String(value.GetBytes(Encoding.ASCII));
}
@ -95,12 +127,37 @@ public static class StringExtensions
/// <paramref name="destinationEncoding" /> is <see langword="null" />.
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string ChangeEncoding(this string value, Encoding sourceEncoding, Encoding destinationEncoding)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(sourceEncoding);
#else
if (sourceEncoding is null)
{
throw new ArgumentNullException(nameof(sourceEncoding));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(destinationEncoding);
#else
if (destinationEncoding is null)
{
throw new ArgumentNullException(nameof(destinationEncoding));
}
#endif
return value.GetBytes(sourceEncoding).ToString(destinationEncoding);
}
@ -116,7 +173,11 @@ public static class StringExtensions
/// (http://geekswithblogs.net/sdorman/Default.aspx).
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static T EnumParse<T>(this string value)
where T : struct, Enum
{
@ -135,11 +196,22 @@ public static class StringExtensions
/// (http://geekswithblogs.net/sdorman/Default.aspx).
/// </remarks>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static T EnumParse<T>(this string value, bool ignoreCase)
where T : struct, Enum
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
value = value.Trim();
@ -151,6 +223,7 @@ public static class StringExtensions
return Enum.Parse<T>(value, ignoreCase);
}
#if NET5_0_OR_GREATER
/// <summary>
/// Returns an object from the specified JSON string.
/// </summary>
@ -161,11 +234,11 @@ public static class StringExtensions
/// An object constructed from the JSON string, or <see langword="null" /> if deserialization could not be performed.
/// </returns>
[Pure]
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public static T? FromJson<T>(this string value, JsonSerializerOptions? options = null)
{
return JsonSerializer.Deserialize<T>(value, options);
}
#endif
/// <summary>
/// Gets a <see cref="byte" />[] representing the value the <see cref="string" /> with
@ -174,7 +247,11 @@ public static class StringExtensions
/// <param name="value">The string to convert.</param>
/// <returns>Returns a <see cref="byte" />[].</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte[] GetBytes(this string value)
{
return value.GetBytes(Encoding.UTF8);
@ -191,11 +268,29 @@ public static class StringExtensions
/// <see langword="null" />.
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static byte[] GetBytes(this string value, Encoding encoding)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(encoding);
#else
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
#endif
return encoding.GetBytes(value);
}
@ -208,13 +303,25 @@ public static class StringExtensions
/// <see langword="true" /> if all alpha characters in this string are lowercase; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLower(this string value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
for (var index = 0; index < value.Length; index++)
{
#if NETCOREAPP3_0_OR_GREATER
var rune = new Rune(value[index]);
if (!Rune.IsLetter(rune))
@ -226,6 +333,19 @@ public static class StringExtensions
{
return false;
}
#else
char current = value[index];
if (!char.IsLetter(current))
{
continue;
}
if (!char.IsLower(current))
{
return false;
}
#endif
}
return true;
@ -242,10 +362,21 @@ public static class StringExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsPalindrome(this string value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
if (string.IsNullOrWhiteSpace(value))
{
@ -255,6 +386,7 @@ public static class StringExtensions
for (int index = 0, endIndex = value.Length - 1; index < value.Length; index++, endIndex--)
{
#if NETCOREAPP3_0_OR_GREATER
Rune startRune = new Rune(value[index]);
Rune endRune = new Rune(value[endIndex]);
@ -274,6 +406,27 @@ public static class StringExtensions
{
return false;
}
#else
char startChar = value[index];
char endChar = value[endIndex];
if (!char.IsLetter(startChar) && !char.IsNumber(startChar))
{
endIndex++;
continue;
}
if (!char.IsLetter(endChar) && !char.IsNumber(endChar))
{
index--;
continue;
}
if (char.ToUpperInvariant(startChar) != char.ToUpperInvariant(endChar))
{
return false;
}
#endif
}
return true;
@ -287,13 +440,25 @@ public static class StringExtensions
/// <see langword="true" /> if all alpha characters in this string are uppercase; otherwise, <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsUpper(this string value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
for (var index = 0; index < value.Length; index++)
{
#if NETCOREAPP3_0_OR_GREATER
var rune = new Rune(value[index]);
if (!Rune.IsLetter(rune))
@ -305,6 +470,19 @@ public static class StringExtensions
{
return false;
}
#else
char current = value[index];
if (!char.IsLetter(current))
{
continue;
}
if (!char.IsUpper(current))
{
return false;
}
#endif
}
return true;
@ -319,10 +497,21 @@ public static class StringExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Repeat(this string value, int count)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
switch (count)
{
@ -357,10 +546,21 @@ public static class StringExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is less than 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Randomize(this string source, int length, Random? random = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(source);
#else
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
#endif
if (length < 0)
{
@ -372,7 +572,7 @@ public static class StringExtensions
return string.Empty;
}
random ??= Random.Shared;
random ??= RandomExtensions.GetShared();
char[] array = source.ToCharArray();
var builder = new StringBuilder(length);
@ -392,10 +592,21 @@ public static class StringExtensions
/// <param name="value">The string to reverse.</param>
/// <returns>A <see cref="string" /> whose characters are that of <paramref name="value" /> in reverse order.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Reverse(this string value)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
if (value.Length < 2)
{
@ -417,18 +628,29 @@ public static class StringExtensions
/// </summary>
/// <param name="value">The string to shuffle.</param>
/// <param name="random">
/// The <see cref="System.Random" /> instance to use for the shuffling. If <see langword="null" /> is specified,
/// <see cref="System.Random.Shared" /> is used.
/// The <see cref="System.Random" /> instance to use for the shuffling. If <see langword="null" /> is specified, a shared
/// instance is used.
/// </param>
/// <returns>A new <see cref="string" /> containing the characters in <paramref name="value" />, rearranged.</returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static string Shuffled(this string value, Random? random = null)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
random ??= Random.Shared;
random ??= RandomExtensions.GetShared();
char[] array = value.ToCharArray();
array.Shuffle(random);
@ -446,10 +668,21 @@ public static class StringExtensions
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="value" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static IEnumerable<string> Split(this string value, int chunkSize)
{
#if NET6_0_OR_GREATER
ArgumentNullException.ThrowIfNull(value);
#else
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
#endif
if (chunkSize == 0)
{
@ -473,7 +706,11 @@ public static class StringExtensions
/// <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
[return: NotNullIfNotNull("alternative")]
public static string? WithEmptyAlternative(this string? value, string? alternative)
{
@ -491,7 +728,11 @@ public static class StringExtensions
/// whitespace; otherwise, <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
[return: NotNullIfNotNull("alternative")]
public static string? WithWhiteSpaceAlternative(this string? value, string? alternative)
{

View File

@ -17,7 +17,11 @@ public static class ByteExtensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this byte value)
{
if (value == 0)
@ -43,7 +47,11 @@ public static class ByteExtensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this byte value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -64,7 +72,11 @@ public static class ByteExtensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this byte value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -76,7 +88,11 @@ public static class ByteExtensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this byte value)
{
return TimeSpan.FromTicks(value);
@ -90,7 +106,11 @@ public static class ByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this byte value)
{
return TimeSpan.FromMilliseconds(value);
@ -104,7 +124,11 @@ public static class ByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this byte value)
{
return TimeSpan.FromSeconds(value);
@ -118,7 +142,11 @@ public static class ByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this byte value)
{
return TimeSpan.FromMinutes(value);
@ -132,7 +160,11 @@ public static class ByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this byte value)
{
return TimeSpan.FromHours(value);
@ -144,7 +176,11 @@ public static class ByteExtensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this byte value)
{
return TimeSpan.FromDays(value);
@ -158,7 +194,11 @@ public static class ByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this byte value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -10,7 +10,11 @@ public static class DateTimeExtensions
{
/// <inheritdoc cref="DateTimeOffsetExtensions.Age(DateTimeOffset)" />
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Age(this DateTime value)
{
return value.Age(DateTime.Today);
@ -18,7 +22,11 @@ public static class DateTimeExtensions
/// <inheritdoc cref="DateTimeOffsetExtensions.Age(DateTimeOffset, DateTimeOffset)" />
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Age(this DateTime value, DateTime asOf)
{
return ((DateTimeOffset)value).Age(asOf);
@ -27,7 +35,11 @@ public static class DateTimeExtensions
/// <inheritdoc cref="DateTimeOffsetExtensions.First(DateTimeOffset, DayOfWeek)" />
/// <returns>A <see cref="DateTime" /> representing the first occurence of <paramref name="dayOfWeek" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime First(this DateTime value, DayOfWeek dayOfWeek)
{
return ((DateTimeOffset)value).First(dayOfWeek).DateTime;
@ -36,7 +48,11 @@ public static class DateTimeExtensions
/// <inheritdoc cref="DateTimeOffsetExtensions.FirstDayOfMonth(DateTimeOffset)" />
/// <returns>A <see cref="DateTime" /> representing the first day of the current month.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime FirstDayOfMonth(this DateTime value)
{
return ((DateTimeOffset)value).FirstDayOfMonth().DateTime;
@ -51,7 +67,11 @@ public static class DateTimeExtensions
/// <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this DateTime value)
{
return DateTime.IsLeapYear(value.Year);
@ -60,7 +80,11 @@ public static class DateTimeExtensions
/// <inheritdoc cref="DateTimeOffsetExtensions.Last(DateTimeOffset, DayOfWeek)" />
/// <returns>A <see cref="DateTimeOffset" /> representing the final occurence of <paramref name="dayOfWeek" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime Last(this DateTime value, DayOfWeek dayOfWeek)
{
return ((DateTimeOffset)value).Last(dayOfWeek).DateTime;
@ -69,7 +93,11 @@ public static class DateTimeExtensions
/// <inheritdoc cref="DateTimeOffsetExtensions.LastDayOfMonth(DateTimeOffset)" />
/// <returns>A <see cref="DateTimeOffset" /> representing the last day of the current month.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime LastDayOfMonth(this DateTime value)
{
return ((DateTimeOffset)value).LastDayOfMonth().DateTime;
@ -78,7 +106,11 @@ public static class DateTimeExtensions
/// <inheritdoc cref="DateTimeOffsetExtensions.Next(DateTimeOffset, DayOfWeek)" />
/// <returns>A <see cref="DateTimeOffset" /> representing the next occurence of <paramref name="dayOfWeek" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime Next(this DateTime value, DayOfWeek dayOfWeek)
{
return ((DateTimeOffset)value).Next(dayOfWeek).DateTime;
@ -90,7 +122,11 @@ public static class DateTimeExtensions
/// <param name="value">The current date.</param>
/// <returns>The number of milliseconds that have elapsed since 1970-01-01T00:00:00.000Z.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long ToUnixTimeMilliseconds(this DateTime value)
{
return ((DateTimeOffset)value).ToUnixTimeMilliseconds();
@ -102,7 +138,11 @@ public static class DateTimeExtensions
/// <param name="value">The current date.</param>
/// <returns>The number of seconds that have elapsed since 1970-01-01T00:00:00.000Z.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static long ToUnixTimeSeconds(this DateTime value)
{
return ((DateTimeOffset)value).ToUnixTimeSeconds();

View File

@ -14,7 +14,11 @@ public static class DateTimeOffsetExtensions
/// <param name="value">The date from which to calculate.</param>
/// <returns>The rounded-down integer number of years since <paramref name="value" /> as of today.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Age(this DateTimeOffset value)
{
return value.Age(DateTime.Today);
@ -30,7 +34,11 @@ public static class DateTimeOffsetExtensions
/// <paramref name="asOf" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static int Age(this DateTimeOffset value, DateTimeOffset asOf)
{
return (int)(((asOf.Date - TimeSpan.FromDays(1) - value.Date).TotalDays + 1) / 365.2425);
@ -43,7 +51,11 @@ public static class DateTimeOffsetExtensions
/// <param name="dayOfWeek">The day of the week.</param>
/// <returns>A <see cref="DateTimeOffset" /> representing the first occurence of <paramref name="dayOfWeek" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset First(this DateTimeOffset value, DayOfWeek dayOfWeek)
{
var first = value.FirstDayOfMonth();
@ -62,7 +74,11 @@ public static class DateTimeOffsetExtensions
/// <param name="value">The current date.</param>
/// <returns>A <see cref="DateTimeOffset" /> representing the first day of the current month.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FirstDayOfMonth(this DateTimeOffset value)
{
return value.AddDays(1 - value.Day);
@ -77,7 +93,11 @@ public static class DateTimeOffsetExtensions
/// <see langword="false" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this DateTimeOffset value)
{
return DateTime.IsLeapYear(value.Year);
@ -90,7 +110,11 @@ public static class DateTimeOffsetExtensions
/// <param name="dayOfWeek">The day of the week.</param>
/// <returns>A <see cref="DateTimeOffset" /> representing the final occurence of <paramref name="dayOfWeek" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset Last(this DateTimeOffset value, DayOfWeek dayOfWeek)
{
var last = value.LastDayOfMonth();
@ -108,7 +132,11 @@ public static class DateTimeOffsetExtensions
/// <param name="value">The current date.</param>
/// <returns>A <see cref="DateTimeOffset" /> representing the last day of the current month.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset LastDayOfMonth(this DateTimeOffset value)
{
int daysInMonth = DateTime.DaysInMonth(value.Year, value.Month);
@ -122,7 +150,11 @@ public static class DateTimeOffsetExtensions
/// <param name="dayOfWeek">The day of the week.</param>
/// <returns>A <see cref="DateTimeOffset" /> representing the next occurence of <paramref name="dayOfWeek" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset Next(this DateTimeOffset value, DayOfWeek dayOfWeek)
{
int offsetDays = dayOfWeek - value.DayOfWeek;

View File

@ -16,7 +16,11 @@ public static class DecimalExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this decimal value)
{
return TimeSpan.FromMilliseconds((double)value);
@ -30,7 +34,11 @@ public static class DecimalExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this decimal value)
{
return TimeSpan.FromSeconds((double)value);
@ -44,7 +52,11 @@ public static class DecimalExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this decimal value)
{
return TimeSpan.FromMinutes((double)value);
@ -58,7 +70,11 @@ public static class DecimalExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this decimal value)
{
return TimeSpan.FromHours((double)value);
@ -70,7 +86,11 @@ public static class DecimalExtensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this decimal value)
{
return TimeSpan.FromDays((double)value);
@ -84,7 +104,11 @@ public static class DecimalExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this decimal value)
{
return TimeSpan.FromDays((double)value * 7);

View File

@ -16,7 +16,11 @@ public static class DoubleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this double value)
{
return TimeSpan.FromMilliseconds(value);
@ -30,7 +34,11 @@ public static class DoubleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this double value)
{
return TimeSpan.FromSeconds(value);
@ -44,7 +52,11 @@ public static class DoubleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this double value)
{
return TimeSpan.FromMinutes(value);
@ -58,7 +70,11 @@ public static class DoubleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this double value)
{
return TimeSpan.FromHours(value);
@ -70,7 +86,11 @@ public static class DoubleExtensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this double value)
{
return TimeSpan.FromDays(value);
@ -84,7 +104,11 @@ public static class DoubleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this double value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -1,4 +1,5 @@
using System.Diagnostics.Contracts;
#if NET5_0_OR_GREATER
using System.Diagnostics.Contracts;
using System.Runtime.CompilerServices;
namespace X10D.Time;
@ -90,3 +91,4 @@ public static class HalfExtensions
return TimeSpan.FromDays((float)value * 7);
}
}
#endif

View File

@ -18,7 +18,11 @@ public static class Int16Extensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this short value)
{
if (value == 0)
@ -49,7 +53,11 @@ public static class Int16Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this short value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -70,7 +78,11 @@ public static class Int16Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this short value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -82,7 +94,11 @@ public static class Int16Extensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this short value)
{
return TimeSpan.FromTicks(value);
@ -96,7 +112,11 @@ public static class Int16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this short value)
{
return TimeSpan.FromMilliseconds(value);
@ -110,7 +130,11 @@ public static class Int16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this short value)
{
return TimeSpan.FromSeconds(value);
@ -124,7 +148,11 @@ public static class Int16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this short value)
{
return TimeSpan.FromMinutes(value);
@ -138,7 +166,11 @@ public static class Int16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this short value)
{
return TimeSpan.FromHours(value);
@ -150,7 +182,11 @@ public static class Int16Extensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this short value)
{
return TimeSpan.FromDays(value);
@ -164,7 +200,11 @@ public static class Int16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this short value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -18,7 +18,11 @@ public static class Int32Extensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this int value)
{
if (value == 0)
@ -49,7 +53,11 @@ public static class Int32Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this int value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -70,7 +78,11 @@ public static class Int32Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this int value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -82,7 +94,11 @@ public static class Int32Extensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this int value)
{
return TimeSpan.FromTicks(value);
@ -96,7 +112,11 @@ public static class Int32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this int value)
{
return TimeSpan.FromMilliseconds(value);
@ -110,7 +130,11 @@ public static class Int32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this int value)
{
return TimeSpan.FromSeconds(value);
@ -124,7 +148,11 @@ public static class Int32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this int value)
{
return TimeSpan.FromMinutes(value);
@ -138,7 +166,11 @@ public static class Int32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this int value)
{
return TimeSpan.FromHours(value);
@ -150,7 +182,11 @@ public static class Int32Extensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this int value)
{
return TimeSpan.FromDays(value);
@ -164,7 +200,11 @@ public static class Int32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this int value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -18,7 +18,11 @@ public static class Int64Extensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this long value)
{
if (value == 0)
@ -49,7 +53,11 @@ public static class Int64Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this long value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -70,7 +78,11 @@ public static class Int64Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this long value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -82,7 +94,11 @@ public static class Int64Extensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this long value)
{
return TimeSpan.FromTicks(value);
@ -96,7 +112,11 @@ public static class Int64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this long value)
{
return TimeSpan.FromMilliseconds(value);
@ -110,7 +130,11 @@ public static class Int64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this long value)
{
return TimeSpan.FromSeconds(value);
@ -124,7 +148,11 @@ public static class Int64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this long value)
{
return TimeSpan.FromMinutes(value);
@ -138,7 +166,11 @@ public static class Int64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this long value)
{
return TimeSpan.FromHours(value);
@ -150,7 +182,11 @@ public static class Int64Extensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this long value)
{
return TimeSpan.FromDays(value);
@ -164,7 +200,11 @@ public static class Int64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this long value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -19,7 +19,11 @@ public static class SByteExtensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this sbyte value)
{
if (value == 0)
@ -50,7 +54,11 @@ public static class SByteExtensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this sbyte value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -71,7 +79,11 @@ public static class SByteExtensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this sbyte value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -83,7 +95,11 @@ public static class SByteExtensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this sbyte value)
{
return TimeSpan.FromTicks(value);
@ -97,7 +113,11 @@ public static class SByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this sbyte value)
{
return TimeSpan.FromMilliseconds(value);
@ -111,7 +131,11 @@ public static class SByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this sbyte value)
{
return TimeSpan.FromSeconds(value);
@ -125,7 +149,11 @@ public static class SByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this sbyte value)
{
return TimeSpan.FromMinutes(value);
@ -139,7 +167,11 @@ public static class SByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this sbyte value)
{
return TimeSpan.FromHours(value);
@ -151,7 +183,11 @@ public static class SByteExtensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this sbyte value)
{
return TimeSpan.FromDays(value);
@ -165,7 +201,11 @@ public static class SByteExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this sbyte value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -16,7 +16,11 @@ public static class SingleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this float value)
{
return TimeSpan.FromMilliseconds(value);
@ -30,7 +34,11 @@ public static class SingleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this float value)
{
return TimeSpan.FromSeconds(value);
@ -44,7 +52,11 @@ public static class SingleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this float value)
{
return TimeSpan.FromMinutes(value);
@ -58,7 +70,11 @@ public static class SingleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this float value)
{
return TimeSpan.FromHours(value);
@ -70,7 +86,11 @@ public static class SingleExtensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this float value)
{
return TimeSpan.FromDays(value);
@ -84,7 +104,11 @@ public static class SingleExtensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this float value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -57,7 +57,11 @@ public static class StringExtensions
/// <returns>A new instance of <see cref="TimeSpan" />.</returns>
/// <exception cref="ArgumentNullException"><paramref name="input" /> is <see langword="null" />.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan ToTimeSpan(this string input)
{
if (input is null)

View File

@ -16,7 +16,11 @@ public static class TimeSpanExtensions
/// A <see cref="DateTime" /> that is a duration of <paramref name="value" /> in the past relative to the current time.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime Ago(this TimeSpan value)
{
return DateTime.Now.Subtract(value);
@ -30,7 +34,11 @@ public static class TimeSpanExtensions
/// A <see cref="DateTime" /> that is a duration of <paramref name="value" /> in the future relative to the current time.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTime FromNow(this TimeSpan value)
{
return DateTime.Now.Add(value);

View File

@ -24,7 +24,11 @@ public static class UInt16Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this ushort value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -45,7 +49,11 @@ public static class UInt16Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this ushort value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -60,7 +68,11 @@ public static class UInt16Extensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this ushort value)
{
if (value == 0)
@ -77,7 +89,11 @@ public static class UInt16Extensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this ushort value)
{
return TimeSpan.FromTicks(value);
@ -91,7 +107,11 @@ public static class UInt16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this ushort value)
{
return TimeSpan.FromMilliseconds(value);
@ -105,7 +125,11 @@ public static class UInt16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this ushort value)
{
return TimeSpan.FromSeconds(value);
@ -119,7 +143,11 @@ public static class UInt16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this ushort value)
{
return TimeSpan.FromMinutes(value);
@ -133,7 +161,11 @@ public static class UInt16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this ushort value)
{
return TimeSpan.FromHours(value);
@ -145,7 +177,11 @@ public static class UInt16Extensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this ushort value)
{
return TimeSpan.FromDays(value);
@ -159,7 +195,11 @@ public static class UInt16Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this ushort value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -24,7 +24,11 @@ public static class UInt32Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this uint value)
{
return DateTimeOffset.FromUnixTimeMilliseconds(value);
@ -45,7 +49,11 @@ public static class UInt32Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this uint value)
{
return DateTimeOffset.FromUnixTimeSeconds(value);
@ -60,7 +68,11 @@ public static class UInt32Extensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this uint value)
{
if (value == 0)
@ -77,7 +89,11 @@ public static class UInt32Extensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this uint value)
{
return TimeSpan.FromTicks(value);
@ -91,7 +107,11 @@ public static class UInt32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this uint value)
{
return TimeSpan.FromMilliseconds(value);
@ -105,7 +125,11 @@ public static class UInt32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this uint value)
{
return TimeSpan.FromSeconds(value);
@ -119,7 +143,11 @@ public static class UInt32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this uint value)
{
return TimeSpan.FromMinutes(value);
@ -133,7 +161,11 @@ public static class UInt32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this uint value)
{
return TimeSpan.FromHours(value);
@ -145,7 +177,11 @@ public static class UInt32Extensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this uint value)
{
return TimeSpan.FromDays(value);
@ -159,7 +195,11 @@ public static class UInt32Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this uint value)
{
return TimeSpan.FromDays(value * 7);

View File

@ -24,7 +24,11 @@ public static class UInt64Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799,999.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeMilliseconds(this ulong value)
{
return DateTimeOffset.FromUnixTimeMilliseconds((long)value);
@ -45,7 +49,11 @@ public static class UInt64Extensions
/// <para><paramref name="value" /> is greater than 253,402,300,799.</para>
/// </exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static DateTimeOffset FromUnixTimeSeconds(this ulong value)
{
return DateTimeOffset.FromUnixTimeSeconds((long)value);
@ -60,7 +68,11 @@ public static class UInt64Extensions
/// </returns>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="value" /> is 0.</exception>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static bool IsLeapYear(this ulong value)
{
if (value == 0)
@ -77,7 +89,11 @@ public static class UInt64Extensions
/// <param name="value">The duration, in ticks.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.Ticks" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Ticks(this ulong value)
{
return TimeSpan.FromTicks((long)value);
@ -91,7 +107,11 @@ public static class UInt64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMilliseconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Milliseconds(this ulong value)
{
return TimeSpan.FromMilliseconds((long)value);
@ -105,7 +125,11 @@ public static class UInt64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalSeconds" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Seconds(this ulong value)
{
return TimeSpan.FromSeconds((long)value);
@ -119,7 +143,11 @@ public static class UInt64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalMinutes" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Minutes(this ulong value)
{
return TimeSpan.FromMinutes((long)value);
@ -133,7 +161,11 @@ public static class UInt64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalHours" /> will equal <paramref name="value" />.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Hours(this ulong value)
{
return TimeSpan.FromHours((long)value);
@ -145,7 +177,11 @@ public static class UInt64Extensions
/// <param name="value">The duration, in days.</param>
/// <returns>A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" />.</returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Days(this ulong value)
{
return TimeSpan.FromDays((long)value);
@ -159,7 +195,11 @@ public static class UInt64Extensions
/// A <see cref="TimeSpan" /> whose <see cref="TimeSpan.TotalDays" /> will equal <paramref name="value" /> × 7.
/// </returns>
[Pure]
#if NETSTANDARD2_1
[MethodImpl(MethodImplOptions.AggressiveInlining)]
#else
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
#endif
public static TimeSpan Weeks(this ulong value)
{
return TimeSpan.FromDays((long)value * 7);