internal ReadOnlyCharSegment Append(ReadOnlyMemory <char> allocation, int bytesUsed)
        {
            var ret =
                new ReadOnlyCharSegment(allocation, bytesUsed)
            {
                RunningIndex = RunningIndex + Memory.Length
            };

            Next = ret;

            return(ret);
        }
        internal bool MakeSequence(ref ReadOnlySequence <char> nonEmpty)
        {
            AssertNotDisposedInternal(this);

            // nothing written
            if (!HasNodes || Head.BytesUsed == 0)
            {
                return(false);
            }

            // single segment case
            if (IsSingleSegment)
            {
                var usedPartOfHead = Head.Allocation.Slice(0, Head.BytesUsed);
                nonEmpty = new ReadOnlySequence <char>(usedPartOfHead);
                return(true);
            }

            // multi segment series
            //   we need this mapping because the
            //   Node representation isn't "finished"
            //   and still has extra space floating around
            //   between each node
            var headSeg = new ReadOnlyCharSegment(Head.Allocation, Head.BytesUsed);
            var tailSeg = headSeg;

            if (Head.HasNext)
            {
                var n = Head.Next;
                while (true)
                {
                    tailSeg = tailSeg.Append(n.Allocation, n.BytesUsed);
                    if (n.HasNext)
                    {
                        n = n.Next;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            var startIx = 0;

            var endIx = Tail.BytesUsed;

            nonEmpty = new ReadOnlySequence <char>(headSeg, startIx, tailSeg, endIx);
            return(true);
        }