Skip to content

Conversation

@gewarren
Copy link
Contributor

@gewarren gewarren commented Nov 7, 2020

@gewarren gewarren requested a review from a team as a code owner November 7, 2020 01:48
@dotnet-bot dotnet-bot added this to the November 2020 milestone Nov 7, 2020
Console.WriteLine(span.LastIndexOf("")); // prints '5' (correct)
```

In these examples, `5` is the correct answer because `"Hello".Substring(5)` and `"Hello".AsSpan().Slice(5)` both produce an empty string, which is trivially equal to the empty substring that is sought.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The API reference needs to reflect this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean by this comment? True, the sentence I included in the breaking change notification is a drastic simplification of how IndexOf and LastIndexOf work, and there are some counterexamples. But as a high-level overview it's a useful way to think about the API.

See https://github.com/dotnet/runtime/blob/a057f27e860c1a59aa12e7dd9be028fba82b037f/src/libraries/System.Private.CoreLib/src/System/String.Searching.cs#L223 if you're curious as to the real logic that underlies these APIs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GrabYourPitchforks I meant this, and this, etc..

Currently it says that the method returns the following:

The zero-based starting index position of value if that string is found, or -1 if it is not. If value is Empty, the return value is the last index position in this instance.

Note the bolded part isn't correct (similar wording is there for different overloads).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're referring to the remarks at https://docs.microsoft.com/dotnet/api/system.string.lastindexof. I don't think those files are in this repo, so they'd not be included in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, they belong to dotnet-api-docs repo.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll update the API docs now.

Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM @gewarren

You can decide if any changes are warranted from the conversations.

@gewarren gewarren merged commit dcdb77d into dotnet:master Nov 9, 2020
@gewarren gewarren deleted the lastindexof branch November 9, 2020 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Breaking change: Improved String.LastIndexOf handling of empty values

5 participants