Exemple #1
0
        public static TokenType GetTypeByChar(char c)
        {
            const string WHITESPACE = " \t";
            const string DELIMITERS = "+-*/^%()<>=&|!,";
            const string NUMBERS    = ".0123456789";
            const string LETTERS    = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_";

            if (WHITESPACE.IndexOf(c) >= 0)
            {
                return(TokenType.Whitespace);
            }
            if (DELIMITERS.IndexOf(c) >= 0)
            {
                return(TokenType.Delimeter);
            }
            if (NUMBERS.IndexOf(c) >= 0)
            {
                return(TokenType.Number);
            }
            if (LETTERS.IndexOf(c) >= 0)
            {
                return(TokenType.Letter);
            }

            throw new InvalidEquationException("Invalid token found in equation: " + c);
        }
        public static int ToDecimal(this string source, int @base)
        {
            const int    NUMBEROFDIGITS = 10;
            const string NUMBERS        = "0123456789ABCDEF";

            if (source == null)
            {
                throw new ArgumentNullException(nameof(source), "Null source string!");
            }

            if (@base < 2 || @base > 16)
            {
                throw new ArgumentOutOfRangeException(nameof(@base), "Base must be between 2 and 16 inclusively!");
            }

            int    result = 0;
            string number = source.ToLowerInvariant();

            string pattern = NUMBERS.Substring(0, @base);

            int radix = 1;

            // getting current digit
            for (int i = source.Length - 1; i >= 0; i--)
            {
                int digit = pattern.IndexOf(char.ToUpper(source[i]));
                if (digit == -1)
                {
                    throw new ArgumentException("Invalid source string!", nameof(source));
                }

                try
                {
                    checked
                    {
                        result += radix * digit;
                        if (i > 0)
                        {
                            radix *= @base;
                        }
                    }
                }
                catch (OverflowException e)
                {
                    throw new ArgumentException("Too big number!", nameof(source), e);
                }
            }

            return(result);
        }
Exemple #3
0
        private async Task <double> ReadNumber(char c0)
        {
            bool         hasDecimal = false;
            const string NUMBERS    = "0123456789.";
            var          sb         = new StringBuilder();
            char         c          = c0;

            while (true)
            {
                if (c == '.')
                {
                    if (hasDecimal)
                    {
                        throw new FormatException("Invalid number; only one decimal point is allowed");
                    }
                    else
                    {
                        hasDecimal = true;
                    }
                }

                sb.Append(c);

                try
                {
                    c = await ReadNextChar();
                }
                catch (EndOfStreamException)
                {
                    break;
                }
                if (!NUMBERS.Contains(c))
                {
                    UnreadChar(c);
                    break;
                }
            }

            return(double.Parse(sb.ToString()));
        }
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static string GenerateRawPassword()
        {
            const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2;

            const string SMALL_LETTERS = "abcdefghijklmnopqrstuvwxyz";

            const string CAPTAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            const string NUMBERS = "0123456789";

            const string SPECIAL_LETTERS = @"!#$%&*@\";
            //random possible character pool
            List <string> CHARACTER_ARRAY = new List <string> {
                SMALL_LETTERS, CAPTAL_LETTERS, NUMBERS, SPECIAL_LETTERS
            };

            //character pool to generate raw password.
            string characterSet = "";


            //get 3 types from random pool
            System.Random random = new System.Random();
            CHARACTER_ARRAY.RemoveAt(random.Next(0, 3));

            for (int i = 0; i < 3; i++)
            {
                characterSet += CHARACTER_ARRAY[i];
            }


            //raw password holder
            char[] password = new char[8];

            for (int characterPosition = 0; characterPosition < 8; characterPosition++)
            {
                password[characterPosition] = characterSet[random.Next(characterSet.Length - 1)];

                bool moreThanTwoIdenticalInARow =
                    characterPosition >= MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS &&
                    password[characterPosition] == password[characterPosition - 1] &&
                    password[characterPosition - 1] == password[characterPosition - 2];

                if (moreThanTwoIdenticalInARow)
                {
                    characterPosition--;
                }
            }

            //check if 8 random characters are 3 different types

            int SMALL_LETTER_COUNT   = 0;
            int CAPITAL_LETTER_COUNT = 0;
            int NUMBER_COUNT         = 0;
            int SPECIAL_LETTER_COUNT = 0;

            //Cout each type
            for (int i = 0; i < password.Length; i++)
            {
                if (SMALL_LETTERS.Contains(password[i]))
                {
                    SMALL_LETTER_COUNT++;
                }
                else if (CAPTAL_LETTERS.Contains(password[i]))
                {
                    CAPITAL_LETTER_COUNT++;
                }
                else if (NUMBERS.Contains(password[i]))
                {
                    NUMBER_COUNT++;
                }
                else if (SPECIAL_LETTERS.Contains(password[i]))
                {
                    SPECIAL_LETTER_COUNT++;
                }
            }

            List <string> LEFT_RANDOM = new List <string>();

            if (SMALL_LETTER_COUNT == 0)
            {
                LEFT_RANDOM.Add(SMALL_LETTERS);
            }
            if (CAPITAL_LETTER_COUNT == 0)
            {
                LEFT_RANDOM.Add(CAPTAL_LETTERS);
            }

            if (NUMBER_COUNT == 0)
            {
                LEFT_RANDOM.Add(NUMBERS);
            }

            if (SPECIAL_LETTER_COUNT == 0)
            {
                LEFT_RANDOM.Add(SPECIAL_LETTERS);
            }

            string str_password = new string(password);

            if (LEFT_RANDOM.Count > 1)
            {
                //get random Character set wheren count = 0
                string leftcharSet = LEFT_RANDOM[random.Next(LEFT_RANDOM.Count)];

                //replace last character with another random character
                str_password = str_password.Remove(7, 1) + leftcharSet[random.Next(leftcharSet.Length - 1)].ToString();
            }

            return(str_password);
        }
        public static string Generate6DigitPassword()
        {
            const int MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS = 2;

            const string CAPTAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

            const string NUMBERS = "0123456789";

            //random possible character pool
            List <string> CHARACTER_ARRAY = new List <string> {
                CAPTAL_LETTERS, NUMBERS
            };

            //character pool to generate raw password.
            string characterSet = "";

            System.Random random = new System.Random();

            //raw password holder
            char[] password = new char[6];

            for (int characterPosition = 0; characterPosition < 6; characterPosition++)
            {
                password[characterPosition] = characterSet[random.Next(characterSet.Length - 1)];

                bool moreThanTwoIdenticalInARow =
                    characterPosition >= MAXIMUM_IDENTICAL_CONSECUTIVE_CHARS &&
                    password[characterPosition] == password[characterPosition - 1] &&
                    password[characterPosition - 1] == password[characterPosition - 2];

                if (moreThanTwoIdenticalInARow)
                {
                    characterPosition--;
                }
            }

            //check if 6 random characters are 2 different types

            int CAPITAL_LETTER_COUNT = 0;
            int NUMBER_COUNT         = 0;

            //Count each type
            for (int i = 0; i < password.Length; i++)
            {
                if (CAPTAL_LETTERS.Contains(password[i].ToString()))
                {
                    CAPITAL_LETTER_COUNT++;
                }
                else if (NUMBERS.Contains(password[i].ToString()))
                {
                    NUMBER_COUNT++;
                }
            }

            List <string> LEFT_RANDOM = new List <string>();


            if (CAPITAL_LETTER_COUNT == 0)
            {
                LEFT_RANDOM.Add(CAPTAL_LETTERS);
            }

            if (NUMBER_COUNT == 0)
            {
                LEFT_RANDOM.Add(NUMBERS);
            }


            string str_password = string.Join(null, password);

            if (LEFT_RANDOM.Count > 1)
            {
                //get random Character set wheren count = 0
                string leftcharSet = LEFT_RANDOM[random.Next(LEFT_RANDOM.Count - 1)];

                //replace last character with another random character
                str_password = str_password.Remove(7, 1) + random.Next(leftcharSet.Length - 1).ToString();
            }

            return(str_password);
        }