diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs index 1f89a953b409bf..e0b4da0588d1de 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs @@ -1362,11 +1362,29 @@ public int LastIndexOf(string source, string value, int startIndex, int count, C internal static int LastIndexOfOrdinal(string source, string value, int startIndex, int count, bool ignoreCase) { + Debug.Assert(source != null); + Debug.Assert(value != null); + if (GlobalizationMode.Invariant) { return InvariantLastIndexOf(source, value, startIndex, count, ignoreCase); } + // For ordinal (non-linguistic) comparisons, an empty target string is always + // found at the end of the search space, and a non-empty target string + // can never be found within an empty search space. This assumption is not + // valid for linguistic comparisons, including InvariantCulture comparisons. + + if (value.Length == 0) + { + return Math.Max(0, startIndex - count + 1); + } + + if (count == 0) + { + return -1; + } + return LastIndexOfOrdinalCore(source, value, startIndex, count, ignoreCase); }