예제 #1
0
        /// <summary>
        /// Reports the zero-based index of the first occurrence of the specified <paramref name="value"/> in the current <paramref name="span"/>.
        /// <param name="span">The source span.</param>
        /// <param name="value">The value to seek within the source span.</param>
        /// <param name="comparisonType">One of the enumeration values that determines how the <paramref name="span"/> and <paramref name="value"/> are compared.</param>
        /// </summary>
        public static int IndexOf(this ReadOnlySpan <char> span, ReadOnlySpan <char> value, StringComparison comparisonType)
        {
            string.CheckStringComparison(comparisonType);

            if (value.Length == 0)
            {
                return(0);
            }

            if (span.Length == 0)
            {
                return(-1);
            }

            switch (comparisonType)
            {
            case StringComparison.CurrentCulture:
                return(SpanHelpers.IndexOfCultureHelper(span, value, CultureInfo.CurrentCulture.CompareInfo));

            case StringComparison.CurrentCultureIgnoreCase:
                return(SpanHelpers.IndexOfCultureIgnoreCaseHelper(span, value, CultureInfo.CurrentCulture.CompareInfo));

            case StringComparison.InvariantCulture:
                return(SpanHelpers.IndexOfCultureHelper(span, value, CompareInfo.Invariant));

            case StringComparison.InvariantCultureIgnoreCase:
                return(SpanHelpers.IndexOfCultureIgnoreCaseHelper(span, value, CompareInfo.Invariant));

            case StringComparison.Ordinal:
                return(SpanHelpers.IndexOfOrdinalHelper(span, value, ignoreCase: false));

            case StringComparison.OrdinalIgnoreCase:
                return(SpanHelpers.IndexOfOrdinalHelper(span, value, ignoreCase: true));
            }

            Debug.Fail("StringComparison outside range");
            return(-1);
        }