Exemplo n.º 1
0
        /// <summary>
        ///     Main generation method which chooses the algorithm to use for the generation.
        /// </summary>
        private string GenerateString(int length)
        {
            if (length == 0)
            {
                throw new ArgumentException("You can't generate a string of a zero length");
            }
            if (!UseUpperCaseCharacters && !UseLowerCaseCharacters && !UseNumericCharacters && !UseSpecialCharacters)
            {
                throw new ArgumentException("There should be at least one character set in use");
            }
            if (!RepeatCharacters && (CurrentGeneralCharacters.Length < length))
            {
                throw new ArgumentException("There is not enough characters to create a string without repeats");
            }
            var result = ""; // This string will contain the result

            if (PatternDriven)
            {
                // Using the pattern to generate a string
                result = PatternDrivenAlgo(Pattern);
            }
            else if (MinUpperCaseCharacters == 0 && MinLowerCaseCharacters == 0 &&
                     MinNumericCharacters == 0 && MinSpecialCharacters == 0)
            {
                // Using the simpliest algorithm in this case
                result = SimpleGenerateAlgo(length);
            }
            else
            {
                // Paying attention to limits
                result = GenerateAlgoWithLimits(length);
            }
            // Support for unique strings
            if (UniqueStrings && ExistingStrings.Contains(result))
            {
                return(GenerateString(length));
            }
            AddExistingString(result); // Saving history
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Main generation method which chooses the algorithm to use for the generation.
        ///     It checks some exceptional situations as well.
        /// </summary>
        private string GenerateString(int length)
        {
            if (length == 0)
            {
                throw new ArgumentException("You can't generate a string of a zero length");
            }
            if (!UseUpperCaseCharacters && !UseLowerCaseCharacters && !UseNumericCharacters && !UseSpecialCharacters)
            {
                throw new ArgumentException("There should be at least one character set in use");
            }
            if (!RepeatCharacters && (CurrentGeneralCharacters.Length < length))
            {
                throw new ArgumentException("There is not enough characters to create a string without repeats");
            }
            var result = ""; // This string will contain the result

            if (PatternDriven)
            {
                result = PatternDrivenAlgo(Pattern);
            }
            else if ((MinUpperCaseCharacters == 0) && (MinLowerCaseCharacters == 0) && (MinNumericCharacters == 0) &&
                     (MinSpecialCharacters == 0))
            {
                result = SimpleGenerateAlgo(length);
            }
            else
            {
                result = GenerateAlgoWithLimits(length);
            }
            // Support for unique strings
            // Recursion, but possibility of the stack overflow is low for big strings (> 3 chars).
            if (UniqueStrings && ExistingStrings.Contains(result))
            {
                return(GenerateString(length));
            }
            AddExistingString(result); // Saving history
            return(result);
        }