< Summary

Information
Class: MyNet.Text.TextRandomGeneratorExtensions
Assembly: MyNet.Text
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Extensions/TextRandomGeneratorExtensions.cs
Tag: 323_28699572109
Line coverage
0%
Covered lines: 0
Uncovered lines: 30
Coverable lines: 30
Total lines: 121
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ReplaceSymbolsByNumbers(...)100%210%
ReplaceSymbolsByNumbers(...)100%210%
ReplaceSymbols(...)0%4260%
ReplaceSymbols(...)100%210%
FitLength(...)0%506220%
FitLength(...)100%210%
RandomizePattern(...)100%210%
RandomizePattern(...)100%210%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.Text/Extensions/TextRandomGeneratorExtensions.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="TextRandomGeneratorExtensions.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8using MyNet.Generator;
 9using MyNet.Generator.Facade;
 10using MyNet.Text.Randomize;
 11
 12#pragma warning disable IDE0130 // Namespace does not match folder structure
 13namespace MyNet.Text;
 14#pragma warning restore IDE0130 // Namespace does not match folder structure
 15
 16public 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>
 026        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>
 033        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        {
 044            ArgumentNullException.ThrowIfNull(symbols);
 045            ArgumentNullException.ThrowIfNull(func);
 46
 047            var buffer = value.Length <= 256
 048                ? stackalloc char[value.Length]
 049                : new char[value.Length];
 50
 051            for (var i = 0; i < value.Length; i++)
 52            {
 053                var c = value[i];
 054                buffer[i] = symbols.AsSpan().Contains(c)
 055                    ? func(c)
 056                    : c;
 57            }
 58
 059            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>
 068        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        {
 079            ArgumentNullException.ThrowIfNull(value);
 80
 081            if (min is < 0 || max is < 0)
 082                throw new ArgumentOutOfRangeException(min is < 0 ? nameof(min) : nameof(max), "min/max cannot be negativ
 83
 084            if (min > max)
 085                throw new ArgumentOutOfRangeException(nameof(min), "min cannot be greater than max.");
 86
 087            var result = value;
 088            if (max is not null && result.Length > max)
 089                result = result[..max.Value];
 90
 091            if (min is null || min <= result.Length)
 092                return result;
 93
 094            var missingChars = min.Value - result.Length;
 095            var fillerChars = string.Empty.PadRight(missingChars, '?').RandomizePattern(random);
 096            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>
 0105        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>
 0112        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>
 0118        public string RandomizePattern() => value.RandomizePattern(TextRandomGenerator.Current);
 119    }
 120}
 121