-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Add missing early-exit for LastIndexOfOrdinal #33601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
||
| if (value.Length == 0) | ||
| { | ||
| return Math.Max(0, startIndex - count + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line is because the behavior of LastIndexOf is nutty. See the comment below (lifted from PR #1514) for more info.
runtime/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs
Lines 277 to 298 in 52aea3b
| * For LastIndexOf specifially, overloads which take a 'startIndex' and 'count' behave differently | |
| * than their IndexOf counterparts. 'startIndex' is the index of the last char element that should | |
| * be considered when performing the search. For example, if startIndex = 4, then the caller is | |
| * indicating "when finding the match I want you to include the char element at index 4, but not | |
| * any char elements past that point." | |
| * | |
| * idx = 0123456 ("abcdefg".Length = 7) | |
| * So, if the search string is "abcdefg", startIndex = 5 and count = 3, then the search space will | |
| * ~~~ be the substring "def", as highlighted to the left. | |
| * Essentially: "the search space should be of length 3 chars and should end *just after* the char | |
| * element at index 5." | |
| * | |
| * Since this behavior can introduce off-by-one errors in the boundary cases, we allow startIndex = -1 | |
| * with a zero-length 'searchString' (treated as equivalent to startIndex = 0), and we allow | |
| * startIndex = searchString.Length (treated as equivalent to startIndex = searchString.Length - 1). | |
| * | |
| * Note also that this behavior can introduce errors when dealing with UTF-16 surrogate pairs. | |
| * If the search string is the 3 chars "[BMP][HI][LO]", startIndex = 1 and count = 2, then the | |
| * ~~~~~~~~~ search space wil be the substring "[BMP][ HI]". | |
| * This means that the char [HI] is incorrectly seen as a standalone high surrogate, which could | |
| * lead to incorrect matching behavior, or it could cause LastIndexOf to incorrectly report that | |
| * a zero-weight character could appear between the [HI] and [LO] chars. |
|
@dotnet-bot help |
|
@dotnet-bot test runtime (Libraries Test Run checked coreclr Windows_NT x64 Debug) |
|
/azp list |
|
/azp run runtime (Libraries Test Run checked coreclr Windows_NT x86 Release) |
|
No pipelines are associated with this pull request. |
|
Don't worry, bots. I still love you. ❤️ |
|
/azp run runtime |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Hello @GrabYourPitchforks! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Fix #33596. We added the appropriate early-exit for
IndexOfOrdinalbut missed it forLastIndexOfOrdinal.