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);
 }
Exemplo n.º 5
0
 public IAppendable Append(byte c)
 {
     if ((uint)_pos >= (uint)_chars.Length)
     {
         byte[] old = _chars;
         _chars = new byte[old.Length << 1];
         PlatformDependent.CopyMemory(old, 0, _chars, 0, old.Length);
     }
     _chars[_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));
        }
Exemplo n.º 7
0
        public ICharSequence SubSequence(int start, int end)
        {
            int length = end - start;

            if (0u >= (uint)length)
            {
                // If start and end index is the same we need to return an empty sequence to conform to the interface.
                // As our expanding logic depends on the fact that we have a char[] with length > 0 we need to construct
                // an instance for which this is true.
                return(new AppendableCharSequence(Math.Min(16, _chars.Length)));
            }
            var data = new byte[length];

            PlatformDependent.CopyMemory(_chars, start, data, 0, length);
            return(new AppendableCharSequence(data));
        }
Exemplo n.º 8
0
        public IAppendable Append(ICharSequence sequence, int start, int end)
        {
            if ((uint)end > (uint)sequence.Count)
            {
                ThrowHelper.ThrowIndexOutOfRangeException();
            }
            if ((uint)start > (uint)end)
            {
                ThrowHelper.ThrowIndexOutOfRangeException();
            }

            int length = end - start;

            if ((uint)length > (uint)(_chars.Length - _pos))
            {
                _chars = Expand(_chars, _pos + length, _pos);
            }

            switch (sequence)
            {
            case AppendableCharSequence seq:
                // Optimize append operations via array copy
                byte[] src = seq._chars;
                PlatformDependent.CopyMemory(src, start, _chars, _pos, length);
                _pos += length;
                break;

            case IHasAsciiSpan hasAscii:
                hasAscii.AsciiSpan.Slice(start, length).CopyTo(_chars.AsSpan(_pos, length));
                _pos += length;
                break;

            default:
                for (int idx = start; idx < end; idx++)
                {
                    _chars[_pos++] = (byte)sequence[idx];
                }
                break;
            }

            return(this);
        }
        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);
        }