diff --git a/X10D/src/ByteExtensions.cs b/X10D/src/ByteExtensions.cs index 3412cfc..f1d81a4 100644 --- a/X10D/src/ByteExtensions.cs +++ b/X10D/src/ByteExtensions.cs @@ -97,10 +97,17 @@ /// The bytes to convert. /// The encoding to use. /// Returns a . + /// is . public static string GetString(this IEnumerable bytes, Encoding encoding) { - IEnumerable enumerable = bytes as byte[] ?? bytes.ToArray(); - return encoding.GetString(enumerable.ToArray(), 0, enumerable.Count()); + if (encoding is null) + { + throw new ArgumentNullException(nameof(encoding)); + } + + // ReSharper disable once SuggestVarOrType_Elsewhere + var array = bytes.ToArray(); + return encoding.GetString(array, 0, array.Length); } } } diff --git a/X10D/src/CharExtensions.cs b/X10D/src/CharExtensions.cs index 9bfca63..a377e09 100644 --- a/X10D/src/CharExtensions.cs +++ b/X10D/src/CharExtensions.cs @@ -28,12 +28,23 @@ /// The length of the string to generate. /// The instance. /// Returns a containing characters. + /// is . public static string Random(this char[] chars, int length, Random random) { - StringBuilder builder = new StringBuilder(length); - for (int i = 0; i < length; i++) + if (chars is null) { - builder.Append(chars.ElementAt(random.Next(0, chars.Length))); + throw new ArgumentNullException(nameof(chars)); + } + + if (random is null) + { + throw new ArgumentNullException(nameof(random)); + } + + var builder = new StringBuilder(length); + for (var i = 0; i < length; i++) + { + builder.Append(chars[random.Next(0, chars.Length)]); } return builder.ToString(); diff --git a/X10D/src/RandomExtensions.cs b/X10D/src/RandomExtensions.cs index 2ae2147..99f8dcc 100644 --- a/X10D/src/RandomExtensions.cs +++ b/X10D/src/RandomExtensions.cs @@ -52,8 +52,20 @@ /// The instance. /// The collection from which to draw. /// Returns a random element of type from . + /// or is + /// . public static T OneOf(this Random random, IList source) { + if (random is null) + { + throw new ArgumentNullException(nameof(random)); + } + + if (source is null) + { + throw new ArgumentNullException(nameof(source)); + } + return source[random.Next(source.Count)]; } } diff --git a/X10D/src/StreamExtensions.cs b/X10D/src/StreamExtensions.cs index a6eec97..bdbb6c5 100644 --- a/X10D/src/StreamExtensions.cs +++ b/X10D/src/StreamExtensions.cs @@ -16,10 +16,17 @@ /// A derived type. /// The stream whose hash is to be computed. /// Returns a array representing the hash of the stream. - public static byte[] GetHash(this Stream stream) where T : HashAlgorithm + /// is . + public static byte[] GetHash(this Stream stream) + where T : HashAlgorithm { + if (stream is null) + { + throw new ArgumentNullException(nameof(stream)); + } + MethodInfo create = typeof(T).GetMethod("Create", Array.Empty()); - using T crypt = (T)create?.Invoke(null, null); + using var crypt = (T)create?.Invoke(null, null); return crypt?.ComputeHash(stream); } } diff --git a/X10D/src/StringExtensions.cs b/X10D/src/StringExtensions.cs index c329e3c..0b5b0b1 100644 --- a/X10D/src/StringExtensions.cs +++ b/X10D/src/StringExtensions.cs @@ -52,7 +52,7 @@ /// The value corresponding to the public static T EnumParse(this string value, bool ignoreCase) { - if (value == null) + if (value is null) { throw new ArgumentNullException(nameof(value)); } @@ -61,18 +61,17 @@ if (value.Length == 0) { - throw new ArgumentException("Must specify valid information for parsing in the string.", - nameof(value)); + throw new ArgumentException("Must specify valid information for parsing in the string.", nameof(value)); } Type t = typeof(T); if (!t.IsEnum) { - throw new ArgumentException("Type provided must be an Enum.", nameof(T)); + throw new ArgumentException("Type provided must be an Enum."); } - return (T) Enum.Parse(t, value, ignoreCase); + return (T)Enum.Parse(t, value, ignoreCase); } /// @@ -92,8 +91,20 @@ /// The string to convert. /// The encoding to use. /// Returns a []. + /// or or both are + /// . public static byte[] GetBytes(this string str, Encoding encoding) { + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + + if (encoding is null) + { + throw new ArgumentNullException(nameof(encoding)); + } + return encoding.GetBytes(str); } @@ -115,8 +126,14 @@ /// The length of the string to generate. /// The instance. /// Returns a containing characters. + /// is . public static string Random(this string str, int length, Random random) { + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + return str.ToCharArray().Random(length, random); } @@ -127,11 +144,17 @@ /// The repeat count. /// Returns a whose value is repeated /// times. + /// is . public static string Repeat(this string str, int count) { - StringBuilder builder = new StringBuilder(str.Length * count); + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } - for (int i = 0; i < count; i++) + var builder = new StringBuilder(str.Length * count); + + for (var i = 0; i < count; i++) { builder.Append(str); } @@ -155,8 +178,14 @@ /// The string to shuffle. /// The instance. /// Returns a containing the characters in , rearranged. + /// is . public static string Shuffle(this string str, Random random) { + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + return new string(str.ToCharArray().Shuffle(random).ToArray()); } @@ -167,9 +196,15 @@ /// The maximum length of each string in the returned result. /// Returns an containing instances which are no /// greater than in length. + /// is . public static IEnumerable Split(this string str, int chunkSize) { - for (int i = 0; i < str.Length; i += chunkSize) + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + + for (var i = 0; i < str.Length; i += chunkSize) { yield return str.Substring(i, Math.Min(chunkSize, str.Length - i)); } @@ -180,8 +215,14 @@ /// /// The string to convert. /// Returns a . + /// is . public static SecureString ToSecureString(this string str) { + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + if (string.IsNullOrWhiteSpace(str)) { return null; @@ -202,9 +243,15 @@ /// The to convert. /// Whether or not to use this extension method. /// Returns a . + /// is . public static string ToString(this SecureString str, bool extension) { - return extension ? (new NetworkCredential(string.Empty, str).Password) : str.ToString(); + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + + return extension ? new NetworkCredential(string.Empty, str).Password : str.ToString(); } /// @@ -213,8 +260,14 @@ /// /// The input string. /// Returns an instance of . + /// is . public static TimeSpan ToTimeSpan(this string str) { + if (str is null) + { + throw new ArgumentNullException(nameof(str)); + } + return TimeSpanParser.Parse(str); } }