From b8d9d880a8ae59b9ba287974f2145376d1e04de4 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Wed, 10 Mar 2021 12:51:09 +0000 Subject: [PATCH] Rename string.Random to string.Randomize Implements better logic for generating a random string --- X10D/src/StringExtensions/StringExtensions.cs | 70 ++++++++++++++----- 1 file changed, 54 insertions(+), 16 deletions(-) diff --git a/X10D/src/StringExtensions/StringExtensions.cs b/X10D/src/StringExtensions/StringExtensions.cs index 8540d2a..77e5905 100644 --- a/X10D/src/StringExtensions/StringExtensions.cs +++ b/X10D/src/StringExtensions/StringExtensions.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Text; using X10D.EnumerableExtensions; using X10D.ListExtensions; +using X10D.RandomExtensions; namespace X10D.StringExtensions { @@ -235,32 +236,69 @@ namespace X10D.StringExtensions } /// - /// Generates a new random string by filling it with characters found in . + /// Returns a new string of a specified length by randomly selecting characters from the current string. /// - /// The character set. - /// The length of the string to generate. - /// Returns a containing characters. - public static string Random(this string str, int length) + /// The pool of characters to use. + /// The length of the new string returned. + /// + /// A new string whose length is equal to which contains randomly selected characters from + /// . + /// + /// is . + /// is less than 0. + public static string Randomize(this string source, int length) { - return str.Random(length, RandomExtensions.Random); + return source.Randomize(length, RandomExtensions.RandomExtensions.Random); } /// - /// Generates a new random string by filling it with characters found in . + /// Returns a new string of a specified length by randomly selecting characters from the current string. /// - /// The character set. - /// 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) + /// The pool of characters to use. + /// The length of the new string returned. + /// The supplier. + /// + /// A new string whose length is equal to which contains randomly selected characters from + /// . + /// + /// + /// is . + /// -or- + /// is . + /// + /// is less than 0. + public static string Randomize(this string source, int length, Random random) { - if (str is null) + if (source is null) { - throw new ArgumentNullException(nameof(str)); + throw new ArgumentNullException(nameof(source)); } - return str.ToCharArray().Random(length, random); + if (random is null) + { + throw new ArgumentNullException(nameof(random)); + } + + if (length < 0) + { + throw new ArgumentOutOfRangeException(nameof(length), ExceptionMessages.LengthGreaterThanOrEqualTo0); + } + + if (length == 0) + { + return string.Empty; + } + + var array = source.ToCharArray(); + var builder = new StringBuilder(length); + + while (builder.Length < length) + { + var next = random.NextFrom(array); + builder.Append(next); + } + + return builder.ToString(); } ///