1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-23 00:38:47 +00:00

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

View File

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