Пример #1
0
        public static bool TryReadUntill <TSequence>(ref BufferReader <TSequence> reader, out ReadOnlyBuffer <byte> bytes, byte delimiter)
            where TSequence : ISequence <ReadOnlyMemory <byte> >, ISlicable
        {
            var copy  = reader;
            var start = reader.Position;

            while (!reader.End)
            {
                SequencePosition end = reader.Position;
                if (reader.Read() == delimiter)
                {
                    bytes = reader.Sequence.Slice(start, end);
                    return(true);
                }
            }
            reader = copy;
            bytes  = default;
            return(false);
        }
Пример #2
0
        public static bool TryReadUntill <TSequence>(ref BufferReader <TSequence> reader, out ReadOnlyBuffer <byte> bytes, ReadOnlySpan <byte> delimiter)
            where TSequence : ISequence <ReadOnlyMemory <byte> >, ISlicable
        {
            if (delimiter.Length == 0)
            {
                bytes = default;
                return(true);
            }

            int matched = 0;
            var copy    = reader;
            var start   = reader.Position;
            var end     = reader.Position;

            while (!reader.End)
            {
                if (reader.Read() == delimiter[matched])
                {
                    matched++;
                }
                else
                {
                    end     = reader.Position;
                    matched = 0;
                }
                if (matched >= delimiter.Length)
                {
                    bytes = reader.Sequence.Slice(start, end);
                    return(true);
                }
            }
            reader = copy;
            bytes  = default;
            return(false);
        }
Пример #3
0
 /// <summary>
 ///
 /// </summary>
 public Enumerator(ReadOnlyBuffer <T> readOnlyBuffer)
 {
     _readOnlyBuffer = readOnlyBuffer;
     _currentMemory  = default;
     _next           = readOnlyBuffer.Start;
 }
Пример #4
0
 public bool Equals(ReadOnlyBuffer <T> other)
 {
     return(other.Equals(this));
 }
Пример #5
0
 public ReadOnlyBytes(ReadOnlyBuffer <byte> buffer) :
     this(buffer, null, buffer.Length)
 {
 }
Пример #6
0
 public ReadOnlyBytes(ReadOnlyBuffer <byte> first, IReadOnlyBufferList <byte> rest) :
     this(first, rest, rest == null?first.Length:Unspecified)
 {
 }
Пример #7
0
        public static void Pipe(this IBufferOperation transformation, ReadOnlyBuffer <byte> source, IOutput destination)
        {
            int afterMergeSlice = 0;

            // Assign 'remainder' to something formally stack-referring.
            // The default classification is "returnable, not referring to stack", we want the opposite in this case.
            ReadOnlySpan <byte> remainder = stackalloc byte[0];
            Span <byte>         stackSpan = stackalloc byte[stackLength];

            Position poisition = default;

            while (source.TryGet(ref poisition, out var sourceBuffer))
            {
                Span <byte>         outputSpan = destination.GetSpan();
                ReadOnlySpan <byte> sourceSpan = sourceBuffer.Span;

                if (!remainder.IsEmpty)
                {
                    int leftOverBytes = remainder.Length;
                    remainder.CopyTo(stackSpan);
                    int amountToCopy = Math.Min(sourceSpan.Length, stackSpan.Length - leftOverBytes);
                    sourceSpan.Slice(0, amountToCopy).CopyTo(stackSpan.Slice(leftOverBytes));
                    int amountOfData = leftOverBytes + amountToCopy;

                    Span <byte> spanToTransform = stackSpan.Slice(0, amountOfData);

TryTransformWithRemainder:
                    OperationStatus status = transformation.Execute(spanToTransform, outputSpan, out int bytesConsumed, out int bytesWritten);
                    if (status != OperationStatus.Done)
                    {
                        destination.Advance(bytesWritten);
                        spanToTransform = spanToTransform.Slice(bytesConsumed);

                        if (status == OperationStatus.DestinationTooSmall)
                        {
                            outputSpan = destination.GetSpan();

                            if (outputSpan.Length - bytesWritten < 3)
                            {
                                return; // no more output space, user decides what to do.
                            }
                            goto TryTransformWithRemainder;
                        }
                        else
                        {
                            if (status == OperationStatus.InvalidData)
                            {
                                continue; // source buffer contains invalid bytes, user decides what to do for fallback
                            }

                            // at this point, status = TransformationStatus.NeedMoreSourceData
                            // left over bytes in stack span
                            remainder = spanToTransform;
                        }
                        continue;
                    }
                    else    // success
                    {
                        afterMergeSlice = bytesConsumed - remainder.Length;
                        remainder       = Span <byte> .Empty;
                        destination.Advance(bytesWritten);
                        outputSpan = destination.GetSpan();
                    }
                }

TryTransform:
                OperationStatus result = transformation.Execute(sourceSpan.Slice(afterMergeSlice), outputSpan, out int consumed, out int written);
                afterMergeSlice        = 0;
                destination.Advance(written);
                sourceSpan = sourceSpan.Slice(consumed);

                if (result == OperationStatus.Done)
                {
                    continue;
                }

                // Not successful
                if (result == OperationStatus.DestinationTooSmall)
                {
                    destination.GetMemory();  // output buffer is too small
                    outputSpan = destination.GetSpan();
                    if (outputSpan.Length - written < 3)
                    {
                        return; // no more output space, user decides what to do.
                    }
                    goto TryTransform;
                }
                else
                {
                    if (result == OperationStatus.InvalidData)
                    {
                        continue; // source buffer contains invalid bytes, user decides what to do for fallback
                    }

                    // at this point, result = TransformationStatus.NeedMoreSourceData
                    // left over bytes in source span
                    remainder = sourceSpan;
                }
            }
            return;
        }
Пример #8
0
 public BytesReader(ReadOnlyBuffer <byte> bytes) : this(bytes, SymbolTable.InvariantUtf8)
 {
 }
Пример #9
0
 public BytesReader(ReadOnlyBuffer <byte> bytes) : this(bytes, TextEncoder.Utf8)
 {
 }