Explicitly validate arguments

Delegating validation to another class caused warnings due to the
compiler not being aware of the null check
This commit is contained in:
Oliver Booth 2021-03-07 18:15:58 +00:00
parent 9bcd1eabce
commit c4e890f2cb
3 changed files with 216 additions and 71 deletions

View File

@ -22,7 +22,11 @@ namespace X10D.ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static string AsString(this IReadOnlyList<byte> source)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToString(source.ToArray());
}
@ -49,7 +53,11 @@ namespace X10D.ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static double ToDouble(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToDouble(source.ToArray(), startIndex);
}
@ -73,7 +81,11 @@ namespace X10D.ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static short ToInt16(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToInt16(source.ToArray(), startIndex);
}
@ -97,7 +109,11 @@ namespace X10D.ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static int ToInt32(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToInt32(source.ToArray(), startIndex);
}
@ -121,7 +137,11 @@ namespace X10D.ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static long ToInt64(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToInt64(source.ToArray(), startIndex);
}
@ -148,7 +168,11 @@ namespace X10D.ListExtensions
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
public static float ToSingle(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToSingle(source.ToArray(), startIndex);
}
@ -165,8 +189,15 @@ namespace X10D.ListExtensions
/// </exception>
public static string ToString(this IReadOnlyList<byte> source, Encoding encoding)
{
Validate.IsNotNull(source, nameof(source));
Validate.IsNotNull(encoding, nameof(encoding));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (encoding is null)
{
throw new ArgumentNullException(nameof(encoding));
}
return encoding.GetString(source.ToArray());
}
@ -193,7 +224,11 @@ namespace X10D.ListExtensions
[CLSCompliant(false)]
public static ushort ToUInt16(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToUInt16(source.ToArray(), startIndex);
}
@ -219,7 +254,11 @@ namespace X10D.ListExtensions
[CLSCompliant(false)]
public static uint ToUInt32(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToUInt32(source.ToArray(), startIndex);
}
@ -245,7 +284,11 @@ namespace X10D.ListExtensions
[CLSCompliant(false)]
public static ulong ToUInt64(this IReadOnlyList<byte> source, int startIndex)
{
Validate.IsNotNull(source, nameof(source));
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
return BitConverter.ToUInt64(source.ToArray(), startIndex);
}
}

View File

@ -76,8 +76,15 @@ namespace X10D.StreamExtensions
/// <returns>A decimal value read from the stream.</returns>
public static decimal ReadDecimal(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
const int decimalSize = sizeof(decimal);
const int int32Size = sizeof(int);
@ -112,8 +119,15 @@ namespace X10D.StreamExtensions
/// <returns>A double-precision floating point value read from the stream.</returns>
public static double ReadDouble(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -148,8 +162,15 @@ namespace X10D.StreamExtensions
/// <returns>An two-byte unsigned integer read from the stream.</returns>
public static short ReadInt16(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -184,8 +205,15 @@ namespace X10D.StreamExtensions
/// <returns>An four-byte unsigned integer read from the stream.</returns>
public static int ReadInt32(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -220,8 +248,15 @@ namespace X10D.StreamExtensions
/// <returns>An eight-byte unsigned integer read from the stream.</returns>
public static long ReadInt64(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -256,8 +291,15 @@ namespace X10D.StreamExtensions
/// <returns>A single-precision floating point value read from the stream.</returns>
public static double ReadSingle(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -294,8 +336,15 @@ namespace X10D.StreamExtensions
[CLSCompliant(false)]
public static ushort ReadUInt16(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -332,8 +381,15 @@ namespace X10D.StreamExtensions
[CLSCompliant(false)]
public static uint ReadUInt32(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -370,8 +426,15 @@ namespace X10D.StreamExtensions
[CLSCompliant(false)]
public static ulong ReadUInt64(this Stream stream, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -408,8 +471,15 @@ namespace X10D.StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, short value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(short)];
@ -452,8 +522,15 @@ namespace X10D.StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, int value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(int)];
@ -496,8 +573,15 @@ namespace X10D.StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, long value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(long)];
@ -542,8 +626,15 @@ namespace X10D.StreamExtensions
[CLSCompliant(false)]
public static int Write(this Stream stream, ushort value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(ushort)];
@ -588,8 +679,15 @@ namespace X10D.StreamExtensions
[CLSCompliant(false)]
public static int Write(this Stream stream, uint value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(uint)];
@ -634,8 +732,15 @@ namespace X10D.StreamExtensions
[CLSCompliant(false)]
public static int Write(this Stream stream, ulong value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(ulong)];
@ -666,8 +771,15 @@ namespace X10D.StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, float value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(float)];
@ -698,8 +810,15 @@ namespace X10D.StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, double value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
#if NET5_0
Span<byte> buffer = stackalloc byte[sizeof(double)];
@ -730,8 +849,15 @@ namespace X10D.StreamExtensions
/// <returns>The number of bytes written to the stream.</returns>
public static int Write(this Stream stream, decimal value, Endianness endianness)
{
Validate.IsNotNull(stream, nameof(stream));
Validate.IsDefined(endianness, nameof(endianness));
if (stream is null)
{
throw new ArgumentNullException(nameof(stream));
}
if (!Enum.IsDefined(typeof(Endianness), endianness))
{
throw new ArgumentOutOfRangeException(nameof(endianness));
}
var bits = decimal.GetBits(value);
var preWritePosition = stream.Position;

View File

@ -1,24 +0,0 @@
using System;
namespace X10D
{
internal static class Validate
{
public static void IsDefined<T>(T value, string argumentName)
where T : Enum
{
if (!Enum.IsDefined(typeof(T), value))
{
throw new ArgumentOutOfRangeException(argumentName);
}
}
public static void IsNotNull<T>(T argument, string argumentName)
{
if (argument is null)
{
throw new ArgumentNullException(argumentName);
}
}
}
}