11using System . Buffers ;
2+ using System . Collections ;
23using System . Diagnostics ;
34using System . Diagnostics . CodeAnalysis ;
45using System . Globalization ;
@@ -12,6 +13,7 @@ namespace DotNext.IO;
1213
1314using Buffers ;
1415using Buffers . Binary ;
16+ using Collections . Generic ;
1517using static Pipelines . PipeExtensions ;
1618using DecodingContext = Text . DecodingContext ;
1719
@@ -89,7 +91,7 @@ public T Read<T>()
8991 /// <typeparam name="T">The integer type.</typeparam>
9092 /// <returns>The integer value.</returns>
9193 /// <exception cref="EndOfStreamException">The underlying source doesn't contain necessary amount of bytes to decode the value.</exception>
92- public unsafe T ReadLittleEndian < T > ( )
94+ public T ReadLittleEndian < T > ( )
9395 where T : notnull , IBinaryInteger < T >
9496 {
9597 var type = typeof ( T ) ;
@@ -111,7 +113,7 @@ public unsafe T ReadLittleEndian<T>()
111113 /// <typeparam name="T">The integer type.</typeparam>
112114 /// <returns>The integer value.</returns>
113115 /// <exception cref="EndOfStreamException">The underlying source doesn't contain necessary amount of bytes to decode the value.</exception>
114- public unsafe T ReadBigEndian < T > ( )
116+ public T ReadBigEndian < T > ( )
115117 where T : notnull , IBinaryInteger < T >
116118 {
117119 var type = typeof ( T ) ;
@@ -215,9 +217,12 @@ public bool TryRead(int maxLength, out ReadOnlyMemory<byte> chunk)
215217 chunk = remaining . First . TrimLength ( maxLength ) ;
216218 position = remaining . GetPosition ( chunk . Length ) ;
217219 }
220+ else
221+ {
222+ chunk = default ;
223+ }
218224
219- chunk = default ;
220- return false ;
225+ return ! chunk . IsEmpty ;
221226 }
222227
223228 /// <summary>
@@ -379,18 +384,15 @@ private unsafe TResult Parse<TArg, TResult>(TArg arg, delegate*<ReadOnlySpan<byt
379384 if ( length is 0 )
380385 return parser ( [ ] , arg ) ;
381386
382- unsafe
387+ if ( length <= Parsing256Reader < IFormatProvider ? , TResult > . MaxSize )
383388 {
384- if ( length <= Parsing256Reader < IFormatProvider ? , TResult > . MaxSize )
385- {
386- var reader = new Parsing256Reader < TArg , TResult > ( arg , parser , length ) ;
387- return Read < TResult , Parsing256Reader < TArg , TResult > > ( ref reader ) ;
388- }
389- else
390- {
391- var reader = new ParsingReader < TArg , TResult > ( arg , parser , length ) ;
392- return Read < TResult , ParsingReader < TArg , TResult > > ( ref reader ) ;
393- }
389+ var reader = new Parsing256Reader < TArg , TResult > ( arg , parser , length ) ;
390+ return Read < TResult , Parsing256Reader < TArg , TResult > > ( ref reader ) ;
391+ }
392+ else
393+ {
394+ var reader = new ParsingReader < TArg , TResult > ( arg , parser , length ) ;
395+ return Read < TResult , ParsingReader < TArg , TResult > > ( ref reader ) ;
394396 }
395397 }
396398
@@ -744,8 +746,9 @@ ValueTask<MemoryOwner<char>> IAsyncBinaryReader.DecodeAsync(DecodingContext cont
744746 }
745747
746748 /// <inheritdoc/>
747- IAsyncEnumerable < ReadOnlyMemory < char > > IAsyncBinaryReader . DecodeAsync ( DecodingContext context , LengthFormat lengthFormat , Memory < char > buffer , CancellationToken token )
748- => Decode ( context , lengthFormat , buffer ) . AsAsyncEnumerable ( token ) ;
749+ IAsyncEnumerable < ReadOnlyMemory < char > > IAsyncBinaryReader . DecodeAsync ( DecodingContext context , LengthFormat lengthFormat , Memory < char > buffer ,
750+ CancellationToken token )
751+ => Decode ( context , lengthFormat , buffer ) ;
749752
750753 /// <inheritdoc/>
751754 ValueTask IAsyncBinaryReader . CopyToAsync ( Stream destination , long ? count , CancellationToken token )
@@ -839,7 +842,7 @@ readonly bool IAsyncBinaryReader.TryGetRemainingBytesCount(out long count)
839842 /// Represents decoding enumerable.
840843 /// </summary>
841844 [ StructLayout ( LayoutKind . Auto ) ]
842- public readonly struct DecodingEnumerable
845+ public readonly struct DecodingEnumerable : IEnumerable < ReadOnlyMemory < char > > , IAsyncEnumerable < ReadOnlyMemory < char > >
843846 {
844847 private readonly ReadOnlySequence < byte > bytes ;
845848 private readonly DecodingContext context ;
@@ -859,23 +862,24 @@ internal DecodingEnumerable(ReadOnlySequence<byte> bytes, in DecodingContext con
859862 /// </summary>
860863 /// <returns>The enumerator over decoded chunks of characters.</returns>
861864 public Enumerator GetEnumerator ( ) => new ( in bytes , in context , buffer ) ;
865+
866+ /// <inheritdoc />
867+ IEnumerator < ReadOnlyMemory < char > > IEnumerable < ReadOnlyMemory < char > > . GetEnumerator ( )
868+ => GetEnumerator ( ) . ToClassicEnumerator < Enumerator , ReadOnlyMemory < char > > ( ) ;
862869
863- #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
864- internal async IAsyncEnumerable < ReadOnlyMemory < char > > AsAsyncEnumerable ( [ EnumeratorCancellation ] CancellationToken token )
865- #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
866- {
867- foreach ( var chunk in this )
868- {
869- token . ThrowIfCancellationRequested ( ) ;
870- yield return chunk ;
871- }
872- }
870+ /// <inheritdoc />
871+ IEnumerator IEnumerable . GetEnumerator ( )
872+ => GetEnumerator ( ) . ToClassicEnumerator < Enumerator , ReadOnlyMemory < char > > ( ) ;
873+
874+ /// <inheritdoc />
875+ IAsyncEnumerator < ReadOnlyMemory < char > > IAsyncEnumerable < ReadOnlyMemory < char > > . GetAsyncEnumerator ( CancellationToken token )
876+ => GetEnumerator ( ) . ToAsyncEnumerator < Enumerator , ReadOnlyMemory < char > > ( token ) ;
873877
874878 /// <summary>
875879 /// Represents enumerator over decoded characters.
876880 /// </summary>
877881 [ StructLayout ( LayoutKind . Auto ) ]
878- public struct Enumerator
882+ public struct Enumerator : IEnumerator < Enumerator , ReadOnlyMemory < char > >
879883 {
880884 private readonly ReadOnlySequence < byte > bytes ;
881885 private readonly Decoder decoder ;
@@ -899,7 +903,7 @@ internal Enumerator(in ReadOnlySequence<byte> bytes, in DecodingContext context,
899903 /// <summary>
900904 /// Decodes the next chunk of bytes.
901905 /// </summary>
902- /// <returns><see langword="true"/> if decoding is successfull ; <see langword="false"/> if nothing to decode.</returns>
906+ /// <returns><see langword="true"/> if decoding is successful ; <see langword="false"/> if nothing to decode.</returns>
903907 public bool MoveNext ( )
904908 => ( charsWritten = GetChars ( in bytes , ref position , decoder , buffer . Span ) ) > 0 ;
905909 }
@@ -941,7 +945,7 @@ public ref struct Enumerator
941945 private readonly ReadOnlySequence < byte > bytes ;
942946 private readonly Decoder decoder ;
943947 private readonly Span < char > buffer ;
944- private ref SequencePosition position ;
948+ private readonly ref SequencePosition position ;
945949 private int charsWritten ;
946950
947951 internal Enumerator ( scoped in ReadOnlySequence < byte > bytes , ref SequencePosition position , scoped in DecodingContext context , Span < char > buffer )
@@ -960,7 +964,7 @@ internal Enumerator(scoped in ReadOnlySequence<byte> bytes, ref SequencePosition
960964 /// <summary>
961965 /// Decodes the next chunk of bytes.
962966 /// </summary>
963- /// <returns><see langword="true"/> if decoding is successfull ; <see langword="false"/> if nothing to decode.</returns>
967+ /// <returns><see langword="true"/> if decoding is successful ; <see langword="false"/> if nothing to decode.</returns>
964968 public bool MoveNext ( )
965969 => ( charsWritten = GetChars ( in bytes , ref position , decoder , buffer ) ) > 0 ;
966970 }
0 commit comments