Esempio n. 1
0
        private void FillBuffer()
        {
            int charsToRead = Math.Min(_charBuffer.Length, _source.Length - _sourceOffset);

            _source.CopyTo(_sourceOffset, _charBuffer, 0, charsToRead);
            _sourceOffset     += charsToRead;
            _bufferOffset      = 0;
            _bufferUnreadChars = charsToRead;
        }
Esempio n. 2
0
        /// <summary>
        /// Implements equality comparison of the content of two different instances of <see cref="SourceText"/>.
        /// </summary>
        protected virtual bool ContentEqualsImpl(SourceText other)
        {
            if (other == null)
            {
                return(false);
            }

            if (ReferenceEquals(this, other))
            {
                return(true);
            }

            if (this.Length != other.Length)
            {
                return(false);
            }

            var buffer1 = s_charArrayPool.Allocate();
            var buffer2 = s_charArrayPool.Allocate();

            try
            {
                int position = 0;
                while (position < this.Length)
                {
                    int n = Math.Min(this.Length - position, buffer1.Length);
                    this.CopyTo(position, buffer1, 0, n);
                    other.CopyTo(position, buffer2, 0, n);

                    for (int i = 0; i < n; i++)
                    {
                        if (buffer1[i] != buffer2[i])
                        {
                            return(false);
                        }
                    }

                    position += n;
                }

                return(true);
            }
            finally
            {
                s_charArrayPool.Free(buffer2);
                s_charArrayPool.Free(buffer1);
            }
        }
Esempio n. 3
0
        public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            if (!CheckCopyToArguments(sourceIndex, destination, destinationIndex, count))
            {
                return;
            }

            GetIndexAndOffset(sourceIndex, out int segIndex, out int segOffset);

            while (segIndex < _segments.Length && count > 0)
            {
                SourceText segment    = _segments[segIndex];
                int        copyLength = Math.Min(count, segment.Length - segOffset);

                segment.CopyTo(segOffset, destination, destinationIndex, copyLength);

                count            -= copyLength;
                destinationIndex += copyLength;
                segIndex++;
                segOffset = 0;
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Implements equality comparison of the content of two different instances of <see cref="SourceText"/>.
        /// </summary>
        protected virtual bool ContentEqualsImpl(SourceText other)
        {
            if (other == null)
            {
                return false;
            }

            if (ReferenceEquals(this, other))
            {
                return true;
            }

            if (this.Length != other.Length)
            {
                return false;
            }

            var buffer1 = s_charArrayPool.Allocate();
            var buffer2 = s_charArrayPool.Allocate();
            try
            {
                int position = 0;
                while (position < this.Length)
                {
                    int n = Math.Min(this.Length - position, buffer1.Length);
                    this.CopyTo(position, buffer1, 0, n);
                    other.CopyTo(position, buffer2, 0, n);

                    for (int i = 0; i < n; i++)
                    {
                        if (buffer1[i] != buffer2[i])
                        {
                            return false;
                        }
                    }

                    position += n;
                }

                return true;
            }
            finally
            {
                s_charArrayPool.Free(buffer2);
                s_charArrayPool.Free(buffer1);
            }
        }
Esempio n. 5
0
 public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
 {
     _newText.CopyTo(sourceIndex, destination, destinationIndex, count);
 }
                public void WriteText(SourceText text, CancellationToken cancellationToken)
                {
                    if (memoryMappedInfo != null)
                    {
                        throw new InvalidOperationException();
                    }

                    using (Logger.LogBlock(FeatureId.TemporaryStorage, FunctionId.Host_TemporaryStorageServiceFactory_WriteText, cancellationToken))
                    {
                        var size = Encoding.Unicode.GetMaxByteCount(text.Length);
                        memoryMappedInfo = service.memoryMappedFileManager.CreateViewInfo(size);

                        var buffer = SharedPools.CharArray.Allocate();
                        using (var stream = memoryMappedInfo.CreateWritableStream())
                        {
                            // PERF: Don't call text.Write(writer) directly since it can cause multiple large string
                            // allocations from String.Substring.  Instead use one of our pooled char[] buffers.
                            using (var writer = new StreamWriter(stream, Encoding.Unicode))
                            {
                                for (int index = 0; index < text.Length; index += buffer.Length)
                                {
                                    cancellationToken.ThrowIfCancellationRequested();
                                    var count = Math.Min(buffer.Length, text.Length - index);
                                    text.CopyTo(index, buffer, 0, count);
                                    writer.Write(buffer, 0, count);
                                }
                            }
                        }

                        SharedPools.CharArray.Free(buffer);
                    }
                }
Esempio n. 7
0
        public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            var span = GetCompositeSpan(sourceIndex, count);

            _text.CopyTo(span.Start, destination, destinationIndex, span.Length);
        }