public void LastIndexOf_Char(CompareInfo compareInfo, string source, char value, int startIndex, int count, CompareOptions options, int expected)
 {
     if (options == CompareOptions.None)
     {
         // Use LastIndexOf(string, char, int, int) or LastIndexOf(string, char)
         if (startIndex + 1 == source.Length && count == source.Length)
         {
             // Use LastIndexOf(string, char)
             Assert.Equal(expected, compareInfo.LastIndexOf(source, value));
         }
         // Use LastIndexOf(string, char, int, int)
         Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count));
     }
     if (count - startIndex - 1 == 0)
     {
         // Use LastIndexOf(string, char, int, CompareOptions) or LastIndexOf(string, char, CompareOptions)
         if (startIndex == source.Length)
         {
             // Use LastIndexOf(string, char, CompareOptions)
             Assert.Equal(expected, compareInfo.LastIndexOf(source, value, options));
         }
         // Use LastIndexOf(string, char, int, CompareOptions)
         Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, options));
     }
     // Use LastIndexOf(string, char, int, int, CompareOptions)
     Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count, options));
 }
        public void LastIndexOf_String(CompareInfo compareInfo, string source, string value, int startIndex, int count, CompareOptions options, int expected)
        {
            if (value.Length == 1)
            {
                LastIndexOf_Char(compareInfo, source, value[0], startIndex, count, options, expected);
            }
            if (options == CompareOptions.None)
            {
                // Use LastIndexOf(string, string, int, int) or LastIndexOf(string, string)
                if (startIndex + 1 == source.Length && count == source.Length)
                {
                    // Use LastIndexOf(string, string)
                    Assert.Equal(expected, compareInfo.LastIndexOf(source, value));
                }
                // Use LastIndexOf(string, string, int, int)
                Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count));
            }
            if (count - startIndex - 1 == 0)
            {
                // Use LastIndexOf(string, string, int, CompareOptions) or LastIndexOf(string, string, CompareOptions)
                if (startIndex == source.Length)
                {
                    // Use LastIndexOf(string, string, CompareOptions)
                    Assert.Equal(expected, compareInfo.LastIndexOf(source, value, options));
                }
                // Use LastIndexOf(string, string, int, CompareOptions)
                Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, options));
            }
            // Use LastIndexOf(string, string, int, int, CompareOptions)
            Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count, options));

            if ((compareInfo == s_invariantCompare) && ((options == CompareOptions.None) || (options == CompareOptions.IgnoreCase)))
            {
                StringComparison stringComparison = (options == CompareOptions.IgnoreCase) ? StringComparison.InvariantCultureIgnoreCase : StringComparison.InvariantCulture;

                // Use int string.LastIndexOf(string, StringComparison)
                Assert.Equal(expected, source.LastIndexOf(value, startIndex, count, stringComparison));

                // Use int MemoryExtensions.LastIndexOf(this ReadOnlySpan<char>, ReadOnlySpan<char>, StringComparison)
                // Filter differences betweeen string-based and Span-based LastIndexOf
                // - Empty value handling - https://github.com/dotnet/runtime/issues/13382
                // - Negative count
                if (value.Length == 0 || count < 0)
                {
                    return;
                }

                if (startIndex == source.Length)
                {
                    startIndex--;
                    if (count > 0)
                    {
                        count--;
                    }
                }
                int leftStartIndex = (startIndex - count + 1);
                Assert.Equal((expected == -1) ? -1 : (expected - leftStartIndex), source.AsSpan(leftStartIndex, count).LastIndexOf(value.AsSpan(), stringComparison));
            }
        }
Exemple #3
0
    public static void LastIndexOfArgumentOutOfRangeIndex(string source, char value, int badStartIndex)
    {
        CompareInfo ci            = CultureInfo.InvariantCulture.CompareInfo;
        string      valueAsString = value.ToString();

        Assert.Throws <ArgumentOutOfRangeException>(() => ci.LastIndexOf(source, value, badStartIndex, CompareOptions.Ordinal));
        Assert.Throws <ArgumentOutOfRangeException>(() => ci.LastIndexOf(source, valueAsString, badStartIndex, CompareOptions.Ordinal));
    }
Exemple #4
0
    public static void LastIndexOfBadCompareOptions()
    {
        CompareInfo ci = CultureInfo.InvariantCulture.CompareInfo;

        Assert.Throws <ArgumentException>(() => ci.LastIndexOf("abc", "a", CompareOptions.Ordinal | CompareOptions.IgnoreWidth));
        Assert.Throws <ArgumentException>(() => ci.LastIndexOf("abc", "a", CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));
        Assert.Throws <ArgumentException>(() => ci.LastIndexOf("abc", "a", (CompareOptions)(-1)));
        Assert.Throws <ArgumentException>(() => ci.LastIndexOf("abc", "a", CompareOptions.StringSort));
    }
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        string s1 = "ani\u00ADmal";
        string s2 = "animal";

        // Find the index of the last soft hyphen.
        Console.WriteLine(ci.LastIndexOf(s1, '\u00AD'));
        Console.WriteLine(ci.LastIndexOf(s2, '\u00AD'));
    }
Exemple #6
0
        public void IndexOfTest(CompareInfo compareInfo, string source, string value, int startIndex, int indexOfExpected, int lastIndexOfExpected)
        {
            Assert.Equal(indexOfExpected, compareInfo.IndexOf(source, value, startIndex));
            if (value.Length == 1)
            {
                Assert.Equal(indexOfExpected, compareInfo.IndexOf(source, value[0], startIndex));
            }

            Assert.Equal(lastIndexOfExpected, compareInfo.LastIndexOf(source, value, startIndex));
            if (value.Length == 1)
            {
                Assert.Equal(lastIndexOfExpected, compareInfo.LastIndexOf(source, value[0], startIndex));
            }
        }
Exemple #7
0
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        string s1 = "ani\u00ADmal";
        string s2 = "animal";

        // Find the last index of the soft hyphen using culture-sensitive comparison.
        Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', CompareOptions.IgnoreCase));
        Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', CompareOptions.IgnoreCase));

        // Find the last index of the soft hyphen using ordinal comparison.
        Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', CompareOptions.Ordinal));
        Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', CompareOptions.Ordinal));
    }
        // Token: 0x060039B7 RID: 14775 RVA: 0x00105DC4 File Offset: 0x00103FC4
        private static int StandardMatchIndexCalculation(string textString, string findPattern, bool matchWholeWord, bool matchLast, bool ignoreCase, CompareInfo compareInfo, bool hasPreceedingSeparatorChar, bool hasFollowingSeparatorChar, out int matchLength)
        {
            CompareOptions options = ignoreCase ? CompareOptions.IgnoreCase : CompareOptions.None;
            int            num     = -1;
            int            num2    = 0;
            int            i       = textString.Length;

            matchLength = 0;
            while (i > 0)
            {
                num         = (matchLast ? compareInfo.LastIndexOf(textString, findPattern, num2 + i - 1, i, options) : compareInfo.IndexOf(textString, findPattern, num2, i, options));
                matchLength = findPattern.Length;
                if (num == -1 || !matchWholeWord || TextFindEngine.IsAtWordBoundary(textString, num, matchLength, hasPreceedingSeparatorChar, hasFollowingSeparatorChar))
                {
                    break;
                }
                if (matchLast)
                {
                    num2 = 0;
                    i    = num + matchLength - 1;
                }
                else
                {
                    num2 = num + 1;
                    i    = textString.Length - num2;
                }
                num = -1;
            }
            return(num);
        }
Exemple #9
0
        public void Test(CultureInfo culture, string str1, string str2, int expected, CompareOptions options)
        {
            CompareInfo ci = culture.CompareInfo;
            int         i  = ci.LastIndexOf(str1, str2, options);

            Assert.Equal(expected, i);
        }
    public bool TestOrd(CultureInfo culture, string str1, string str2, int expected, CompareOptions options, string id)
    {
        CompareInfo ci     = culture.CompareInfo;
        bool        result = true;

        if (str1 == null || str2 == null || (str1.Length < 100 && str2.Length < 100))
        {
            TestFramework.BeginScenario(id + ": Comparing " + ((str1 == null) ? "null" : str1) + " / " + ((str2 == null) ? "null" : str2) + "; options: " + options + "; culture: " + ci.Name);
        }
        else
        {
            TestFramework.BeginScenario(id + ": Comparing LongStr (" + str1.Length + ") / LongStr(" + str2.Length + "); options: " + options + "; culture: " + ci.Name);
        }
        try
        {
            int i = ci.LastIndexOf(str1, str2, options);
            if (i != expected)
            {
                result = false;
                TestFramework.LogError("001", "Error in " + id + ", unexpected comparison result. Actual: " + i + ", Expected: " + expected);
            }
        }
        catch (Exception exc)
        {
            result = false;
            TestFramework.LogError("003", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
        }
        return(result);
    }
    public bool Test(CultureInfo culture, string str1, string str2, int expected, CompareOptions options, string id)
    {
        CompareInfo ci = culture.CompareInfo;

        if (!id.Contains("s") || !Utilities.IsVistaOrLater)         //Due Windows 7 bug 130925
        {
            expected = GlobLocHelper.OSLastIndexOf(culture, str1, str2, options);
        }
        bool result = true;

        if (str1 == null || str2 == null || (str1.Length < 100 && str2.Length < 100))
        {
            TestFramework.BeginScenario(id + ": Comparing " + ((str1 == null) ? "null" : str1) + " / " + ((str2 == null) ? "null" : str2) + "; options: " + options + "; culture: " + ci.Name);
        }
        else
        {
            TestFramework.BeginScenario(id + ": Comparing LongStr (" + str1.Length + ") / LongStr(" + str2.Length + "); options: " + options + "; culture: " + ci.Name);
        }
        try
        {
            int i = ci.LastIndexOf(str1, str2, options);
            if (i != expected)
            {
                result = false;
                TestFramework.LogError("001z", "Error in " + id + ", unexpected comparison result. Actual: " + i + ", Expected: " + expected);
            }
        }
        catch (Exception exc)
        {
            result = false;
            TestFramework.LogError("003z", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
        }
        return(result);
    }
        public void LastIndexOf_String(CompareInfo compareInfo, string source, string value, int startIndex, int count, CompareOptions options, int expected)
        {
            if (value.Length == 1)
            {
                LastIndexOf_Char(compareInfo, source, value[0], startIndex, count, options, expected);
            }
            if (options == CompareOptions.None)
            {
                // Use LastIndexOf(string, string, int, int) or LastIndexOf(string, string)
                if (startIndex + 1 == source.Length && count == source.Length)
                {
                    // Use LastIndexOf(string, string)
                    Assert.Equal(expected, compareInfo.LastIndexOf(source, value));
                }
                // Use LastIndexOf(string, string, int, int)
                Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count));
            }
            if (count - startIndex - 1 == 0)
            {
                // Use LastIndexOf(string, string, int, CompareOptions) or LastIndexOf(string, string, CompareOptions)
                if (startIndex == source.Length)
                {
                    // Use LastIndexOf(string, string, CompareOptions)
                    Assert.Equal(expected, compareInfo.LastIndexOf(source, value, options));
                }
                // Use LastIndexOf(string, string, int, CompareOptions)
                Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, options));
            }
            // Use LastIndexOf(string, string, int, int, CompareOptions)
            Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count, options));

            // Fixup offsets so that we can call the span-based APIs.

            ReadOnlySpan <char> sourceSpan;
            int adjustmentFactor; // number of chars to add to retured index from span-based APIs

            if (startIndex == source.Length - 1 && count == source.Length)
            {
                // This idiom means "read the whole span"
                sourceSpan       = source;
                adjustmentFactor = 0;
            }
            else if (startIndex == source.Length)
            {
                // Account for possible off-by-one at the call site
                sourceSpan       = source.AsSpan()[^ (Math.Max(0, count - 1))..];
Exemple #13
0
        public void TestExc <T>(CultureInfo culture, string str1, string str2, CompareOptions options)
            where T : Exception
        {
            CompareInfo ci = culture.CompareInfo;

            Assert.Throws <T>(() =>
            {
                int i = ci.LastIndexOf(str1, str2, options);
            });
        }
Exemple #14
0
    public static void LastIndexOf(string localeName, string source, string value, int expectedResult, CompareOptions options)
    {
        CompareInfo ci = CompareInfo.GetCompareInfo(localeName);

        Assert.Equal(expectedResult, ci.LastIndexOf(source, value, options));

        if (value.Length == 1)
        {
            Assert.Equal(expectedResult, ci.LastIndexOf(source, value[0], options));
        }

        if (options == CompareOptions.None)
        {
            Assert.Equal(expectedResult, ci.LastIndexOf(source, value));
        }

        if (value.Length == 1 && options == CompareOptions.None)
        {
            Assert.Equal(expectedResult, ci.LastIndexOf(source, value[0]));
        }
    }
Exemple #15
0
        public void LastIndexOf(string culture, string source, string value, CompareOptions options)
        {
            CompareInfo compareInfo = CultureInfo.GetCultureInfo(culture).CompareInfo;

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    compareInfo.LastIndexOf(source, value, options);
                }
            }
        }
    public static void Main()
    {
        // Creates CompareInfo for the InvariantCulture.
        CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

        // iS is the starting index of the substring.
        int iS = 8;
        // iL is the length of the substring.
        int iL = 18;
        // myT1 and myT2 are the strings used for padding.
        String myT1 = new String('-', iS);
        String myT2;

        // Searches for the ligature Æ.
        String myStr = "Is AE or ae the same as Æ or æ?";

        myT2 = new String('-', myStr.Length - iS - iL);
        Console.WriteLine();
        Console.WriteLine("Original      : {0}", myStr);
        Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", iS, iL), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", iS, iL), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL));
        Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL, CompareOptions.Ordinal));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL, CompareOptions.Ordinal));
        Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Æ', iS + iL - 1, iL, CompareOptions.IgnoreCase));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'æ', iS + iL - 1, iL, CompareOptions.IgnoreCase));

        // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
        myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
        myT2  = new String('-', myStr.Length - iS - iL);
        Console.WriteLine();
        Console.WriteLine("Original      : {0}", myStr);
        Console.WriteLine("No options    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL));
        Console.WriteLine("Ordinal       : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL, CompareOptions.Ordinal));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL, CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL, CompareOptions.Ordinal));
        Console.WriteLine("IgnoreCase    : {0}{1}{2}", myT1, myStr.Substring(iS, iL), myT2);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Ü', iS + iL - 1, iL, CompareOptions.IgnoreCase));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', iS, iL, CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'ü', iS + iL - 1, iL, CompareOptions.IgnoreCase));
    }
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        string s1 = "ani\u00ADmal";
        string s2 = "animal";

        int position = 0;

        // Find the last index of the soft hyphen.
        position = ci.LastIndexOf(s1, 'm');
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position));
        }

        position = ci.LastIndexOf(s2, 'm');
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position));
        }
    }
Exemple #18
0
    public static void Main()
    {
        // Creates CompareInfo for the InvariantCulture.
        CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

        // Searches for the ligature Æ.
        String myStr = "Is AE or ae the same as Æ or æ?";

        Console.WriteLine();
        Console.WriteLine("No options    : {0}", myStr);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE"), myComp.LastIndexOf(myStr, "AE"));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae"), myComp.LastIndexOf(myStr, "ae"));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ'), myComp.LastIndexOf(myStr, 'Æ'));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ'), myComp.LastIndexOf(myStr, 'æ'));
        Console.WriteLine("Ordinal       : {0}", myStr);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "AE", CompareOptions.Ordinal));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "ae", CompareOptions.Ordinal));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Æ', CompareOptions.Ordinal));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'æ', CompareOptions.Ordinal));
        Console.WriteLine("IgnoreCase    : {0}", myStr);
        PrintMarker("           AE : ", myComp.IndexOf(myStr, "AE", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "AE", CompareOptions.IgnoreCase));
        PrintMarker("           ae : ", myComp.IndexOf(myStr, "ae", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "ae", CompareOptions.IgnoreCase));
        PrintMarker("            Æ : ", myComp.IndexOf(myStr, 'Æ', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Æ', CompareOptions.IgnoreCase));
        PrintMarker("            æ : ", myComp.IndexOf(myStr, 'æ', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'æ', CompareOptions.IgnoreCase));

        // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
        myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";
        Console.WriteLine();
        Console.WriteLine("No options    : {0}", myStr);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308"), myComp.LastIndexOf(myStr, "U\u0308"));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308"), myComp.LastIndexOf(myStr, "u\u0308"));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü'), myComp.LastIndexOf(myStr, 'Ü'));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü'), myComp.LastIndexOf(myStr, 'ü'));
        Console.WriteLine("Ordinal       : {0}", myStr);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "U\u0308", CompareOptions.Ordinal));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", CompareOptions.Ordinal), myComp.LastIndexOf(myStr, "u\u0308", CompareOptions.Ordinal));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'Ü', CompareOptions.Ordinal));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', CompareOptions.Ordinal), myComp.LastIndexOf(myStr, 'ü', CompareOptions.Ordinal));
        Console.WriteLine("IgnoreCase    : {0}", myStr);
        PrintMarker("           U\u0308 : ", myComp.IndexOf(myStr, "U\u0308", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "U\u0308", CompareOptions.IgnoreCase));
        PrintMarker("           u\u0308 : ", myComp.IndexOf(myStr, "u\u0308", CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, "u\u0308", CompareOptions.IgnoreCase));
        PrintMarker("            Ü : ", myComp.IndexOf(myStr, 'Ü', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'Ü', CompareOptions.IgnoreCase));
        PrintMarker("            ü : ", myComp.IndexOf(myStr, 'ü', CompareOptions.IgnoreCase), myComp.LastIndexOf(myStr, 'ü', CompareOptions.IgnoreCase));
    }
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        string s1 = "ani\u00ADmal";
        string s2 = "animal";

        int position = 0;

        // Find the index of the soft hyphen using culture-sensitive comparison.
        position = ci.LastIndexOf(s1, 'm');
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position,
                                             position + 1, CompareOptions.IgnoreCase));
        }

        position = ci.LastIndexOf(s2, 'm');
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position,
                                             position + 1, CompareOptions.IgnoreCase));
        }

        // Find the index of the soft hyphen using ordinal comparison.
        position = ci.LastIndexOf(s1, 'm');
        Console.WriteLine("'m' at position {0}", position, CompareOptions.Ordinal);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, '\u00AD', position,
                                             position + 1, CompareOptions.Ordinal));
        }

        position = ci.LastIndexOf(s2, 'm');
        Console.WriteLine("'m' at position {0}", position, CompareOptions.Ordinal);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, '\u00AD', position,
                                             position + 1, CompareOptions.Ordinal));
        }
    }
Exemple #20
0
        // Returns the index and length of the first or last occurance of one string
        // within another string.
        private static int StandardMatchIndexCalculation(string textString, string findPattern, bool matchWholeWord, bool matchLast, bool ignoreCase, CompareInfo compareInfo, bool hasPreceedingSeparatorChar, bool hasFollowingSeparatorChar, out int matchLength)
        {
            CompareOptions options      = ignoreCase ? CompareOptions.IgnoreCase : 0;
            int            matchIndex   = -1;
            int            searchStart  = 0;
            int            searchLength = textString.Length;

            matchLength = 0;

            while (searchLength > 0)
            {
                matchIndex = matchLast ?
                             compareInfo.LastIndexOf(textString, findPattern, searchStart + searchLength - 1, searchLength, options) :
                             compareInfo.IndexOf(textString, findPattern, searchStart, searchLength, options);

                matchLength = findPattern.Length;

                if (matchIndex == -1)
                {
                    break;
                }

                if (!matchWholeWord || IsAtWordBoundary(textString, matchIndex, matchLength, hasPreceedingSeparatorChar, hasFollowingSeparatorChar))
                {
                    break;
                }

                if (matchLast)
                {
                    searchStart  = 0;
                    searchLength = matchIndex + matchLength - 1;
                }
                else
                {
                    searchStart  = matchIndex + 1;
                    searchLength = textString.Length - searchStart;
                }

                matchIndex = -1;
            }

            return(matchIndex);
        }
    public bool TestExc(CultureInfo culture, string str1, string str2, Type expected, CompareOptions options, string id)
    {
        CompareInfo ci     = culture.CompareInfo;
        bool        result = true;

        TestFramework.BeginScenario(id + ": Comparing " + str1 + " / " + str2 + "; options: " + options + "; culture: " + ci.Name);
        try
        {
            int i = ci.LastIndexOf(str1, str2, options);
            result = false;
            TestFramework.LogError("004", "Error in " + id + ", expected exception did not occur. Comparison result: " + i);
        }
        catch (Exception exc)
        {
            if (!exc.GetType().Equals(expected))
            {
                result = false;
                TestFramework.LogError("005", "Unexpected exception in " + id + ", excpetion: " + exc.ToString());
            }
        }
        return(result);
    }
Exemple #22
0
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        string searchString = "\u00ADm";
        string s1           = "ani\u00ADmal";
        string s2           = "animal";
        int    position;

        position = ci.LastIndexOf(s1, 'm');
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, searchString, position, CompareOptions.None));
            Console.WriteLine(ci.LastIndexOf(s1, searchString, position, CompareOptions.Ordinal));
        }

        position = ci.LastIndexOf(s2, 'm');
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, searchString, position, CompareOptions.None));
            Console.WriteLine(ci.LastIndexOf(s2, searchString, position, CompareOptions.Ordinal));
        }
    }
Exemple #23
0
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        int position = 0;

        string s1 = "ani\u00ADmal";
        string s2 = "animal";

        // All the following comparisons are culture-sensitive.
        Console.WriteLine("Culture-sensitive comparisons:");
        // Find the index of the soft hyphen.
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00AD", position,
                                             position + 1, CompareOptions.None));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00AD", position,
                                             position + 1, CompareOptions.None));
        }

        // Find the index of the soft hyphen followed by "n".
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn", position,
                                             position + 1, CompareOptions.IgnoreCase));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn", position,
                                             position + 1, CompareOptions.IgnoreCase));
        }

        // Find the index of the soft hyphen followed by "m".
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm", position,
                                             position + 1, CompareOptions.IgnoreCase));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm", position,
                                             position + 1, CompareOptions.IgnoreCase));
        }

        // All the following comparisons are ordinal.
        Console.WriteLine("\nOrdinal comparisons:");
        // Find the index of the soft hyphen.
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00AD", position,
                                             position + 1, CompareOptions.Ordinal));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00AD", position,
                                             position + 1, CompareOptions.Ordinal));
        }

        // Find the index of the soft hyphen followed by "n".
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn", position,
                                             position + 1, CompareOptions.Ordinal));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn", position,
                                             position + 1, CompareOptions.Ordinal));
        }

        // Find the index of the soft hyphen followed by "m".
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm", position,
                                             position + 1, CompareOptions.Ordinal));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm", position,
                                             position + 1, CompareOptions.Ordinal));
        }
    }
Exemple #24
0
 internal static int LastIndexOf(string source, string value)
 {
     return(InvariantCompareInfo.LastIndexOf(source, value, CompareOptions.Ordinal));
 }
        public void LastIndexOf_Invalid()
        {
            // Source is null
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, "a"));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, "a", CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, "a", 0, 0));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, "a", 0, CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, "a", 0, 0, CompareOptions.None));

            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, 'a'));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, 'a', CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, 'a', 0, 0));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, 'a', 0, CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, 'a', 0, 0, CompareOptions.None));

            // Value is null
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf("", null));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf("", null, CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf("", null, 0, 0));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf("", null, 0, CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf("", null, 0, 0, CompareOptions.None));

            // Source and value are null
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, null));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, null, CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, null, 0, 0));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, null, 0, CompareOptions.None));
            Assert.Throws <ArgumentNullException>(() => s_invariantCompare.LastIndexOf(null, null, 0, 0, CompareOptions.None));

            // Options are invalid
            Assert.Throws <ArgumentException>(() => s_invariantCompare.LastIndexOf("Test's", "Tests", CompareOptions.StringSort));
            Assert.Throws <ArgumentException>(() => s_invariantCompare.LastIndexOf("Test's", "Tests", (CompareOptions)(-1)));
            Assert.Throws <ArgumentException>(() => s_invariantCompare.LastIndexOf("Test's", "Tests", (CompareOptions)0x11111111));
        }
        public void IndexOfTest(CompareInfo compareInfo, string source, string value, int startIndex, int indexOfExpected, int lastIndexOfExpected)
        {
            Assert.Equal(indexOfExpected, compareInfo.IndexOf(source, value, startIndex));
            if (value.Length > 0)
            {
                Assert.Equal(indexOfExpected, compareInfo.IndexOf(source, value[0], startIndex));
            }

            Assert.Equal(lastIndexOfExpected, compareInfo.LastIndexOf(source, value, startIndex));
            if (value.Length > 0)
            {
                Assert.Equal(lastIndexOfExpected, compareInfo.LastIndexOf(source, value[0], startIndex));
            }
        }
        public void LastIndexOf_Invalid()
        {
            // Source is null
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, "a"));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, "a", CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, "a", 0, 0));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, "a", 0, CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, "a", 0, 0, CompareOptions.None));

            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, 'a'));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, 'a', CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, 'a', 0, 0));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, 'a', 0, CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, 'a', 0, 0, CompareOptions.None));

            // Value is null
            AssertExtensions.Throws <ArgumentNullException>("value", () => s_invariantCompare.LastIndexOf("", null));
            AssertExtensions.Throws <ArgumentNullException>("value", () => s_invariantCompare.LastIndexOf("", null, CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("value", () => s_invariantCompare.LastIndexOf("", null, 0, 0));
            AssertExtensions.Throws <ArgumentNullException>("value", () => s_invariantCompare.LastIndexOf("", null, 0, CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("value", () => s_invariantCompare.LastIndexOf("", null, 0, 0, CompareOptions.None));

            // Source and value are null
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, null));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, null, CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, null, 0, 0));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, null, 0, CompareOptions.None));
            AssertExtensions.Throws <ArgumentNullException>("source", () => s_invariantCompare.LastIndexOf(null, null, 0, 0, CompareOptions.None));

            // Options are invalid
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", CompareOptions.StringSort));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, CompareOptions.StringSort));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, 2, CompareOptions.StringSort));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', CompareOptions.StringSort));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, CompareOptions.StringSort));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, 2, CompareOptions.StringSort));

            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", CompareOptions.Ordinal | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, CompareOptions.Ordinal | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, 2, CompareOptions.Ordinal | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', CompareOptions.Ordinal | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, CompareOptions.Ordinal | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, 2, CompareOptions.Ordinal | CompareOptions.IgnoreWidth));

            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, 2, CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, 2, CompareOptions.OrdinalIgnoreCase | CompareOptions.IgnoreWidth));

            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", (CompareOptions)(-1)));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, (CompareOptions)(-1)));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, 2, (CompareOptions)(-1)));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", (CompareOptions)(-1)));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, (CompareOptions)(-1)));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, 2, (CompareOptions)(-1)));

            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", (CompareOptions)0x11111111));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, (CompareOptions)0x11111111));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", "Tests", 0, 2, (CompareOptions)0x11111111));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', (CompareOptions)0x11111111));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, (CompareOptions)0x11111111));
            AssertExtensions.Throws <ArgumentException>("options", () => s_invariantCompare.LastIndexOf("Test's", 'a', 0, 2, (CompareOptions)0x11111111));

            // StartIndex < 0
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", "Test", -1, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", "Test", -1, 2));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", "Test", -1, 2, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", 'a', -1, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", 'a', -1, 2));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", 'a', -1, 2, CompareOptions.None));

            // StartIndex >= source.Length
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", "Test", 5, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", "Test", 5, 0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", "Test", 5, 0, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", 'a', 5, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", 'a', 5, 0));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("startIndex", () => s_invariantCompare.LastIndexOf("Test", 'a', 5, 0, CompareOptions.None));

            // Count < 0
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 0, -1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 0, -1, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 0, -1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 0, -1, CompareOptions.None));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 4, -1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 4, -1, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 4, -1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 4, -1, CompareOptions.None));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "", 4, -1));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "", 4, -1, CompareOptions.None));

            // Count > source.Length
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 0, 5));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 0, 5, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 0, 5));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 0, 5, CompareOptions.None));

            // StartIndex + count > source.Length + 1
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 3, 5));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "Test", 3, 5, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 3, 5));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 'a', 3, 5, CompareOptions.None));

            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "s", 4, 6));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", "s", 4, 7, CompareOptions.None));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 's', 4, 6));
            AssertExtensions.Throws <ArgumentOutOfRangeException>("count", () => s_invariantCompare.LastIndexOf("Test", 's', 4, 7, CompareOptions.None));
        }
 public void LastIndexOf_Char(CompareInfo compareInfo, string source, char value, int startIndex, int count, CompareOptions options, int expected)
 {
     if (options == CompareOptions.None)
     {
         // Use LastIndexOf(string, char, int, int) or LastIndexOf(string, char)
         if (startIndex + 1 == source.Length && count == source.Length)
         {
             // Use LastIndexOf(string, char)
             Assert.Equal(expected, compareInfo.LastIndexOf(source, value));
         }
         // Use LastIndexOf(string, char, int, int)
         Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count));
     }
     if (count - startIndex - 1 == 0)
     {
         // Use LastIndexOf(string, char, int, CompareOptions) or LastIndexOf(string, char, CompareOptions)
         if (startIndex == source.Length)
         {
             // Use LastIndexOf(string, char, CompareOptions)
             Assert.Equal(expected, compareInfo.LastIndexOf(source, value, options));
         }
         // Use LastIndexOf(string, char, int, CompareOptions)
         Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, options));
     }
     // Use LastIndexOf(string, char, int, int, CompareOptions)
     Assert.Equal(expected, compareInfo.LastIndexOf(source, value, startIndex, count, options));
 }
Exemple #29
0
            private object RFindWorker(string /*!*/ s, long start, long end)
            {
                ContractUtils.RequiresNotNull(s, nameof(s));

                start = PythonOps.FixSliceIndex(start, _view.Capacity);
                end   = PythonOps.FixSliceIndex(end, _view.Capacity);

                if (s == "")
                {
                    return(start <= end?ReturnLong(start) : -1);
                }

                long findLength = end - start;

                if (s.Length > findLength)
                {
                    return(-1);
                }

                int         index        = -1;
                int         bufferLength = Math.Max(s.Length, PAGESIZE);
                CompareInfo c            = CultureInfo.InvariantCulture.CompareInfo;

                if (findLength <= bufferLength * 2)
                {
                    // In this case, the search area is not significantly larger than s, so we only need to
                    // allocate a single string to search through.
                    byte[] buffer = new byte[findLength];

                    findLength = _view.ReadArray(start, buffer, 0, (int)findLength);

                    string findString = PythonOps.MakeString(buffer);
                    index = c.LastIndexOf(findString, s, CompareOptions.Ordinal);
                }
                else
                {
                    // We're matching s against a significantly larger file, so we partition the stream into
                    // sections twice the length of s and search each segment. Because a match could exist on a
                    // boundary, sections must overlap by s.Length. Data is saved in 2 buffers to avoid
                    // reading the same parts of the stream twice.
                    byte[] buffer0 = new byte[bufferLength];
                    byte[] buffer1 = new byte[bufferLength];

                    int remainder = (int)((end - start) % bufferLength);
                    if (remainder == 0)
                    {
                        remainder = bufferLength;
                    }

                    start       = end - bufferLength - remainder;
                    findLength -= bufferLength + remainder;

                    _view.ReadArray(start, buffer0, 0, bufferLength);
                    int bytesRead = _view.ReadArray(start + bufferLength, buffer1, 0, remainder);

                    while (findLength >= 0)
                    {
                        string findString = GetString(buffer0, buffer1, bytesRead);
                        index = c.LastIndexOf(findString, s, CompareOptions.Ordinal);

                        if (index != -1)
                        {
                            return(ReturnLong(index + start));
                        }

                        byte[] temp = buffer0;
                        buffer0 = buffer1;
                        buffer1 = temp;

                        start      -= bufferLength;
                        bytesRead   = _view.ReadArray(start, buffer0, 0, bufferLength);
                        findLength -= bytesRead;
                    }
                }

                return(index == -1 ? -1 : ReturnLong(index + start));
            }
        public void CompareInfoIndexTest2()
        {
            // Creates CompareInfo for the InvariantCulture.
            CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

            // iS is the starting index of the substring.
            int iS = 8;
            // iL is the length of the substring.
            int iL = 18;

            // Searches for the combining character sequence Latin capital letter U with diaeresis or Latin small letter u with diaeresis.
            string myStr = "Is \u0055\u0308 or \u0075\u0308 the same as \u00DC or \u00FC?";

            Assert.Equal(myComp.IndexOf(myStr, "U\u0308", iS, iL), 24);
            Assert.Equal(myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL), 24);

            Assert.Equal(myComp.IndexOf(myStr, "u\u0308", iS, iL), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL), 9);

            Assert.Equal(myComp.IndexOf(myStr, '\u00DC', iS, iL), 24);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00DC', iS + iL - 1, iL), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00FC', iS, iL), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00FC', iS + iL - 1, iL), 9);

            Assert.Equal(myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.Ordinal), -1);
            Assert.Equal(myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.Ordinal), -1);

            Assert.Equal(myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.Ordinal), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.Ordinal), 9);

            Assert.Equal(myComp.IndexOf(myStr, '\u00DC', iS, iL, CompareOptions.Ordinal), 24);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00DC', iS + iL - 1, iL, CompareOptions.Ordinal), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00FC', iS, iL, CompareOptions.Ordinal), -1);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00FC', iS + iL - 1, iL, CompareOptions.Ordinal), -1);

            Assert.Equal(myComp.IndexOf(myStr, "U\u0308", iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "U\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);

            Assert.Equal(myComp.IndexOf(myStr, "u\u0308", iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "u\u0308", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00DC', iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00DC', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00FC', iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00FC', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
        }
        public void CompareInfoIndexTest1()
        {
            // Creates CompareInfo for the InvariantCulture.
            CompareInfo myComp = CultureInfo.InvariantCulture.CompareInfo;

            // iS is the starting index of the substring.
            int iS = 8;
            // iL is the length of the substring.
            int iL = 18;

            // Searches for the ligature Æ.
            String myStr = "Is AE or ae the same as \u00C6 or \u00E6?";

            Assert.Equal(myComp.IndexOf(myStr, "AE", iS, iL), 24);
            Assert.Equal(myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL), 24);

            Assert.Equal(myComp.IndexOf(myStr, "ae", iS, iL), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL), 9);

            Assert.Equal(myComp.IndexOf(myStr, '\u00C6', iS, iL), 24);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00C6', iS + iL - 1, iL), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00E6', iS, iL), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00E6', iS + iL - 1, iL), 9);

            Assert.Equal(myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.Ordinal), -1);
            Assert.Equal(myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.Ordinal), -1);

            Assert.Equal(myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.Ordinal), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.Ordinal), 9);

            Assert.Equal(myComp.IndexOf(myStr, '\u00C6', iS, iL, CompareOptions.Ordinal), 24);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00C6', iS + iL - 1, iL, CompareOptions.Ordinal), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00E6', iS, iL, CompareOptions.Ordinal), -1);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00E6', iS + iL - 1, iL, CompareOptions.Ordinal), -1);

            Assert.Equal(myComp.IndexOf(myStr, "AE", iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "AE", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);

            Assert.Equal(myComp.IndexOf(myStr, "ae", iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, "ae", iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00C6', iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00C6', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);

            Assert.Equal(myComp.IndexOf(myStr, '\u00E6', iS, iL, CompareOptions.IgnoreCase), 9);
            Assert.Equal(myComp.LastIndexOf(myStr, '\u00E6', iS + iL - 1, iL, CompareOptions.IgnoreCase), 24);
        }
Exemple #32
0
    public static void Main()
    {
        CompareInfo ci = CultureInfo.CurrentCulture.CompareInfo;

        int position = 0;

        string s1 = "ani\u00ADmal";
        string s2 = "animal";

        // Find the index of the soft hyphen.
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00AD", position, position + 1));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00AD", position, position + 1));
        }

        // Find the index of the soft hyphen followed by "n".
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00ADn", position, position + 1));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00ADn", position, position + 1));
        }

        // Find the index of the soft hyphen followed by "m".
        position = ci.LastIndexOf(s1, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s1, "\u00ADm", position, position + 1));
        }

        position = ci.LastIndexOf(s2, "m");
        Console.WriteLine("'m' at position {0}", position);
        if (position >= 0)
        {
            Console.WriteLine(ci.LastIndexOf(s2, "\u00ADm", position, position + 1));
        }
    }