From 210e525b9da50abd9e8e037ca6ca5d775e1c5641 Mon Sep 17 00:00:00 2001 From: Levi Broderick Date: Sat, 14 Mar 2020 15:11:49 -0700 Subject: [PATCH] Add missing early-exit for LastIndexOfOrdinal --- .../src/System/Globalization/CompareInfo.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) 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); }