-
Notifications
You must be signed in to change notification settings - Fork 119
/
IBufferReader.cs
277 lines (218 loc) · 8.33 KB
/
IBufferReader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace DotNext.Buffers;
using Binary;
using Numerics;
using DecodingContext = DotNext.Text.DecodingContext;
/// <summary>
/// Represents buffer reader.
/// </summary>
public interface IBufferReader : IReadOnlySpanConsumer<byte>
{
/// <summary>
/// The expected number of bytes to be consumed this reader.
/// </summary>
int RemainingBytes { get; }
/// <summary>
/// Gets a value indicating that reader doesn't support decoding of partial data.
/// </summary>
static virtual bool ThrowOnPartialData => true;
}
[StructLayout(LayoutKind.Auto)]
internal struct MemoryBlockReader(Memory<byte> destination) : IBufferReader
{
readonly int IBufferReader.RemainingBytes => destination.Length;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
source.CopyTo(destination.Span, out var count);
destination = destination.Slice(count);
}
}
[StructLayout(LayoutKind.Auto)]
internal struct MemoryReader(Memory<byte> destination) : IBufferReader, ISupplier<int>
{
private int bytesWritten;
internal readonly int BytesWritten => bytesWritten;
readonly int IBufferReader.RemainingBytes => destination.Length;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
source.CopyTo(destination.Span, out var count);
bytesWritten += count;
destination = default;
}
readonly int ISupplier<int>.Invoke() => BytesWritten;
static bool IBufferReader.ThrowOnPartialData => false;
}
[StructLayout(LayoutKind.Auto)]
internal unsafe struct WellKnownIntegerReader<T>(delegate*<ReadOnlySpan<byte>, bool, T> parser) : IBufferReader, ISupplier<T>
where T : notnull, IBinaryInteger<T>
{
private T? buffer;
private int writtenBytes;
private Span<byte> Buffer => MemoryMarshal.CreateSpan(ref Unsafe.As<T?, byte>(ref buffer), Unsafe.SizeOf<T>());
readonly int IBufferReader.RemainingBytes => Unsafe.SizeOf<T>() - writtenBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
source.CopyTo(Buffer.Slice(writtenBytes), out var count);
writtenBytes += count;
}
T ISupplier<T>.Invoke() => parser(Buffer, Number.IsSigned<T>() is false);
internal static WellKnownIntegerReader<T> LittleEndian() => new(&T.ReadLittleEndian);
internal static WellKnownIntegerReader<T> BigEndian() => new(&T.ReadBigEndian);
}
[StructLayout(LayoutKind.Auto)]
internal unsafe struct IntegerReader<T>(delegate*<ReadOnlySpan<byte>, bool, T> parser) : IBufferReader, ISupplier<T>
where T : notnull, IBinaryInteger<T>
{
private MemoryOwner<byte> buffer = Memory.AllocateExactly<byte>(Number.GetMaxByteCount<T>());
private int writtenBytes;
readonly int IBufferReader.RemainingBytes => buffer.Length - writtenBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
source.CopyTo(buffer.Span.Slice(writtenBytes), out var count);
writtenBytes += count;
}
T ISupplier<T>.Invoke()
{
try
{
return parser(buffer.Span.Slice(0, writtenBytes), Number.IsSigned<T>() is false);
}
finally
{
buffer.Dispose();
}
}
internal static IntegerReader<T> LittleEndian() => new(&T.ReadLittleEndian);
internal static IntegerReader<T> BigEndian() => new(&T.ReadBigEndian);
}
[StructLayout(LayoutKind.Auto)]
internal struct BinaryFormattable256Reader<T> : IBufferReader, ISupplier<T>
where T : notnull, IBinaryFormattable<T>
{
private Buffer256 buffer;
private int writtenBytes;
readonly int IBufferReader.RemainingBytes => T.Size - writtenBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
Span<byte> destination = buffer;
source.CopyTo(destination.Slice(writtenBytes), out var count);
writtenBytes += count;
}
readonly T ISupplier<T>.Invoke()
{
ReadOnlySpan<byte> source = buffer;
return T.Parse(source.Slice(0, writtenBytes));
}
internal static int MaxSize => Unsafe.SizeOf<Buffer256>();
}
[StructLayout(LayoutKind.Auto)]
internal struct BinaryFormattableReader<T>() : IBufferReader, ISupplier<T>
where T : notnull, IBinaryFormattable<T>
{
private MemoryOwner<byte> buffer = Memory.AllocateExactly<byte>(T.Size);
private int writtenBytes;
readonly int IBufferReader.RemainingBytes => T.Size - writtenBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
source.CopyTo(buffer.Span.Slice(writtenBytes), out var count);
writtenBytes += count;
}
T ISupplier<T>.Invoke()
{
try
{
return T.Parse(buffer.Span.Slice(0, writtenBytes));
}
finally
{
buffer.Dispose();
}
}
}
[StructLayout(LayoutKind.Auto)]
internal struct CharBufferDecodingReader(in DecodingContext context, int length, Memory<char> buffer) : IBufferReader, ISupplier<int>
{
private readonly Decoder decoder = context.GetDecoder();
private int remainingBytes = length, writtenChars;
readonly int ISupplier<int>.Invoke() => writtenChars;
readonly int IBufferReader.RemainingBytes => remainingBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> bytes)
{
remainingBytes -= bytes.Length;
writtenChars += decoder.GetChars(bytes, buffer.Span.Slice(writtenChars), remainingBytes is 0);
}
}
[StructLayout(LayoutKind.Auto)]
internal struct DecodingReader(Decoder decoder, int length, Memory<char> buffer) : IBufferReader, ISupplier<int>
{
private int writtenChars;
public readonly int RemainingBytes => Math.Min(length, buffer.Length);
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
writtenChars = decoder.GetChars(source, buffer.Span, length <= source.Length);
length = 0;
}
readonly int ISupplier<int>.Invoke() => writtenChars;
}
[StructLayout(LayoutKind.Auto)]
internal unsafe struct Parsing256Reader<TArg, TResult>(TArg arg, delegate*<ReadOnlySpan<byte>, TArg, TResult> parser, int length) : IBufferReader, ISupplier<TResult>
{
private Buffer256 buffer;
private int consumedBytes;
readonly int IBufferReader.RemainingBytes => length - consumedBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
Span<byte> destination = buffer;
source.CopyTo(destination.Slice(consumedBytes), out var count);
consumedBytes += count;
}
readonly TResult ISupplier<TResult>.Invoke()
{
ReadOnlySpan<byte> source = buffer;
return parser(source.Slice(0, consumedBytes), arg);
}
internal static int MaxSize => Unsafe.SizeOf<Buffer256>();
}
[StructLayout(LayoutKind.Auto)]
internal unsafe struct ParsingReader<TArg, TResult>(TArg arg, delegate*<ReadOnlySpan<byte>, TArg, TResult> parser, int length) : IBufferReader, ISupplier<TResult>
{
private MemoryOwner<byte> buffer = Memory.AllocateExactly<byte>(length);
private int consumedBytes;
readonly int IBufferReader.RemainingBytes => buffer.Length - consumedBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
{
source.CopyTo(buffer.Span.Slice(consumedBytes), out var count);
consumedBytes += count;
}
TResult ISupplier<TResult>.Invoke()
{
try
{
return parser(buffer.Span.Slice(0, consumedBytes), arg);
}
finally
{
buffer.Dispose();
}
}
}
[StructLayout(LayoutKind.Auto)]
internal struct SkippingReader(long length) : IBufferReader
{
readonly int IBufferReader.RemainingBytes => int.CreateSaturating(length);
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
=> length -= source.Length;
}
internal struct BufferReader<TReader>(TReader reader) : IBufferReader, ISupplier<TReader>
where TReader : struct, IBufferReader
{
int IBufferReader.RemainingBytes => reader.RemainingBytes;
void IReadOnlySpanConsumer<byte>.Invoke(ReadOnlySpan<byte> source)
=> reader.Invoke(source);
static bool IBufferReader.ThrowOnPartialData => TReader.ThrowOnPartialData;
readonly TReader ISupplier<TReader>.Invoke() => reader;
public static implicit operator BufferReader<TReader>(TReader reader) => new(reader);
}