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]: Create an AsnReader from an AsnReader #86723
Comments
Tagging subscribers to this area: @dotnet/area-system-formats-asn1, @bartonjs, @vcsjones Issue DetailsBackground and motivationThe It would be nice if there was a way to copy a reader with its current state. The Peek methods don't work particularly well for this scenario, since that only gives you the data for the next encoded value, not any trailing data. I would propose a constructor on AsnReader that takes an AsnReader, and duplicates the state. API Proposalnamespace System.Formats.Asn1;
public partial class AsnReader
{
public AsnReader(AsnReader reader);
} API Usagepublic void ProcessData(AsnReader reader) {
AsnReader clone = new AsnReader(reader);
while (clone.HasData) {
ReadOnlyMemory<byte> value = clone.ReadEncodedValue();
// Do something with the value
}
// The reader parameter has not been advanced
} Alternative DesignsThis could be a RisksNo response
|
Seems reasonable to me. I've probably done it with the internal AsnValueReader by just saving a copy. Structs are nice for copying (but terrible for side-effecting) :) |
Creating a copy of a public class AsnReader
{
public int Position { get; set; }
} But this would require non trivial changes to the type itself (it's based on slicing as far as I can see). So the proposal LGTM ;) |
One reason I like the current design, and not a position, is a Consider this ASN.1, in BER: This is a SEQUENCE that contains a REAL with trailing data. If I start reading it at Position 1, now all of the sudden it becomes an ASN.1 NULL (a weird one). The current design really doesn't let you do that, which I think is a good characteristic. |
👍 |
namespace System.Formats.Asn1;
public partial class AsnReader
{
public AsnReader Clone();
} |
Background and motivation
The
AsnReader
is great for forward-only reading, but sometimes you want to use it, but not consume it; or you want to be able to put it back to how it was before you read through it.It would be nice if there was a way to copy a reader with its current state. The Peek methods don't work particularly well for this scenario, since that only gives you the data for the next encoded value, not any trailing data.
I would propose a constructor on AsnReader that takes an AsnReader, and duplicates the state.
API Proposal
API Usage
Alternative Designs
This could be a
Copy
orClone
instance method, but my preference leans toward a new constructor.Risks
No response
The text was updated successfully, but these errors were encountered: