예제 #1
0
        /// <summary>
        /// Determines if a substring of the current string instance
        /// starts with the target string (or a substring of this target string) specified by <paramref name="target" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="startIndex">The start position of the substring of the current string instance.</param>
        /// <param name="target">The target string instance to compare.</param>
        /// <param name="targetStartIndex">The start position of the substring of the target string instance.</param>
        /// <param name="targetLength">The length of the substring of the target string specified by <paramref name="target" />.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring starts with the target substring (or a substring of this target string); otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Occurs when at least one of <paramref name="startIndex"/>, <paramref name="targetStartIndex"/> and <paramref name="targetLength"/> is assigned an invalid value.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">Occurs when the argument <paramref name="target"/> is <c>null</c>.</exception>
        public static bool StartsWith(this string source, int startIndex, string target, int targetStartIndex, int targetLength, StringComparison comparisonType)
        {
            if (targetLength < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetLength"));
            }

            if (targetStartIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetStartIndex"));
            }

            var realTargetLen = target?.Length ?? 0;

            var targetEndIndex = targetStartIndex + targetLength;

            if (targetEndIndex > realTargetLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("targetLength", 0, true, target.Length - targetStartIndex, true));
            }

            var sourceLen = source.Length;

            if (startIndex < 0 && startIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }
            sourceLen -= startIndex;
            return(sourceLen >= targetLength && string.Compare(source, startIndex, target, targetStartIndex, targetLength, comparisonType) == 0);
        }
예제 #2
0
 /// <summary>
 /// For internal use only. Checks whether the arguments <paramref name="startIndex"/> and <paramref name="length"/> are valid for a backward search method.
 /// </summary>
 /// <param name="startIndex">The start index indicating the position of the last character of the search scope.</param>
 /// <param name="length">The length of the search scope.</param>
 /// <param name="startIndexLimit">The integer immediately larger than maximum value which <paramref name="startIndex"/> can reach.</param>
 /// <param name="argNameForStartIndex">The argument name for <paramref name="startIndex"/> which will be displayed in the exception message.</param>
 /// <param name="argNameForLength">The argument name for <paramref name="length"/> which will be displayed in the exception message.</param>
 /// <returns>The end index, namely <paramref name="startIndex"/> plus <paramref name="length"/>.</returns>
 /// <exception cref="System.ArgumentOutOfRangeException">Occurs when the value of either <paramref name="startIndex"/> or <paramref name="length"/> is not valid.</exception>
 internal static int BackwardCheckStartIndexAndLength(int startIndex, int length, int startIndexLimit, string argNameForStartIndex = "startIndex", string argNameForLength = "length")
 {
     if (startIndexLimit == 0)
     {
         if (startIndex != 0)
         {
             throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage(argNameForStartIndex, 0, true, 0, true));
         }
         else if (length != 0)
         {
             throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage(argNameForLength, 0, true, 0, true));
         }
         else
         {
             return(0);
         }
     }
     else
     {
         NonNegativeArgumentRequired(argNameForLength, length);
         ArgumentRangeRequired <int>(argNameForStartIndex, startIndex, 0, true, startIndexLimit - 1, true);
         var endIndex = startIndex - length;
         if (endIndex < -1)
         {
             throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage(argNameForLength, 0, true, startIndex + 1, true));
         }
         return(endIndex);
     }
 }
예제 #3
0
        /// <summary>
        /// Determines if a substring of the current string instance
        /// ends with the target string (or a substring of this target string) specified by <paramref name="target" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="endIndex">The end position of the substring of the current string instance.</param>
        /// <param name="target">The target string instance to compare.</param>
        /// <param name="targetStartIndex">The start position of the substring of the target string instance.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring ends with the target substring (or a substring of this target string); otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Occurs when either <paramref name="endIndex"/> or <paramref name="targetStartIndex"/> is assigned an invalid value.
        /// </exception>
        public static bool EndsWith(this string source, int endIndex, string target, int targetStartIndex, StringComparison comparisonType)
        {
            var sourceLen = source.Length;

            if (endIndex < 0 && endIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }

            var targetLen = target == null ? 0 : target.Length;

            if (targetStartIndex < 0 && targetStartIndex >= targetLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("targetStartIndex", 0, true, targetLen, false));
            }

            targetLen -= targetStartIndex;
            if (targetLen != 0 && targetLen != 1)
            {
                endIndex -= (targetLen - 1);
            }
            if (endIndex < 0)
            {
                return(false);
            }
            return(string.Compare(source, endIndex, target, targetStartIndex, targetLen, comparisonType) == 0);
        }
예제 #4
0
        /// <summary>
        /// Reports the index of the last character satisfying the specified predicate.
        /// The search starts at a specified character position and examines a specified number of character positions toward the beginning of this string instance.
        /// </summary>
        /// <param name="str">This string instance.</param>
        /// <param name="predicate">A function to test each character of the current string.</param>
        /// <param name="startIndex">The search starting position. The search proceeds from <paramref name="startIndex" /> toward the beginning of this instance.</param>
        /// <param name="count">The number of character positions to examine.</param>
        /// <returns>
        /// The zero-based index position of the last character in this instance satisfying the specified predicate.
        /// </returns>
        public static int LastIndexOf(this string str, Func <char, bool> predicate, int startIndex, int count)
        {
            if (count < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("count"));
            }
            if (startIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, str.Length - 1, true));
            }
            var endIndex = startIndex - count;

            if (endIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("count", 0, true, startIndex, true));
            }

            for (var i = startIndex; i >= endIndex; --i)
            {
                if (predicate(str[i]))
                {
                    return(i);
                }
            }
            return(-1);
        }
예제 #5
0
        /// <summary>
        /// Determines if a substring of the current string instance starts with the target string specified by <paramref name="value" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="startIndex">The start position of the substring.</param>
        /// <param name="value">The string instance to compare.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring starts with the target string; otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">Occurs when <paramref name="startIndex"/> is assigned an invalid value.</exception>
        public static bool StartsWith(this string source, int startIndex, string value, StringComparison comparisonType)
        {
            var sourceLen = source.Length;

            if (startIndex < 0 && startIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }

            sourceLen -= startIndex;
            var targetLen = value?.Length ?? 0;

            return(sourceLen >= targetLen && string.Compare(source, startIndex, value, 0, targetLen, comparisonType) == 0);
        }
예제 #6
0
        /// <summary>
        /// Determines if a substring of the current string instance
        /// starts with the target string (or a substring of this target string) specified by <paramref name="target" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="startIndex">The start position of the substring of the current string instance.</param>
        /// <param name="target">The target string instance to compare.</param>
        /// <param name="targetStartIndex">The start position of the substring of the target string instance.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring starts with the target substring (or a substring of this target string); otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Occurs when either <paramref name="startIndex"/> or <paramref name="targetStartIndex"/> is assigned an invalid value.
        /// </exception>
        public static bool StartsWith(this string source, int startIndex, string target, int targetStartIndex, StringComparison comparisonType)
        {
            var sourceLen = source.Length;

            if (startIndex < 0 && startIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }

            var targetLen = target == null ? 0 : target.Length;

            if (targetStartIndex < 0 && targetStartIndex >= targetLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("targetStartIndex", 0, true, targetLen, false));
            }

            targetLen -= targetStartIndex;
            sourceLen -= startIndex;
            return(sourceLen >= targetLen && string.Compare(source, startIndex, target, targetStartIndex, targetLen, comparisonType) == 0);
        }
예제 #7
0
        /// <summary>
        /// Determines if a substring of the current string instance
        /// ends with the target string (or a substring of this target string) specified by <paramref name="target" />.
        /// </summary>
        /// <param name="source">This string instance.</param>
        /// <param name="endIndex">The end position of the substring of the current string instance.</param>
        /// <param name="target">The target string instance to compare.</param>
        /// <param name="targetStartIndex">The start position of the substring of the target string instance.</param>
        /// <param name="targetLength">The length of the substring of the target string specified by <paramref name="target" />.</param>
        /// <param name="comparisonType">One of the enumeration values that specifies the rules for the comparison.</param>
        /// <returns>
        ///   <c>true</c> if the specified substring ends with the target substring (or a substring of this target string); otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// Occurs when at least one of <paramref name="endIndex"/>, <paramref name="targetStartIndex"/> and <paramref name="targetLength"/> is assigned an invalid value.
        /// </exception>
        /// <exception cref="System.ArgumentNullException">Occurs when the argument <paramref name="target"/> is <c>null</c>.</exception>
        public static bool EndsWith(this string source, int endIndex, string target, int targetStartIndex, int targetLength, StringComparison comparisonType)
        {
            if (targetLength < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetLength"));
            }

            if (targetStartIndex < 0)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetNonNegativeArgumentRequiredMessage("targetStartIndex"));
            }

            var realTargetLen = target == null ? 0 : target.Length;

            var targetEndIndex = targetStartIndex + targetLength;

            if (targetEndIndex > realTargetLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("targetLength", 0, true, target.Length - targetStartIndex, true));
            }

            var sourceLen = source.Length;

            if (endIndex < 0 && endIndex >= sourceLen)
            {
                throw new ArgumentOutOfRangeException(ExceptionHelper.GetArgumentOutOfRangeMessage("startIndex", 0, true, sourceLen, false));
            }

            if (targetLength != 0 && targetLength != 1)
            {
                endIndex -= (targetLength - 1);
            }
            if (endIndex < 0)
            {
                return(false);
            }
            return(string.Compare(source, endIndex, target, targetStartIndex, targetLength, comparisonType) == 0);
        }