| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TextRandomGeneratorExtensions.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.Generator; |
| | | 9 | | using MyNet.Generator.Facade; |
| | | 10 | | using MyNet.Text.Randomize; |
| | | 11 | | |
| | | 12 | | #pragma warning disable IDE0130 // Namespace does not match folder structure |
| | | 13 | | namespace MyNet.Text; |
| | | 14 | | #pragma warning restore IDE0130 // Namespace does not match folder structure |
| | | 15 | | |
| | | 16 | | public static class TextRandomGeneratorExtensions |
| | | 17 | | { |
| | | 18 | | extension(string value) |
| | | 19 | | { |
| | | 20 | | /// <summary> |
| | | 21 | | /// Replaces characters in the string with random numbers based on the provided symbols. Each character in the s |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="random">The random generator used to generate random values.</param> |
| | | 24 | | /// <param name="symbols">The symbols to be replaced with random numbers.</param> |
| | | 25 | | /// <returns>A new string with the specified symbols replaced by random numbers.</returns> |
| | 0 | 26 | | public string ReplaceSymbolsByNumbers(IRandomGenerator random, params char[] symbols) => value.ReplaceSymbols(ra |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Replaces characters in the string with random numbers based on the provided symbols. Each character in the s |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="symbols">The symbols to be replaced with random numbers.</param> |
| | | 32 | | /// <returns>A new string with the specified symbols replaced by random numbers.</returns> |
| | 0 | 33 | | public string ReplaceSymbolsByNumbers(params char[] symbols) => value.ReplaceSymbolsByNumbers(RandomGenerator.Cu |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Replaces characters in the string with random letters based on the provided symbols. Each character in the s |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="random">The random generator used to generate random values.</param> |
| | | 39 | | /// <param name="symbols">The symbols to be replaced with random letters.</param> |
| | | 40 | | /// <param name="func">A function that defines how each symbol is replaced with a random letter.</param> |
| | | 41 | | /// <returns>A new string with the specified symbols replaced by random letters.</returns> |
| | | 42 | | public string ReplaceSymbols(IRandomGenerator random, char[] symbols, Func<char, char> func) |
| | | 43 | | { |
| | 0 | 44 | | ArgumentNullException.ThrowIfNull(symbols); |
| | 0 | 45 | | ArgumentNullException.ThrowIfNull(func); |
| | | 46 | | |
| | 0 | 47 | | var buffer = value.Length <= 256 |
| | 0 | 48 | | ? stackalloc char[value.Length] |
| | 0 | 49 | | : new char[value.Length]; |
| | | 50 | | |
| | 0 | 51 | | for (var i = 0; i < value.Length; i++) |
| | | 52 | | { |
| | 0 | 53 | | var c = value[i]; |
| | 0 | 54 | | buffer[i] = symbols.AsSpan().Contains(c) |
| | 0 | 55 | | ? func(c) |
| | 0 | 56 | | : c; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | return new(buffer); |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Replaces characters in the string with random letters based on the provided symbols. Each character in the s |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="symbols">The symbols to be replaced with random letters.</param> |
| | | 66 | | /// <param name="func">A function that defines how each symbol is replaced with a random letter.</param> |
| | | 67 | | /// <returns>A new string with the specified symbols replaced by random letters.</returns> |
| | 0 | 68 | | public string ReplaceSymbols(char[] symbols, Func<char, char> func) => value.ReplaceSymbols(RandomGenerator.Curr |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Adjusts the length of the string to fit within the specified minimum and maximum bounds. If the string is sh |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="random">The random generator used to generate random values.</param> |
| | | 74 | | /// <param name="min">The minimum length of the string.</param> |
| | | 75 | | /// <param name="max">The maximum length of the string.</param> |
| | | 76 | | /// <returns>The adjusted string.</returns> |
| | | 77 | | public string FitLength(ITextRandomGenerator random, int? min = null, int? max = null) |
| | | 78 | | { |
| | 0 | 79 | | ArgumentNullException.ThrowIfNull(value); |
| | | 80 | | |
| | 0 | 81 | | if (min is < 0 || max is < 0) |
| | 0 | 82 | | throw new ArgumentOutOfRangeException(min is < 0 ? nameof(min) : nameof(max), "min/max cannot be negativ |
| | | 83 | | |
| | 0 | 84 | | if (min > max) |
| | 0 | 85 | | throw new ArgumentOutOfRangeException(nameof(min), "min cannot be greater than max."); |
| | | 86 | | |
| | 0 | 87 | | var result = value; |
| | 0 | 88 | | if (max is not null && result.Length > max) |
| | 0 | 89 | | result = result[..max.Value]; |
| | | 90 | | |
| | 0 | 91 | | if (min is null || min <= result.Length) |
| | 0 | 92 | | return result; |
| | | 93 | | |
| | 0 | 94 | | var missingChars = min.Value - result.Length; |
| | 0 | 95 | | var fillerChars = string.Empty.PadRight(missingChars, '?').RandomizePattern(random); |
| | 0 | 96 | | return result + fillerChars; |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <summary> |
| | | 100 | | /// Adjusts the length of the string to fit within the specified minimum and maximum bounds. If the string is sh |
| | | 101 | | /// </summary> |
| | | 102 | | /// <param name="min">The minimum length of the string.</param> |
| | | 103 | | /// <param name="max">The maximum length of the string.</param> |
| | | 104 | | /// <returns>The adjusted string.</returns> |
| | 0 | 105 | | public string FitLength(int? min = null, int? max = null) => value.FitLength(TextRandomGenerator.Current, min, m |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Randomizes the format of the string based on predefined rules. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="random">The random generator used to generate random values.</param> |
| | | 111 | | /// <returns>A new string with the format randomized.</returns> |
| | 0 | 112 | | public string RandomizePattern(ITextRandomGenerator random) => random.Randomize(value); |
| | | 113 | | |
| | | 114 | | /// <summary> |
| | | 115 | | /// Randomizes the format of the string based on predefined rules. |
| | | 116 | | /// </summary> |
| | | 117 | | /// <returns>A new string with the format randomized.</returns> |
| | 0 | 118 | | public string RandomizePattern() => value.RandomizePattern(TextRandomGenerator.Current); |
| | | 119 | | } |
| | | 120 | | } |
| | | 121 | | |