Return the matching search value in a multi-string search (i.e. IndexOfAny with SearchValues<string>)

Open
#110,670 12 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Quiet
Tech stack
csharp

Research direction

Start by reading the SearchValues and IndexOfAny API discussion linked from the issue, then compare the proposed return forms with the existing Regex alternative. Determine whether exposing the matching search value is a suitable API change and define how callers would identify the match. Done means reaching agreement on the API shape and its expected behavior.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Memory needs-further-triage
Discussed in https://github.com/dotnet/runtime/discussions/110663

Originally posted by f2bo December 12, 2024
.NET 9 introduced multi-string search with IndexOfAny(this ReadOnlySpan<char> span, SearchValues<string> values), which is great if you need to find a set of matching strings. However, and I would say more often than not, in addition to the location, you also need to know which of the search values was found, for example, to count instances for each of the search values or even just to advance a pointer past the match and continue processing.

Until now, when searching for single characters, this was trivial since the matching search value had length 1 and could be directly retrieved from the location returned by IndexOfAny. When searching for strings, however, it now becomes necessary to execute an additional comparison at the match location to determine which of the search values was found.

I'm aware that you could get this information by using a Regex instead, but it seems odd given that IndexOfAny has already determined the matching search value. Wouldn't it be possible to also return it, even if it's in the form of an index into the ReadOnlySpan<string> that was originally used to create the SearchValues instance. This could be provided either as an additional output parameter or by returning a tuple that also includes the location.

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.