Skip to content

Commit a46a792

Browse files
Add missing early-exit for LastIndexOfOrdinal (#33601)
1 parent 7b1ff42 commit a46a792

File tree

1 file changed

+18
-0
lines changed
  • src/libraries/System.Private.CoreLib/src/System/Globalization

1 file changed

+18
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/Globalization/CompareInfo.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,11 +1362,29 @@ public int LastIndexOf(string source, string value, int startIndex, int count, C
13621362

13631363
internal static int LastIndexOfOrdinal(string source, string value, int startIndex, int count, bool ignoreCase)
13641364
{
1365+
Debug.Assert(source != null);
1366+
Debug.Assert(value != null);
1367+
13651368
if (GlobalizationMode.Invariant)
13661369
{
13671370
return InvariantLastIndexOf(source, value, startIndex, count, ignoreCase);
13681371
}
13691372

1373+
// For ordinal (non-linguistic) comparisons, an empty target string is always
1374+
// found at the end of the search space, and a non-empty target string
1375+
// can never be found within an empty search space. This assumption is not
1376+
// valid for linguistic comparisons, including InvariantCulture comparisons.
1377+
1378+
if (value.Length == 0)
1379+
{
1380+
return Math.Max(0, startIndex - count + 1);
1381+
}
1382+
1383+
if (count == 0)
1384+
{
1385+
return -1;
1386+
}
1387+
13701388
return LastIndexOfOrdinalCore(source, value, startIndex, count, ignoreCase);
13711389
}
13721390

0 commit comments

Comments
 (0)