public IAppendable Append(ICharSequence sequence, int start, int end)
        {
            Contract.Requires(sequence.Count >= end);

            int length = end - start;

            if (length > this.chars.Length - this.pos)
            {
                this.chars = Expand(this.chars, this.pos + length, this.pos);
            }

            if (sequence is AppendableCharSequence seq)
            {
                // Optimize append operations via array copy
                byte[] src = seq.chars;
                PlatformDependent.CopyMemory(src, start, this.chars, this.pos, length);
                this.pos += length;

                return(this);
            }

            for (int i = start; i < end; i++)
            {
                this.chars[this.pos++] = (byte)sequence[i];
            }

            return(this);
        }
        public ICharSequence SubSequence(int start, int end)
        {
            int length = end - start;
            var data   = new byte[length];

            PlatformDependent.CopyMemory(this.chars, start, data, 0, length);
            return(new AppendableCharSequence(data));
        }
        public unsafe AsciiString SubStringUnsafe(int start, int end)
        {
            var bytes = new byte[end - start];

            fixed(byte *src = &this.chars[start])
            fixed(byte *dst = bytes)
            {
                PlatformDependent.CopyMemory(src, dst, bytes.Length);
            }
            return(new AsciiString(bytes));
        }
 public IAppendable Append(byte c)
 {
     if (this.pos == this.chars.Length)
     {
         byte[] old = this.chars;
         this.chars = new byte[old.Length << 1];
         PlatformDependent.CopyMemory(old, 0, this.chars, 0, old.Length);
     }
     this.chars[this.pos++] = c;
     return(this);
 }
        public bool Equals(AppendableCharSequence other)
        {
            if (other == null)
            {
                return(false);
            }
            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            return(this.pos == other.pos &&
                   PlatformDependent.ByteArrayEquals(this.chars, 0, other.chars, 0, this.pos));
        }
        static byte[] Expand(byte[] array, int neededSpace, int size)
        {
            int newCapacity = array.Length;

            do
            {
                // double capacity until it is big enough
                newCapacity <<= 1;

                if (newCapacity < 0)
                {
                    throw new InvalidOperationException($"New capacity {newCapacity} must be positive");
                }
            }while (neededSpace > newCapacity);

            var newArray = new byte[newCapacity];

            PlatformDependent.CopyMemory(array, 0, newArray, 0, size);
            return(newArray);
        }