diff --git a/X10D/src/ListExtensions/ListOfByteExtensions.cs b/X10D/src/ListExtensions/ListOfByteExtensions.cs index 4f5f474..1fea2f8 100644 --- a/X10D/src/ListExtensions/ListOfByteExtensions.cs +++ b/X10D/src/ListExtensions/ListOfByteExtensions.cs @@ -22,7 +22,11 @@ namespace X10D.ListExtensions /// is . public static string AsString(this IReadOnlyList 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 /// is . public static double ToDouble(this IReadOnlyList 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 /// is . public static short ToInt16(this IReadOnlyList 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 /// is . public static int ToInt32(this IReadOnlyList 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 /// is . public static long ToInt64(this IReadOnlyList 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 /// is . public static float ToSingle(this IReadOnlyList 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 /// public static string ToString(this IReadOnlyList 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 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 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 source, int startIndex) { - Validate.IsNotNull(source, nameof(source)); + if (source is null) + { + throw new ArgumentNullException(nameof(source)); + } + return BitConverter.ToUInt64(source.ToArray(), startIndex); } } diff --git a/X10D/src/StreamExtensions/StreamExtensions.cs b/X10D/src/StreamExtensions/StreamExtensions.cs index 6c2a7c8..996ca1e 100644 --- a/X10D/src/StreamExtensions/StreamExtensions.cs +++ b/X10D/src/StreamExtensions/StreamExtensions.cs @@ -76,8 +76,15 @@ namespace X10D.StreamExtensions /// A decimal value read from the stream. 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 /// A double-precision floating point value read from the stream. 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 buffer = stackalloc byte[sizeof(int)]; @@ -148,8 +162,15 @@ namespace X10D.StreamExtensions /// An two-byte unsigned integer read from the stream. 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 buffer = stackalloc byte[sizeof(int)]; @@ -184,8 +205,15 @@ namespace X10D.StreamExtensions /// An four-byte unsigned integer read from the stream. 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 buffer = stackalloc byte[sizeof(int)]; @@ -220,8 +248,15 @@ namespace X10D.StreamExtensions /// An eight-byte unsigned integer read from the stream. 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 buffer = stackalloc byte[sizeof(int)]; @@ -256,8 +291,15 @@ namespace X10D.StreamExtensions /// A single-precision floating point value read from the stream. 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 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 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 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 buffer = stackalloc byte[sizeof(int)]; @@ -408,8 +471,15 @@ namespace X10D.StreamExtensions /// The number of bytes written to the stream. 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 buffer = stackalloc byte[sizeof(short)]; @@ -452,8 +522,15 @@ namespace X10D.StreamExtensions /// The number of bytes written to the stream. 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 buffer = stackalloc byte[sizeof(int)]; @@ -496,8 +573,15 @@ namespace X10D.StreamExtensions /// The number of bytes written to the stream. 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 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 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 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 buffer = stackalloc byte[sizeof(ulong)]; @@ -666,8 +771,15 @@ namespace X10D.StreamExtensions /// The number of bytes written to the stream. 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 buffer = stackalloc byte[sizeof(float)]; @@ -698,8 +810,15 @@ namespace X10D.StreamExtensions /// The number of bytes written to the stream. 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 buffer = stackalloc byte[sizeof(double)]; @@ -730,8 +849,15 @@ namespace X10D.StreamExtensions /// The number of bytes written to the stream. 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; diff --git a/X10D/src/Validate.cs b/X10D/src/Validate.cs deleted file mode 100644 index b46e88a..0000000 --- a/X10D/src/Validate.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; - -namespace X10D -{ - internal static class Validate - { - public static void IsDefined(T value, string argumentName) - where T : Enum - { - if (!Enum.IsDefined(typeof(T), value)) - { - throw new ArgumentOutOfRangeException(argumentName); - } - } - - public static void IsNotNull(T argument, string argumentName) - { - if (argument is null) - { - throw new ArgumentNullException(argumentName); - } - } - } -}