Skip to content

[API Proposal]: SequenceReaderExtensions.{TryPeekLittleEndian, TryPeekBigEndian} extensions #116414

Closed

Description

@NinoFloris

Background and motivation

I have needed to *peek* numbers a few times in differing endianness on a SequenceReader. Currently only TryReadLittleEndian and TryReadBigEndian are defined. This proposal aims to add their peeking counterparts.

I've had these extension methods defined privately for quite some time now; they're just copies of the TryRead implementations without Advance being called.
https://gist.github.com/NinoFloris/616170c620607d302d9e7626f9195a33

As there are a few other SequenceReader API proposals awaiting review it seems timely and efficient to propose these apis to be moved into the box as well.

API Proposal

namespace System.Buffers;

public partial static class SequenceReaderExtensions
{
    public static bool TryPeekLittleEndian(ref this SequenceReader<byte> reader, out short value);
    public static bool TryPeekLittleEndian(ref this SequenceReader<byte> reader, out int value);
    public static bool TryPeekLittleEndian(ref this SequenceReader<byte> reader, out long value);

    public static bool TryPeekBigEndian(ref this SequenceReader<byte> reader, out short value);
    public static bool TryPeekBigEndian(ref this SequenceReader<byte> reader, out int value);
    public static bool TryPeekBigEndian(ref this SequenceReader<byte> reader, out long value);
}

API Usage

var reader = new SequenceReader(...);

if (reader.TryPeekBigEndian(out var length)) // network data
{
    // Leave the reader at the error position.
    if (length < -1)
        throw new InvalidOperationException("Unexpected length");

    // Sentinel value does not need to go through full value reading, e.g. it represents absent value.
    if (length is -1)
    {
        reader.Advance(sizeof(int));
        return default(T);
    }

    // ReadValue assumes it can read its own length again.
    return ReadValue(reader);
}

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on Jun 8, 2025
  2. added
    untriaged
    on Jun 8, 2025
  3. dotnet-policy-service commented on Jun 8, 2025

    @dotnet-policy-service
    Contributor

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

  4. NinoFloris commented on Jul 1, 2025

    @NinoFloris
    ContributorAuthor

    I believe these are reasonable api additions, could somebody take a look? @jeffhandley maybe?

  5. jeffhandley commented on Jul 23, 2025

    @jeffhandley
    Member

    Seems OK to me. @tannergooding, any objections on this or is this worthy of marking as api-ready-for-review API is ready for review, it is NOT ready for implementation and help wanted [up-for-grabs] Good issue for external contributors ?

  6. removed
    untriaged
    on Jul 23, 2025
  7. added this to the Future milestone on Jul 23, 2025
  8. NinoFloris commented on Dec 29, 2025

    @NinoFloris
    ContributorAuthor

    Early 11.0 ping @tannergooding.

  9. added
    api-ready-for-review
    and removed
    api-suggestion
    on Apr 23, 2026
  10. modified the milestones: Future, 11.0.0 on Apr 23, 2026
  11. tannergooding commented on Apr 23, 2026

    @tannergooding
    Member

    This may warrant also having the unsigned variants.

  12. bartonjs commented on May 12, 2026

    @bartonjs
    Member

    Video

    Looks good as proposed.

    If unsigned variants, or offset variants, are needed later, they can come as a separate proposal.

    namespace System.Buffers;
    
    public partial static class SequenceReaderExtensions
    {
        public static bool TryPeekLittleEndian(ref this SequenceReader<byte> reader, out short value);
        public static bool TryPeekLittleEndian(ref this SequenceReader<byte> reader, out int value);
        public static bool TryPeekLittleEndian(ref this SequenceReader<byte> reader, out long value);
    
        public static bool TryPeekBigEndian(ref this SequenceReader<byte> reader, out short value);
        public static bool TryPeekBigEndian(ref this SequenceReader<byte> reader, out int value);
        public static bool TryPeekBigEndian(ref this SequenceReader<byte> reader, out long value);
    }
  13. added
    api-approved
    and removed
    api-ready-for-review
    on May 12, 2026
  14. lilinus commented on Jul 1, 2026

    @lilinus
    Contributor

    Hasn't been any activity on this one. I can implement it for .NET 11 unless there are objections?

  15. jeffhandley commented on Aug 17, 2026

    @jeffhandley
    Member

    @tannergooding assigning to you to drive to closure. I'm supportive of seeking Tactics approval of backporting this into RC1 in the next couple days or RC2.

  16. added a commit that references this issue on Aug 20, 2026
    fd0435a
  17. locked and limited conversation to collaborators on Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api-approvedarea-System.Buffers

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions