Skip to content
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

[API Proposal]: ReadOnlySpan<char>.StartsWithAny(SearchValues<string>) and ReadOnlySpan<char>.EndsWithAny(SearchValues<string>) #110115

Open
silkfire opened this issue Nov 24, 2024 · 5 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory untriaged New issue has not been triaged by the area owner

Comments

@silkfire
Copy link

silkfire commented Nov 24, 2024

Background and motivation

There currently doesn't seem to be a means of checking whether the beginning or the end of a string equals to any of the values contained in a specified set of values in an optimized way using SearchValues<string> that was introduced in .NET 9. There are other specialized extension APIs involving ReadOnlySpan<char> and SearchValues<string> that were recently added (e.g. IndexOfAny<T>(ReadOnlySpan<char>, SearchValues<string>), ContainsAny(ReadOnlySpan<char>, SearchValues<string>) et al.) but this particular API seems to be missing.

One use case could be if you need to efficiently check whether a string starts with any one of a particular set of prefixes in order to determine whether your application code supports or handles that particular item.

See also #94155.

API Proposal

namespace System;

public static class MemoryExtensions
{
    // Proposed
    public static bool StartsWithAny(this System.ReadOnlySpan<char> span, System.Buffers.SearchValues<string> values);
    public static bool EndsWithAny(this System.ReadOnlySpan<char> span, System.Buffers.SearchValues<string> values);
}

API Usage

var searchValues = SearchValues.Create(new[] { "P1", A99", "B44" }.AsSpan(), StringComparison.Ordinal);

var isSupported = "testValue".StartsWithAny(searchValues);
var isSupported2 = "testValue".EndsWithAny(searchValues);

Alternative Designs

N/A

Risks

N/A

@silkfire silkfire added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 24, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label Nov 24, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 24, 2024
@huoyaoyuan huoyaoyuan added area-System.Memory and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Nov 24, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-memory
See info in area-owners.md if you want to be subscribed.

@silkfire silkfire changed the title [API Proposal]: ReadOnlySpan<char>.StartsWithAny and ReadOnlySpan<char>.EndsWithAny with SearchValues<string> [API Proposal]: ReadOnlySpan<char>.StartsWithAny(SearchValues<string>) and ReadOnlySpan<char>.EndsWithAny(SearchValues<string>) Nov 24, 2024
@MihaZupan
Copy link
Member

What sort of use cases do you have for EndsWithAny (note that we also didn't expose a LastIndexOfAny for SearchValues<string>)?

@silkfire
Copy link
Author

Good question; I actually wasn't aware of that. I assume StartsWithAny would be more common (and therefore more useful) than EndsWithAny so including the latter one would only be to make the API complete.

@jkoritzinsky
Copy link
Member

Re StartsWithAny, I have a use case in #104999 where I'd love to use it.

I don't currently have a use case for EndsWithAny though.

@MihaZupan
Copy link
Member

As a workaround, you can use text.IndexOfAny(values) == 0, which would give you the same answer.
Depending on your inputs, that could be more expensive than intended though (long haystack with no matches).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Memory untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

4 participants