Esempio n. 1
0
        /// <summary>
        ///  Returns a flag indicating whether a given substring exists in the StringBuilder.
        /// </summary>
        /// <param name="str">The StringBuilder to inspect.</param>
        /// <param name="substring">The substring to look for.</param>
        /// <returns>True if the StringBuilder contains the string; false otherwise.</returns>
        public static bool Contains(this StringBuilder str, string substring)
        {
            _logger.DebugMethodCalled(str, substring);

            #region Input Validation

            Insist.IsNotNullOrEmpty(substring, "substring");

            #endregion

            bool result = false;

            if (str != null &&
                str.Length >= substring.Length)
            {
                char[] substringChars = substring.ToCharArray();
                int    endIndex       = (str.Length - substring.Length);

                for (int i = 0; i <= endIndex; i++)
                {
                    char[] tempChars = GetChars(str, i, i + (substring.Length - 1));

                    if (Match(tempChars, substringChars))
                    {
                        result = true;
                        break;
                    }
                }
            }

            return(result);
        }
Esempio n. 2
0
 public void IsNotNullOrEmpty_Thrown_Exception_Has_Correct_Argument_Name()
 {
     try {
         Insist.IsNotNullOrEmpty(null, ARGUMENT_NAME);
     } catch (ArgumentException e) {
         Assert.AreEqual(ARGUMENT_NAME, e.ParamName);
     }
 }
        /// <summary>
        /// Constructs a new attribute with the specified description
        /// </summary>
        /// <param name="description">
        /// The description that will be applied to the enum members
        /// </param>
        /// <exception cref="System.ArgumentException">
        /// Thrown if description is null or empty.
        /// </exception>
        public DescriptionAttribute(string description)
        {
            #region Input validation

            Insist.IsNotNullOrEmpty(description, "description");

            #endregion

            _description = description;
        }
Esempio n. 4
0
 public void IsNotNullOrEmpty_Thrown_Exception_Has_Correct_Message()
 {
     try
     {
         Insist.IsNotNullOrEmpty(null, ARGUMENT_NAME, MESSAGE);
     }
     catch (ArgumentException e)
     {
         Assert.IsTrue(e.Message.Contains(MESSAGE));
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Hashes a string using the supplied encoding and the supplied hashing algorithm
        /// </summary>
        /// <param name="str">
        /// The string to compute the hash for
        /// </param>
        /// <param name="hashAlgorithm">
        /// The hashing algorithm to use to perform the hash.
        /// </param>
        /// <param name="encoding">
        /// The encoding used to encode the string into a byte[]
        /// </param>
        /// <returns>
        /// A string representing the hash of the original input string.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// Thrown if str is null or empty.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// Thrown if hashAlgorithm is null or encoding is null.
        /// </exception>
        public static string Hash(this string str, HashAlgorithm hashAlgorithm, Encoding encoding)
        {
            _logger.DebugMethodCalled(str, hashAlgorithm, encoding);

            Insist.IsNotNullOrEmpty(str, "str");
            Insist.IsNotNull(hashAlgorithm, "hashAlgorithm");
            Insist.IsNotNull(encoding, "encoding");

            byte[] strBytes = encoding.GetBytes(str);
            byte[] hashed   = hashAlgorithm.ComputeHash(strBytes, 0, strBytes.Length);
            return(BitConverter.ToString(hashed).Replace("-", ""));
        }
        /// <summary>
        ///  Returns a flag indicating whether the given key in the collection is either null or empty.
        /// </summary>
        /// <param name="collection">The collection to check.</param>
        /// <param name="key">The key to check.</param>
        /// <returns>True if the value is null or empty; false otherwise.</returns>
        public static bool IsNullOrEmpty(this NameValueCollection collection, string key)
        {
            _logger.DebugMethodCalled(collection, key);

            #region Input validation

            Insist.IsNotNull(collection, "collection");
            Insist.IsNotNullOrEmpty(key, "key");

            #endregion

            return(collection[key] == null || String.IsNullOrEmpty(collection[key]));
        }
Esempio n. 7
0
        /// <summary>
        ///  Returns a flag indicating whehter the StringBuilder ends with the given string.
        /// </summary>
        /// <param name="str">The StringBuilder to inspect.</param>
        /// <param name="end">The string to look for.</param>
        /// <param name="comparison">The string comparison to be used.</param>
        /// <returns>True if the StringBuilder ends with the given string; false otherwise.</returns>
        public static bool EndsWith(this StringBuilder str, string end, StringComparison comparison)
        {
            _logger.DebugMethodCalled(str, end, comparison);

            Insist.IsNotNullOrEmpty(end, "end");

            bool result = false;

            if (str != null &&
                str.Length >= end.Length)
            {
                result = str.SubString(str.Length - end.Length).Equals(end, comparison);
            }

            return(result);
        }
Esempio n. 8
0
        /// <summary>
        ///  Returns a flag indicating whether the StringBuilder starts with
        ///  the string given.
        /// </summary>
        /// <param name="str">The StringBuilder to inspect.</param>
        /// <param name="start">The string to look for.</param>
        /// <param name="comparison">The string comparison to be used.</param>
        /// <returns>True if the StringBuilder starts with the given string; false otherwise.</returns>
        public static bool StartsWith(this StringBuilder str, string start, StringComparison comparison)
        {
            _logger.DebugMethodCalled(str, start, comparison);

            Insist.IsNotNullOrEmpty(start, "start");

            bool result = false;

            if (str != null &&
                str.Length >= start.Length)
            {
                result = str.SubString(0, start.Length).Equals(start, comparison);
            }

            return(result);
        }
Esempio n. 9
0
 public void IsNotNullOrEmpty_2_Null_Or_Empty_Value_Throws_Exception(string argValue)
 {
     Insist.IsNotNullOrEmpty(argValue, ARGUMENT_NAME);
 }
Esempio n. 10
0
 public void IsNotNullOrEmpty_1_Non_Null_Or_Empty_Value_Does_Not_Throw_Exception()
 {
     Insist.IsNotNullOrEmpty("This is not null", "value");
 }
Esempio n. 11
0
 public void IsNotNullOrEmpty_1_Null_Or_Empty_Value_Throws_Exception(string argValue)
 {
     Insist.IsNotNullOrEmpty(argValue, "argVlue");
 }
Esempio n. 12
0
 public void IsNotNullOrEmpty_2_Non_Null_Or_Empty_Value_Does_Not_Throw_Exception()
 {
     Insist.IsNotNullOrEmpty("This is not null.", ARGUMENT_NAME);
 }
Esempio n. 13
0
        /// <summary>
        /// Formats the supplied string using the supplied arguments
        /// </summary>
        /// <param name="str">
        /// The string pattern to format
        /// </param>
        /// <param name="args">
        /// A list of arguments used in the pattern
        /// </param>
        /// <returns>
        /// The formatted string
        /// </returns>
        /// <exception cref="System.ArgumentException">
        /// Thrown when str is null or empty.
        /// </exception>
        public static string Format(this string str, params object[] args)
        {
            Insist.IsNotNullOrEmpty(str, "str");

            return(string.Format(str, args));
        }