dotnet/runtime

[API Proposal]: SequenceReader<T>.TryAdvanceTo(ReadOnlySpan<T> delimiter)

Open

#71.471 geöffnet am 30. Juni 2022

Auf GitHub ansehen
 (3 Kommentare) (8 Reaktionen) (0 zugewiesene Personen)C# (5.445 Forks)batch import
api-approvedarea-System.Buffershelp wantedin-pr

Repository-Metriken

Stars
 (17.886 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 12T 11h) (661 gemergte PRs in 30 T)

Beschreibung

Background and motivation

Today SequenceReader<T>.TryReadTo contains overloads with T to read to a single delimiter and ReadOnlySpan<T> to read to a sequence of delimiters, but SequenceReader<T>.TryAdvanceTo only contains overloads for single delimiter T, the proposal is to add a new overload to SequenceReader<T>.TryAdvanceTo that takes a ReadOnlySpan<T> which would advance the reader to the next occurrence of a delimiter sequence.

Main usage is to simplify the parsing of data formats with multi-byte delimiters. E.g., the PDF file format marks the start and end of a dictionary using << and >>, and surrounds the object with {id} 0 obj and endobj, this overload helps with parsing of such data.

#1111

API Proposal

namespace System.Buffers;

public ref struct SequenceReader<T>
{
    public bool TryAdvanceTo(ReadOnlySpan<T> delimiter, bool advancePastDelimiter = true);
}

API Usage

// Read data between << and >>
var sequenceReader = new SequenceReader(buffer);
sequenceReader.TryAdvanceTo("<<"u8);
sequenceReader.TryReadTo(out ReadOnlySpan<byte> span, ">>"u8);

Alternative Designs

No response

Risks

No response

Contributor Guide