Beispiel #1
0
        /// <summary>
        /// Resets the Buffer Writer, returning its buffers to the pool
        /// </summary>
        /// <remarks>The Buffer Writer can be reused after disposal</remarks>
        public void Reset(bool clearBuffers)
        {
            ReadOnlySequenceSegment <T>?Segment = _HeadSegment;

            while (Segment != null)
            {
                if (MemoryMarshal.TryGetArray(Segment.Memory, out var MyBuffer))
                {
                    _Pool.Return(MyBuffer.Array !, clearBuffers);
                }

                Segment = Segment.Next;
            }

            if (!_CurrentBuffer.IsEmpty)
            {
                if (MemoryMarshal.TryGetArray <T>(_CurrentBuffer, out var MyBuffer))
                {
                    _Pool.Return(MyBuffer.Array !, clearBuffers);
                }
            }

            _CurrentBuffer = Memory <T> .Empty;
            _CurrentOffset = 0;
            _TailSegment   = _HeadSegment = null;
        }
Beispiel #2
0
        public ReadWriteBytes(ReadOnlySequenceSegment <byte> first, ReadOnlySequenceSegment <byte> last)
        {
            _start      = first;
            _startIndex = 0;
            _end        = last;
            _endIndex   = last.Memory.Length;

            Validate();
        }
Beispiel #3
0
        /// <summary>
        /// Creates an instance of <see cref="ReadOnlySequence{T}"/> from the <see cref="ReadOnlyMemory{T}"/>.
        /// Consumer is expected to manage lifetime of memory until <see cref="ReadOnlySequence{T}"/> is not used anymore.
        /// </summary>
        public ReadOnlySequence(ReadOnlyMemory <T> readOnlyMemory)
        {
            ReadOnlySequenceSegment segment = new ReadOnlySequenceSegment
            {
                Memory = MemoryMarshal.AsMemory(readOnlyMemory)
            };

            _sequenceStart = new SequencePosition(segment, 0 | MemoryListStartMask);
            _sequenceEnd   = new SequencePosition(segment, readOnlyMemory.Length | MemoryListEndMask);
        }
 public static SequencePosition?PositionOf(this ReadOnlySequenceSegment <byte> list, byte value)
 {
     while (list != null)
     {
         var current = list.Memory.Span;
         var index   = current.IndexOf(value);
         if (index != -1)
         {
             return(new SequencePosition(list, index));
         }
         list = list.Next;
     }
     return(null);
 }
Beispiel #5
0
        static internal void GetFirstSpan <T>(this ReadOnlySequence <T> sequence, out ReadOnlySpan <T> first, out SequencePosition next)
        {
            first = default;
            next  = default;
            SequencePosition start = sequence.Start;
            int    startIndex      = start.GetInteger();
            object startObject     = start.GetObject();

            if (startObject != null)
            {
                SequencePosition end     = sequence.End;
                int  endIndex            = end.GetInteger();
                bool hasMultipleSegments = startObject != end.GetObject();

                if (startIndex >= 0)
                {
                    if (endIndex >= 0)
                    {
                        // Positive start and end index == ReadOnlySequenceSegment<T>
                        ReadOnlySequenceSegment <T> segment = (ReadOnlySequenceSegment <T>)startObject;
                        next  = new SequencePosition(segment.Next, 0);
                        first = segment.Memory.Span;
                        if (hasMultipleSegments)
                        {
                            first = first.Slice(startIndex);
                        }
                        else
                        {
                            first = first.Slice(startIndex, endIndex - startIndex);
                        }
                    }
                    else
                    {
                        // Positive start and negative end index == T[]
                        if (hasMultipleSegments)
                        {
                            ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached();
                        }

                        first = new ReadOnlySpan <T>((T[])startObject, startIndex, (endIndex & ReadOnlySequence.IndexBitMask) - startIndex);
                    }
                }
                else
                {
                    first = GetFirstSpanSlow <T>(startObject, startIndex, endIndex, hasMultipleSegments);
                }
            }
        }
        public static long IndexOf(this ReadOnlySequenceSegment <byte> list, ReadOnlySpan <byte> value)
        {
            var first = list.Memory.Span;
            var index = first.IndexOf(value);

            if (index != -1)
            {
                return(index);
            }

            var rest = list.Next;

            if (rest == null)
            {
                return(-1);
            }

            return(IndexOfStraddling(first, list.Next, value));
        }
Beispiel #7
0
            public override void CopyTo(Span <T> destination)
            {
                var dest = destination;
                ReadOnlySequenceSegment <T> segment = _start;

                while (segment.Next != null)
                {
                    var memory = segment.Memory;
                    memory.Span.CopyTo(dest);
                    dest    = dest.Slice(memory.Length);
                    segment = segment.Next;
                }
                var temp = _array.Length - _available;

                Debug.Assert(dest.Length == temp);
                if (temp > 0)
                {
                    _array.AsSpan(0, temp).CopyTo(dest);
                }
            }
        public static int CopyTo(this ReadOnlySequenceSegment <byte> list, Span <byte> destination)
        {
            var current = list.Memory.Span;
            int copied  = 0;

            while (destination.Length > 0)
            {
                if (current.Length >= destination.Length)
                {
                    current.Slice(0, destination.Length).CopyTo(destination);
                    copied += destination.Length;
                    return(copied);
                }
                else
                {
                    current.CopyTo(destination);
                    copied     += current.Length;
                    destination = destination.Slice(current.Length);
                }
            }
            return(copied);
        }
Beispiel #9
0
 public void SetNext(ReadOnlySequenceSegment <T> next) => Next  = next;