/// <summary>
        /// Builds a new <see cref="StringValueExpression"/> instance that can wrap a string value
        /// </summary>
        /// <param name="value">value of the expression.</param>
        /// <remarks>
        /// The <see cref="EscapedParseableString"/> property automatically escapes <see cref="SpecialCharacters"/> from <paramref name="value"/>.
        /// </remarks>
        public StringValueExpression(string value) : base(value)
        {
            _lazyParseableString = new(() =>
            {
                // The length of the final parseable string in worst cases scenario will double (1 backlash + the escaped character)
                // Also we need an extra position for the final '*' that will be append in all cases
                bool requireEscapingCharacters = value.AtLeastOnce(chr => SpecialCharacters.Contains(chr));
                StringBuilder parseableString;

                if (requireEscapingCharacters)
                {
                    parseableString = new((value.Length * 2) + 1);
                    foreach (char chr in value)
                    {
                        if (SpecialCharacters.Contains(chr))
                        {
                            parseableString = parseableString.Append('\\');
                        }
                        parseableString = parseableString.Append(chr);
                    }
                }
                else
                {
                    parseableString = new(value);
                }

                return(parseableString.ToString());
            });
        }
Exemplo n.º 2
0
            private int GetMatchLength(Match match)
            {
                if (SpecialCharacters.Contains(match.Value[match.Value.Length - 1]))
                {
                    return(match.Length - 1);
                }

                return(match.Length);
            }
Exemplo n.º 3
0
        /// <summary>
        /// Validates special characters in string
        /// </summary>
        /// <param name="string">Value to be validated</param>
        /// <param name="error">Error message if fails, contains string format which interpolates character which failed at {0} format.</param>
        private static void ValidateSpecialCharacters(string @string, string error)
        {
            const string SpecialCharacters = "~`!@#$%^&*()_+={}[]:;'<>,.?/|";

            foreach (var @char in @string)
            {
                if (SpecialCharacters.Contains(@char))
                {
                    throw new Exception(string.Format(error, @char));
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Builds a new <see cref="ContainsExpression"/> that holds the specified <paramref name="value"/>.
        /// </summary>
        /// <param name="value"></param>
        /// <exception cref="ArgumentNullException">if <paramref name="value"/> is <c>null</c></exception>
        /// <exception cref="ArgumentOutOfRangeException">if <paramref name="value"/> is <c>empty</c></exception>
        public ContainsExpression(string value)
        {
            if (value is null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            if (value.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(value), $"{nameof(value)} cannot be empty");
            }

            Value = value;

            _lazyEscapedParseableString = new(() =>
            {
                // The length of the final parseable string in worst cases scenario will double (1 backlash + the escaped character)
                // Also we need an extra position for the final '*' that will be append in all cases
                bool requireEscapingCharacters = Value.AtLeastOnce(chr => SpecialCharacters.Contains(chr));
                StringBuilder parseableString;

                if (requireEscapingCharacters)
                {
                    parseableString = new((Value.Length * 2) + 2);
                    foreach (char chr in Value)
                    {
                        if (SpecialCharacters.Contains(chr))
                        {
                            parseableString = parseableString.Append('\\');
                        }
                        parseableString = parseableString.Append(chr);
                    }
                }
                else
                {
                    parseableString = new(Value, Value.Length + 2);
                }

                return(parseableString.Insert(0, "*").Append('*').ToString());
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// https://www.hackerrank.com/challenges/strong-password/problem
        /// </summary>
        /// <param name="n">An <c>int</c> representing the length of <c>password</c>.</param>
        /// <param name="password">A <c>string</c> representing a password.</param>
        /// <returns>
        /// The number characters needed to make <c>password</c> valid as an <c>int</c>.
        /// </returns>
        public static int MinimumNumber(int n, string password)
        {
            const string SpecialCharacters = "!@#$%^&*()-+";
            var          count             = 0;

            if (!password.Any(character => char.IsDigit(character)))
            {
                count++;
            }
            if (!password.Any(character => char.IsLower(character)))
            {
                count++;
            }
            if (!password.Any(character => char.IsUpper(character)))
            {
                count++;
            }
            if (!password.Any(character => SpecialCharacters.Contains(character)))
            {
                count++;
            }
            return(Math.Max(count, 6 - password.Length));
        }