Added first script, bugfixes
This commit is contained in:
52
PkmnLib.Static/Utils/NumericHelpers.cs
Normal file
52
PkmnLib.Static/Utils/NumericHelpers.cs
Normal file
@@ -0,0 +1,52 @@
|
||||
namespace PkmnLib.Static.Utils;
|
||||
|
||||
/// <summary>
|
||||
/// Helper methods for numeric operations.
|
||||
/// </summary>
|
||||
public static class NumericHelpers
|
||||
{
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
|
||||
/// </summary>
|
||||
public static byte MultiplyOrMax(this byte value, byte multiplier)
|
||||
{
|
||||
var result = value * multiplier;
|
||||
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="byte.MaxValue"/>.
|
||||
/// </summary>
|
||||
public static byte MultiplyOrMax(this byte value, float multiplier)
|
||||
{
|
||||
var result = value * multiplier;
|
||||
return result > byte.MaxValue ? byte.MaxValue : (byte)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="sbyte.MaxValue"/>.
|
||||
/// </summary>
|
||||
public static sbyte MultiplyOrMax(this sbyte value, sbyte multiplier)
|
||||
{
|
||||
var result = value * multiplier;
|
||||
return result > sbyte.MaxValue ? sbyte.MaxValue : (sbyte)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="ushort.MaxValue"/>.
|
||||
/// </summary>
|
||||
public static ushort MultiplyOrMax(this ushort value, ushort multiplier)
|
||||
{
|
||||
var result = value * multiplier;
|
||||
return result > ushort.MaxValue ? ushort.MaxValue : (ushort)result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Multiplies two values. If this overflows, returns <see cref="short.MaxValue"/>.
|
||||
/// </summary>
|
||||
public static short MultiplyOrMax(this short value, short multiplier)
|
||||
{
|
||||
var result = value * multiplier;
|
||||
return result > short.MaxValue ? short.MaxValue : (short)result;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,8 @@ public readonly record struct StringKey
|
||||
/// <inheritdoc cref="StringKey"/>
|
||||
public StringKey(string key)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(key))
|
||||
throw new ArgumentException("Key cannot be null or whitespace.", nameof(key));
|
||||
_key = key;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user