first commit
This commit is contained in:
137
Core/Base32.cs
Normal file
137
Core/Base32.cs
Normal file
@@ -0,0 +1,137 @@
|
||||
namespace Just.Core;
|
||||
|
||||
public static class Base32
|
||||
{
|
||||
public const string Alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
|
||||
public const char Padding = '=';
|
||||
|
||||
[Pure]
|
||||
public static string Encode(ReadOnlySpan<byte> input)
|
||||
{
|
||||
if (input.Length == 0)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
int outLength = 8 * ((input.Length + 4) / 5);
|
||||
Span<char> output = stackalloc char[outLength];
|
||||
|
||||
_ = Encode(input, output);
|
||||
|
||||
return new string(output);
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static int Encode(ReadOnlySpan<byte> input, Span<char> output)
|
||||
{
|
||||
int i = 0;
|
||||
ReadOnlySpan<char> alphabet = Alphabet;
|
||||
for (int offset = 0; offset < input.Length;)
|
||||
{
|
||||
int numCharsToOutput = GetNextGroup(input, ref offset, out byte a, out byte b, out byte c, out byte d, out byte e, out byte f, out byte g, out byte h);
|
||||
|
||||
output[i++] = (numCharsToOutput > 0) ? alphabet[a] : Padding;
|
||||
output[i++] = (numCharsToOutput > 1) ? alphabet[b] : Padding;
|
||||
output[i++] = (numCharsToOutput > 2) ? alphabet[c] : Padding;
|
||||
output[i++] = (numCharsToOutput > 3) ? alphabet[d] : Padding;
|
||||
output[i++] = (numCharsToOutput > 4) ? alphabet[e] : Padding;
|
||||
output[i++] = (numCharsToOutput > 5) ? alphabet[f] : Padding;
|
||||
output[i++] = (numCharsToOutput > 6) ? alphabet[g] : Padding;
|
||||
output[i++] = (numCharsToOutput > 7) ? alphabet[h] : Padding;
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static byte[] Decode(ReadOnlySpan<char> input)
|
||||
{
|
||||
if (input.IsEmpty) return [];
|
||||
|
||||
Span<byte> output = stackalloc byte[5 * input.Length / 8];
|
||||
|
||||
var size = Decode(input, output);
|
||||
|
||||
return output[..size].ToArray();
|
||||
}
|
||||
|
||||
[Pure]
|
||||
public static int Decode(ReadOnlySpan<char> input, Span<byte> output)
|
||||
{
|
||||
input = input.TrimEnd(Padding);
|
||||
Span<char> inputspan = stackalloc char[input.Length];
|
||||
input.ToUpperInvariant(inputspan);
|
||||
|
||||
int bitIndex = 0;
|
||||
int inputIndex = 0;
|
||||
int outputBits = 0;
|
||||
int outputIndex = 0;
|
||||
int bitPos;
|
||||
int outBitPos;
|
||||
int bits;
|
||||
|
||||
ReadOnlySpan<char> alphabet = Alphabet;
|
||||
while (inputIndex < input.Length)
|
||||
{
|
||||
var byteIndex = alphabet.IndexOf(inputspan[inputIndex]);
|
||||
if (byteIndex < 0)
|
||||
{
|
||||
throw new FormatException("Provided string contains invalid characters.");
|
||||
}
|
||||
|
||||
bitPos = 5 - bitIndex;
|
||||
outBitPos = 8 - outputBits;
|
||||
bits = bitPos < outBitPos ? bitPos : outBitPos;
|
||||
|
||||
output[outputIndex] <<= bits;
|
||||
output[outputIndex] |= (byte)(byteIndex >> (bitPos - bits));
|
||||
|
||||
outputBits += bits;
|
||||
if (outputBits >= 8)
|
||||
{
|
||||
outputIndex++;
|
||||
outputBits = 0;
|
||||
}
|
||||
|
||||
bitIndex += bits;
|
||||
if (bitIndex >= 5)
|
||||
{
|
||||
inputIndex++;
|
||||
bitIndex = 0;
|
||||
}
|
||||
else if (inputIndex == input.Length -1) break;
|
||||
}
|
||||
|
||||
return outputIndex + (outputBits + 7) / 8;
|
||||
}
|
||||
|
||||
|
||||
// returns the number of bytes that were output
|
||||
[Pure]
|
||||
private static int GetNextGroup(ReadOnlySpan<byte> input, ref int offset, out byte a, out byte b, out byte c, out byte d, out byte e, out byte f, out byte g, out byte h)
|
||||
{
|
||||
var retVal = (input.Length - offset) switch
|
||||
{
|
||||
1 => 2,
|
||||
2 => 4,
|
||||
3 => 5,
|
||||
4 => 7,
|
||||
_ => 8,
|
||||
};
|
||||
uint b1 = (offset < input.Length) ? input[offset++] : 0U;
|
||||
uint b2 = (offset < input.Length) ? input[offset++] : 0U;
|
||||
uint b3 = (offset < input.Length) ? input[offset++] : 0U;
|
||||
uint b4 = (offset < input.Length) ? input[offset++] : 0U;
|
||||
uint b5 = (offset < input.Length) ? input[offset++] : 0U;
|
||||
|
||||
a = (byte)(b1 >> 3);
|
||||
b = (byte)(((b1 & 0x07) << 2) | (b2 >> 6));
|
||||
c = (byte)((b2 >> 1) & 0x1f);
|
||||
d = (byte)(((b2 & 0x01) << 4) | (b3 >> 4));
|
||||
e = (byte)(((b3 & 0x0f) << 1) | (b4 >> 7));
|
||||
f = (byte)((b4 >> 2) & 0x1f);
|
||||
g = (byte)(((b4 & 0x3) << 3) | (b5 >> 5));
|
||||
h = (byte)(b5 & 0x1f);
|
||||
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
118
Core/Base64Url.cs
Normal file
118
Core/Base64Url.cs
Normal file
@@ -0,0 +1,118 @@
|
||||
namespace Just.Core;
|
||||
|
||||
public static class Base64Url
|
||||
{
|
||||
[Pure] public static Guid DecodeGuid(ReadOnlySpan<char> value)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfNotEqual(value.Length, 22);
|
||||
|
||||
Span<byte> guidBytes = stackalloc byte[16];
|
||||
Span<char> chars = stackalloc char[24];
|
||||
value.CopyTo(chars);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
switch (value[i])
|
||||
{
|
||||
case '-': chars[i] = '+'; continue;
|
||||
case '_': chars[i] = '/'; continue;
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
chars[^2..].Fill('=');
|
||||
if (!Convert.TryFromBase64Chars(chars, guidBytes, out int _))
|
||||
throw new FormatException("Invalid Base64 string.");
|
||||
|
||||
return new Guid(guidBytes);
|
||||
}
|
||||
|
||||
[Pure] public static byte[] Decode(ReadOnlySpan<char> input)
|
||||
{
|
||||
if (input.IsEmpty) return [];
|
||||
|
||||
Span<byte> output = stackalloc byte[3 * ((input.Length + 3) / 4)];
|
||||
|
||||
var size = Decode(input, output);
|
||||
|
||||
return output[..size].ToArray();
|
||||
}
|
||||
|
||||
[Pure] public static int Decode(ReadOnlySpan<char> value, Span<byte> output)
|
||||
{
|
||||
var padding = (4 - (value.Length & 3)) & 3;
|
||||
var charlen = value.Length + padding;
|
||||
var outputBytes = charlen / 4;
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(output.Length, outputBytes);
|
||||
Span<char> chars = stackalloc char[charlen];
|
||||
|
||||
value.CopyTo(chars);
|
||||
for (int i = 0; i < value.Length; i++)
|
||||
{
|
||||
switch (value[i])
|
||||
{
|
||||
case '-': chars[i] = '+'; continue;
|
||||
case '_': chars[i] = '/'; continue;
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
chars[^padding..].Fill('=');
|
||||
|
||||
if (!Convert.TryFromBase64Chars(chars, output, out outputBytes))
|
||||
throw new FormatException("Invalid Base64 string.");
|
||||
|
||||
return outputBytes;
|
||||
}
|
||||
|
||||
[Pure] public static string Encode(in Guid id)
|
||||
{
|
||||
Span<byte> guidBytes = stackalloc byte[16];
|
||||
id.TryWriteBytes(guidBytes);
|
||||
Span<char> chars = stackalloc char[24];
|
||||
Convert.TryToBase64Chars(guidBytes, chars, out int _);
|
||||
|
||||
for (int i = 0; i < chars.Length - 2; i++)
|
||||
{
|
||||
switch (chars[i])
|
||||
{
|
||||
case '+': chars[i] = '-'; continue;
|
||||
case '/': chars[i] = '_'; continue;
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
|
||||
return new string(chars[..^2]);
|
||||
}
|
||||
|
||||
[Pure] public static string Encode(ReadOnlySpan<byte> input)
|
||||
{
|
||||
if (input.IsEmpty) return string.Empty;
|
||||
|
||||
int outLength = 8 * ((input.Length + 5) / 6);
|
||||
Span<char> output = stackalloc char[outLength];
|
||||
|
||||
int strlen = Encode(input, output);
|
||||
return new string(output[..strlen]);
|
||||
}
|
||||
|
||||
[Pure] public static int Encode(ReadOnlySpan<byte> input, Span<char> output)
|
||||
{
|
||||
var charlen = 8 * ((input.Length + 5) / 6);
|
||||
Span<char> chars = stackalloc char[charlen];
|
||||
Convert.TryToBase64Chars(input, chars, out int charsWritten);
|
||||
|
||||
int i;
|
||||
for (i = 0; i < charsWritten; i++)
|
||||
{
|
||||
switch (chars[i])
|
||||
{
|
||||
case '+': chars[i] = '-'; continue;
|
||||
case '/': chars[i] = '_'; continue;
|
||||
case '=': goto exitLoop;
|
||||
default: continue;
|
||||
}
|
||||
}
|
||||
exitLoop:
|
||||
chars[..i].CopyTo(output);
|
||||
|
||||
return i;
|
||||
}
|
||||
}
|
||||
87
Core/Collections/DataMap.cs
Normal file
87
Core/Collections/DataMap.cs
Normal file
@@ -0,0 +1,87 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Just.Core.Collections;
|
||||
|
||||
public class DataMap<T> : Map<T>, IDataMap<T>, ICloneable
|
||||
where T : IComparisonOperators<T, T, bool>, IEqualityOperators<T, T, bool>
|
||||
{
|
||||
internal MapPoint<T> MinCache = default;
|
||||
internal MapPoint<T> MaxCache = default;
|
||||
|
||||
internal DataMap(int width, int height, T[] values)
|
||||
: base(width, height, values)
|
||||
{
|
||||
}
|
||||
|
||||
public DataMap(ReadOnlySpan<T> values, int width, int height)
|
||||
: base(values, width, height)
|
||||
{
|
||||
}
|
||||
|
||||
public DataMap(T[] values, int width, int height)
|
||||
: base(values, width, height)
|
||||
{
|
||||
}
|
||||
|
||||
public DataMap(T[,] values)
|
||||
: base(values)
|
||||
{
|
||||
}
|
||||
|
||||
[Pure] public MapPoint<T> Min
|
||||
{
|
||||
get
|
||||
{
|
||||
if (MinCache.Map is null) FindMinMax();
|
||||
return MinCache;
|
||||
}
|
||||
}
|
||||
|
||||
[Pure] public MapPoint<T> Max
|
||||
{
|
||||
get
|
||||
{
|
||||
if (MaxCache.Map is null) FindMinMax();
|
||||
return MaxCache;
|
||||
}
|
||||
}
|
||||
|
||||
private void FindMinMax()
|
||||
{
|
||||
var data = _values.AsSpan();
|
||||
|
||||
T min = data[0];
|
||||
T max = data[0];
|
||||
int minId = 0;
|
||||
int maxId = 0;
|
||||
|
||||
for (int i = 0; i < data.Length; i++)
|
||||
{
|
||||
if (data[i] < min)
|
||||
{
|
||||
min = data[i];
|
||||
minId = i;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (data[i] > max)
|
||||
{
|
||||
max = data[i];
|
||||
maxId = i;
|
||||
}
|
||||
}
|
||||
|
||||
MinCache = new(min, minId % Width, minId / Width, this);
|
||||
MaxCache = new(max, maxId % Width, maxId / Width, this);
|
||||
}
|
||||
|
||||
[Pure] public DataMap<T> Clone()
|
||||
{
|
||||
var clone = new DataMap<T>(Width, Height, [.. _values]);
|
||||
clone.MinCache = new(clone._values[(MinCache.Y * Width) + MinCache.X], MinCache.X, MinCache.Y, clone);
|
||||
clone.MaxCache = new(clone._values[(MaxCache.Y * Width) + MaxCache.X], MaxCache.X, MaxCache.Y, clone);
|
||||
return clone;
|
||||
}
|
||||
|
||||
object ICloneable.Clone() => Clone();
|
||||
}
|
||||
10
Core/Collections/IDataMap.cs
Normal file
10
Core/Collections/IDataMap.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using System.Numerics;
|
||||
|
||||
namespace Just.Core.Collections;
|
||||
|
||||
public interface IDataMap<T> : IMap<T>, ICloneable
|
||||
where T : IComparisonOperators<T, T, bool>, IEqualityOperators<T, T, bool>
|
||||
{
|
||||
MapPoint<T> Min { get; }
|
||||
MapPoint<T> Max { get; }
|
||||
}
|
||||
12
Core/Collections/IMap.cs
Normal file
12
Core/Collections/IMap.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace Just.Core.Collections;
|
||||
|
||||
public interface IMap<T> : IReadOnlyCollection<MapPoint<T>>
|
||||
{
|
||||
int Width { get; }
|
||||
int Height { get; }
|
||||
ref readonly T this[int x, int y] { get; }
|
||||
ref readonly T this[double x, double y] { get; }
|
||||
MapPoint<T> Get(int x, int y);
|
||||
MapPoint<T> Get(double x, double y);
|
||||
T[,] ToArray();
|
||||
}
|
||||
172
Core/Collections/Map.cs
Normal file
172
Core/Collections/Map.cs
Normal file
@@ -0,0 +1,172 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Just.Core.Collections;
|
||||
|
||||
public class Map<T> : IMap<T>
|
||||
{
|
||||
internal readonly T[] _values;
|
||||
|
||||
internal Map(int width, int height, T[] values)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
_values = values;
|
||||
}
|
||||
public Map(ReadOnlySpan<T> values, int width, int height)
|
||||
{
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(width, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(height, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfNotEqual(width * height, values.Length);
|
||||
|
||||
Width = width;
|
||||
Height = height;
|
||||
_values = new T[values.Length];
|
||||
values.CopyTo(_values);
|
||||
}
|
||||
|
||||
public Map(T[] values, int width, int height)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(values);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(width, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(height, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfNotEqual(width * height, values.Length);
|
||||
|
||||
Width = width;
|
||||
Height = height;
|
||||
_values = new T[Width * Height];
|
||||
values.CopyTo(_values.AsSpan());
|
||||
}
|
||||
public Map(T[,] values)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(values);
|
||||
ArgumentOutOfRangeException.ThrowIfZero(values.Length);
|
||||
Width = values.GetLength(0);
|
||||
Height = values.GetLength(1);
|
||||
|
||||
_values = new T[Width * Height];
|
||||
|
||||
var data = _values.AsSpan();
|
||||
for (int y = 0; y < Height; y++)
|
||||
{
|
||||
int idy = y * Width;
|
||||
for (int x = 0; x < Width; x++)
|
||||
{
|
||||
int id = idy + x;
|
||||
data[id] = values[x, y];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Pure] public ReadOnlySpan<T> AsSpan() => _values;
|
||||
[Pure] public ReadOnlyMemory<T> AsMemory() => _values;
|
||||
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
public int Count => _values.Length;
|
||||
|
||||
[Pure] public ref readonly T this[int x, int y]
|
||||
{
|
||||
get
|
||||
{
|
||||
x = Math.Clamp(x, 0, Width);
|
||||
y = Math.Clamp(y, 0, Height);
|
||||
return ref _values[(y * Width) + x];
|
||||
}
|
||||
}
|
||||
[Pure] public ref readonly T this[double x, double y]
|
||||
{
|
||||
get
|
||||
{
|
||||
int xi = Math.Clamp((int)x, 0, Width - 1);
|
||||
int yi = Math.Clamp((int)y, 0, Height - 1);
|
||||
|
||||
return ref _values[(yi * Width) + xi];
|
||||
}
|
||||
}
|
||||
|
||||
[Pure] public MapPoint<T> Get(int x, int y)
|
||||
{
|
||||
x = Math.Clamp(x, 0, Width - 1);
|
||||
y = Math.Clamp(y, 0, Height - 1);
|
||||
return new(_values[(y * Width) + x], x, y, this);
|
||||
}
|
||||
[Pure] public MapPoint<T> Get(double x, double y)
|
||||
{
|
||||
int xi = Math.Clamp((int)x, 0, Width - 1);
|
||||
int yi = Math.Clamp((int)y, 0, Height - 1);
|
||||
|
||||
return new(_values[(yi * Width) + xi], xi, yi, this);
|
||||
}
|
||||
|
||||
[Pure] public T[,] ToArray()
|
||||
{
|
||||
T[,] arr = new T[Width, Height];
|
||||
|
||||
var data = _values.AsSpan();
|
||||
for (int y = 0; y < Height; y++)
|
||||
{
|
||||
int idy = y * Width;
|
||||
for (int x = 0; x < Width; x++)
|
||||
{
|
||||
int id = idy + x;
|
||||
arr[x, y] = data[id];
|
||||
}
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
public Enumerator GetEnumerator() => new(this);
|
||||
IEnumerator<MapPoint<T>> IEnumerable<MapPoint<T>>.GetEnumerator() => new Enumerator(this);
|
||||
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
|
||||
|
||||
public struct Enumerator : IEnumerator<MapPoint<T>>, IEnumerator
|
||||
{
|
||||
private readonly Map<T> _map;
|
||||
private int _index;
|
||||
private MapPoint<T> _current;
|
||||
|
||||
internal Enumerator(Map<T> map)
|
||||
{
|
||||
_map = map;
|
||||
_index = 0;
|
||||
_current = default;
|
||||
}
|
||||
|
||||
public readonly void Dispose() { }
|
||||
public bool MoveNext()
|
||||
{
|
||||
if ((uint)_index < (uint)_map.Count)
|
||||
{
|
||||
_current = new(
|
||||
_map._values[_index],
|
||||
_index % _map.Width,
|
||||
_index / _map.Width,
|
||||
_map);
|
||||
_index++;
|
||||
return true;
|
||||
}
|
||||
|
||||
_index = _map.Count + 1;
|
||||
_current = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public readonly MapPoint<T> Current => _current!;
|
||||
readonly object? IEnumerator.Current
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_index == 0 || _index == _map.Count + 1)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
_index = 0;
|
||||
_current = default;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
Core/Collections/MapPoint.cs
Normal file
9
Core/Collections/MapPoint.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Just.Core.Collections;
|
||||
|
||||
public readonly struct MapPoint<T>(T value, int x, int y, IMap<T> map)
|
||||
{
|
||||
public readonly T Value = value;
|
||||
public IMap<T> Map { get; } = map;
|
||||
public int X { get; } = x;
|
||||
public int Y { get; } = y;
|
||||
}
|
||||
34
Core/Core.csproj
Normal file
34
Core/Core.csproj
Normal file
@@ -0,0 +1,34 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AssemblyName>Just.Core</AssemblyName>
|
||||
<RootNamespace>Just.Core</RootNamespace>
|
||||
|
||||
<Description>Small .Net library with useful helper classes, functions and extensions.</Description>
|
||||
<PackageTags>extensions;helpers;helper-functions</PackageTags>
|
||||
<Authors>JustFixMe</Authors>
|
||||
<Copyright>Copyright (c) 2023 JustFixMe</Copyright>
|
||||
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||
<PackageReadmeFile>README.md</PackageReadmeFile>
|
||||
<RepositoryUrl>https://github.com/JustFixMe/Just.Core/</RepositoryUrl>
|
||||
|
||||
<EmitCompilerGeneratedFiles Condition="'$(Configuration)'=='Debug'">true</EmitCompilerGeneratedFiles>
|
||||
<ReleaseVersion Condition=" '$(ReleaseVersion)' == '' ">1.0.0</ReleaseVersion>
|
||||
<VersionSuffix Condition=" '$(VersionSuffix)' != '' ">$(VersionSuffix)</VersionSuffix>
|
||||
<VersionPrefix Condition=" '$(VersionSuffix)' != '' ">$(ReleaseVersion)</VersionPrefix>
|
||||
<Version Condition=" '$(VersionSuffix)' == '' ">$(ReleaseVersion)</Version>
|
||||
<AssemblyVersion>$(ReleaseVersion)</AssemblyVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="$(AssemblyName).Tests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="..\README.md" Pack="true" PackagePath=""/>
|
||||
<None Include="..\LICENSE" Pack="true" Visible="false" PackagePath=""/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
46
Core/Extensions/SystemIOStreamExtensions.cs
Normal file
46
Core/Extensions/SystemIOStreamExtensions.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
namespace Just.Core.Extensions;
|
||||
|
||||
public static class SystemIOStreamExtensions
|
||||
{
|
||||
public static void Populate(this Stream stream, byte[] buffer, int offset, int length)
|
||||
=> stream.Populate(buffer.AsSpan(offset, length));
|
||||
public static void Populate(this Stream stream, byte[] buffer)
|
||||
=> stream.Populate(buffer.AsSpan());
|
||||
public static void Populate(this Stream stream, Span<byte> buffer)
|
||||
{
|
||||
do
|
||||
{
|
||||
var readed = stream.Read(buffer);
|
||||
|
||||
if (readed == 0)
|
||||
{
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
|
||||
buffer = buffer[readed..];
|
||||
}
|
||||
while (buffer.Length > 0);
|
||||
}
|
||||
|
||||
public static async ValueTask PopulateAsync(this Stream stream, byte[] buffer, CancellationToken cancellationToken = default)
|
||||
=> await stream.PopulateAsync(buffer.AsMemory(), cancellationToken);
|
||||
public static async ValueTask PopulateAsync(this Stream stream, byte[] buffer, int offset, int length, CancellationToken cancellationToken = default)
|
||||
=> await stream.PopulateAsync(buffer.AsMemory(offset, length), cancellationToken);
|
||||
public static async ValueTask PopulateAsync(this Stream stream, Memory<byte> buffer, CancellationToken cancellationToken = default)
|
||||
{
|
||||
do
|
||||
{
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
var readed = await stream.ReadAsync(buffer, cancellationToken);
|
||||
|
||||
if (readed == 0)
|
||||
{
|
||||
throw new EndOfStreamException();
|
||||
}
|
||||
|
||||
buffer = buffer[readed..];
|
||||
}
|
||||
while (buffer.Length > 0);
|
||||
}
|
||||
}
|
||||
36
Core/Extensions/SystemStringExtensions.cs
Normal file
36
Core/Extensions/SystemStringExtensions.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Just.Core.Extensions;
|
||||
|
||||
public static class SystemStringExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Indicates whether the specified string is null or an empty string ("").
|
||||
/// </summary>
|
||||
/// <param name="str">The string to test.</param>
|
||||
/// <returns>true if the value parameter is null or an empty string (""); otherwise, false.</returns>
|
||||
public static bool IsNullOrEmpty([NotNullWhen(false)] this string? str) => string.IsNullOrEmpty(str);
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether a specified string is null, empty, or consists only of white-space
|
||||
/// characters.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to test.</param>
|
||||
/// <returns>true if the value parameter is null or <see cref="System.String.Empty"/>, or if value consists
|
||||
/// exclusively of white-space characters.</returns>
|
||||
public static bool IsNullOrWhiteSpace([NotNullWhen(false)] this string? str) => string.IsNullOrWhiteSpace(str);
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether the specified string contains any characters.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to test.</param>
|
||||
/// <returns>true if the value parameter contains any characters; otherwise, false.</returns>
|
||||
public static bool IsNotNullOrEmpty([NotNullWhen(true)] this string? str) => !string.IsNullOrEmpty(str);
|
||||
|
||||
/// <summary>
|
||||
/// Indicates whether a specified string contains non white-space characters.
|
||||
/// </summary>
|
||||
/// <param name="str">The string to test.</param>
|
||||
/// <returns>true if the value parameter contains non white-space characters; otherwise, false.</returns>
|
||||
public static bool IsNotNullOrWhiteSpace([NotNullWhen(true)] this string? str) => !string.IsNullOrWhiteSpace(str);
|
||||
|
||||
}
|
||||
3
Core/usings.cs
Normal file
3
Core/usings.cs
Normal file
@@ -0,0 +1,3 @@
|
||||
global using System.Diagnostics.CodeAnalysis;
|
||||
global using System.Diagnostics.Contracts;
|
||||
global using System.Runtime.CompilerServices;
|
||||
Reference in New Issue
Block a user