Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[API Proposal]: Add SearchValues Overloads to SequenceReader #114451

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

Open
grbell-ms opened this issue Apr 9, 2025 · 3 comments
Open

[API Proposal]: Add SearchValues Overloads to SequenceReader #114451

grbell-ms opened this issue Apr 9, 2025 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Buffers untriaged New issue has not been triaged by the area owner

Comments

@grbell-ms
Copy link
Member

grbell-ms commented Apr 9, 2025

Background and motivation

SequenceReader has methods that take a ReadOnlySpan of delimiter values and advances/reads until finding one of those values. With SearchValues addition in .NET 8, a new set of overloads is a natural addition. They should be more performant than using the existing overloads while being no more complicated for developers to use (and even easier than using AdvancePastAny(T value0, T value1, T value2, T value3)).

API Proposal

namespace System.Buffers;

public ref struct SequenceReader<T>
{
    public bool TryAdvanceToAny(SearchValues<T> delimiters, bool advancePastDelimiter = true);
    public long AdvancePastAny(SearchValues<T> values);
    public bool TryReadToAny(out ReadOnlySequence<T> sequence, SearchValues<T> delimiters, bool advancePastDelimiter = true);
    public bool TryReadToAny(out ReadOnlySpan<T> span, SearchValues<T> delimiters, bool advancePastDelimiter = true);
}

API Usage

var newlines = SearchValues.Create("\r\n");
var sequenceReader = new SequenceReader(buffer);
sequenceReader.TryReadToAny(out ReadOnlySequence<T> line, newlines, false);
sequenceReader.AdvancePastAny(newlines);

Alternative Designs

We could also add extension methods for SequenceReader<char> and SearchValues<string>.

namespace System.Buffers;

public static class SequenceReaderExtensions
{
    public static bool TryAdvanceToAny(this SequenceReader<char> reader, SearchValues<string> delimiters, bool advancePastDelimiter = true);
    public static long AdvancePastAny(this SequenceReader<char> reader, SearchValues<string> values);
    public static bool TryReadToAny(this SequenceReader<char> reader, out ReadOnlySequence<char> sequence, SearchValues<string> delimiters, bool advancePastDelimiter = true);
    public static bool TryReadToAny(this SequenceReader<char> reader, out ReadOnlySpan<char> span, SearchValues<string> delimiters, bool advancePastDelimiter = true);
}

Risks

No response

@grbell-ms grbell-ms added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Apr 9, 2025
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Apr 9, 2025
Copy link
Contributor

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

@grbell-ms
Copy link
Member Author

Added the possibility of extension methods for SequenceReader<char> and SearchValues<string>.

@MihaZupan
Copy link
Member

The generic overloads sound reasonable.

I would hold off on SearchValues<string> overloads until there's a demonstrated need here since you can't implement those efficiently without support from SearchValues internals (can't search across multiple segments without copying the whole sequence into a single buffer).

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.Buffers untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

2 participants