-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Fix various LastIndexOf bugs when dealing with zero-length target substrings #34616
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
Fix various LastIndexOf bugs when dealing with zero-length target substrings #34616
Conversation
- string.LastIndexOf(string.Empty) shouldn't perform -1 adjustment - MemoryExtensions.LastIndexOf(ROS<char>, string.Empty) shouldn't return 0 - Tighten up some existing unit tests
|
Note to @tarekgh: This PR doesn't have the "avoid validating |
|
CC @safern just in case he is touching some of the changed files here. |
tarekgh
left a comment
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.
I like the change and now our behavior is more understandable. only one thing, we need to document this as a breaking change. right? if so, can we ensure this is written so we can point users to the doc when anyone complains? Thanks for clearing this area.
|
This issue is marked with a breaking change tag. In theory it should be caught in any pre-release sweep we do to account for such changes in the docs. |
adamsitnik
left a comment
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.
LGTM 👍 ![]()
| Assert.Equal(10, span.LastIndexOf(value.AsSpan(), StringComparison.CurrentCulture)); | ||
| Assert.Equal(10, span.LastIndexOf(value.AsSpan(), StringComparison.CurrentCultureIgnoreCase)); | ||
| } | ||
| } |
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.
not related to your changes: IMHO this test (IndexOf_AllSubstrings) is very complex and is trying to test too many test cases at the same time. It would be better to split it into few smaller tests with self-describing names
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.
Agreed, it's a bit of a bear. Do you have recommendations for how it might naturally be split?
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.
I think that the two if blocks could become separate tests: value.Length == 0, s.Length == 0
Fixes #13382.
Fixes #13383.
string.LastIndexOfandMemoryExtensions.LastIndexOfare inconsistent with how they handle zero-length target strings with variousStringComparisonarguments. In some cases they returnstring.Length - 1. In some cases they returnspan.Length. And in some cases they return0.This PR normalizes the behavior of all of these APIs so that a zero-length target substring is always found at the end of the search space. Since
LastIndexOf's parameters vary significantly fromIndexOf's parameters, I also included a code comment right in front ofstring.IndexOf(string)that explains the concept of "search space" vis-à-vis this particular API.There's also some small comment cleanup throughout the code paths which deal with these checks.
This PR was extracted from #1514 into its own standalone review. This PR addresses only
LastIndexOfand doesn't address the zero-weight code point issue tracked by that PR.