mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-10 05:35:41 +00:00
Rename string.Random to string.Randomize
Implements better logic for generating a random string
This commit is contained in:
parent
00b751b555
commit
b8d9d880a8
@ -4,6 +4,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using X10D.EnumerableExtensions;
|
using X10D.EnumerableExtensions;
|
||||||
using X10D.ListExtensions;
|
using X10D.ListExtensions;
|
||||||
|
using X10D.RandomExtensions;
|
||||||
|
|
||||||
namespace X10D.StringExtensions
|
namespace X10D.StringExtensions
|
||||||
{
|
{
|
||||||
@ -235,32 +236,69 @@ namespace X10D.StringExtensions
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates a new random string by filling it with characters found in <see cref="str" />.
|
/// Returns a new string of a specified length by randomly selecting characters from the current string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="str">The character set.</param>
|
/// <param name="source">The pool of characters to use.</param>
|
||||||
/// <param name="length">The length of the string to generate.</param>
|
/// <param name="length">The length of the new string returned.</param>
|
||||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
/// <returns>
|
||||||
public static string Random(this string str, int length)
|
/// A new string whose length is equal to <paramref name="length" /> which contains randomly selected characters from
|
||||||
|
/// <paramref name="source" />.
|
||||||
|
/// </returns>
|
||||||
|
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is less than 0.</exception>
|
||||||
|
public static string Randomize(this string source, int length)
|
||||||
{
|
{
|
||||||
return str.Random(length, RandomExtensions.Random);
|
return source.Randomize(length, RandomExtensions.RandomExtensions.Random);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Generates a new random string by filling it with characters found in <see cref="str" />.
|
/// Returns a new string of a specified length by randomly selecting characters from the current string.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="str">The character set.</param>
|
/// <param name="source">The pool of characters to use.</param>
|
||||||
/// <param name="length">The length of the string to generate.</param>
|
/// <param name="length">The length of the new string returned.</param>
|
||||||
/// <param name="random">The <see cref="System.Random" /> instance.</param>
|
/// <param name="random">The <see cref="System.Random" /> supplier.</param>
|
||||||
/// <returns>Returns a <see cref="string" /> containing <paramref name="length" /> characters.</returns>
|
/// <returns>
|
||||||
/// <exception cref="ArgumentNullException"><paramref name="str" /> is <see langword="null" />.</exception>
|
/// A new string whose length is equal to <paramref name="length" /> which contains randomly selected characters from
|
||||||
public static string Random(this string str, int length, Random random)
|
/// <paramref name="source" />.
|
||||||
|
/// </returns>
|
||||||
|
/// <exception cref="ArgumentNullException">
|
||||||
|
/// <paramref name="source" /> is <see langword="null" />.
|
||||||
|
/// -or-
|
||||||
|
/// <paramref name="random" /> is <see langword="null" />.
|
||||||
|
/// </exception>
|
||||||
|
/// <exception cref="ArgumentOutOfRangeException"><paramref name="length" /> is less than 0.</exception>
|
||||||
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user