mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-23 01:18:46 +00:00
🔨 Improve exception handling and rethrowing
This commit is contained in:
parent
373313f07e
commit
11bc2415fc
@ -97,10 +97,17 @@
|
||||
/// <param name="bytes">The bytes to convert.</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <returns>Returns a <see cref="string"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="encoding"/> is <see langword="null"/>.</exception>
|
||||
public static string GetString(this IEnumerable<byte> bytes, Encoding encoding)
|
||||
{
|
||||
IEnumerable<byte> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,12 +28,23 @@
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random"/> is <see langword="null"/>.</exception>
|
||||
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();
|
||||
|
@ -52,8 +52,20 @@
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <param name="source">The collection from which to draw.</param>
|
||||
/// <returns>Returns a random element of type <see cref="T"/> from <paramref name="source"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="random"/> or <paramref name="source"/> is
|
||||
/// <see langword="null"/>.</exception>
|
||||
public static T OneOf<T>(this Random random, IList<T> 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)];
|
||||
}
|
||||
}
|
||||
|
@ -16,10 +16,17 @@
|
||||
/// <typeparam name="T">A <see cref="HashAlgorithm"/> derived type.</typeparam>
|
||||
/// <param name="stream">The stream whose hash is to be computed.</param>
|
||||
/// <returns>Returns a <see cref="byte"/> array representing the hash of the stream.</returns>
|
||||
public static byte[] GetHash<T>(this Stream stream) where T : HashAlgorithm
|
||||
/// <exception cref="ArgumentNullException"><paramref name="stream"/> is <see langword="null"/>.</exception>
|
||||
public static byte[] GetHash<T>(this Stream stream)
|
||||
where T : HashAlgorithm
|
||||
{
|
||||
if (stream is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(stream));
|
||||
}
|
||||
|
||||
MethodInfo create = typeof(T).GetMethod("Create", Array.Empty<Type>());
|
||||
using T crypt = (T)create?.Invoke(null, null);
|
||||
using var crypt = (T)create?.Invoke(null, null);
|
||||
return crypt?.ComputeHash(stream);
|
||||
}
|
||||
}
|
||||
|
@ -52,7 +52,7 @@
|
||||
/// <returns>The <see cref="Enum"/> value corresponding to the <see cref="string"/></returns>
|
||||
public static T EnumParse<T>(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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -92,8 +91,20 @@
|
||||
/// <param name="str">The string to convert.</param>
|
||||
/// <param name="encoding">The encoding to use.</param>
|
||||
/// <returns>Returns a <see cref="byte"/>[].</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> or <paramref name="encoding"/> or both are
|
||||
/// <see langword="null"/>.</exception>
|
||||
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 @@
|
||||
/// <param name="length">The length of the string to generate.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing <paramref name="length"/> characters.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
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 @@
|
||||
/// <param name="count">The repeat count.</param>
|
||||
/// <returns>Returns a <see cref="string"/> whose value is <paramref name="str"/> repeated
|
||||
/// <paramref name="count"/> times.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
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 @@
|
||||
/// <param name="str">The string to shuffle.</param>
|
||||
/// <param name="random">The <see cref="System.Random"/> instance.</param>
|
||||
/// <returns>Returns a <see cref="string"/> containing the characters in <paramref name="str"/>, rearranged.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
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 @@
|
||||
/// <param name="chunkSize">The maximum length of each string in the returned result.</param>
|
||||
/// <returns>Returns an <see cref="IEnumerable{T}"/> containing <see cref="string"/> instances which are no
|
||||
/// greater than <paramref name="chunkSize"/> in length.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
public static IEnumerable<string> 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 @@
|
||||
/// </summary>
|
||||
/// <param name="str">The string to convert.</param>
|
||||
/// <returns>Returns a <see cref="SecureString"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
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 @@
|
||||
/// <param name="str">The <see cref="SecureString"/> to convert.</param>
|
||||
/// <param name="extension">Whether or not to use this extension method.</param>
|
||||
/// <returns>Returns a <see cref="string"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -213,8 +260,14 @@
|
||||
/// </summary>
|
||||
/// <param name="str">The input string.</param>
|
||||
/// <returns>Returns an instance of <see cref="TimeSpan"/>.</returns>
|
||||
/// <exception cref="ArgumentNullException"><paramref name="str"/> is <see langword="null"/>.</exception>
|
||||
public static TimeSpan ToTimeSpan(this string str)
|
||||
{
|
||||
if (str is null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(str));
|
||||
}
|
||||
|
||||
return TimeSpanParser.Parse(str);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user