예제 #1
0
        public static StringRebuilder Create(ITextStorageLoader loader)
        {
            if (loader == null)
            {
                throw new ArgumentNullException("loader");
            }

            StringRebuilder content = _empty;

            foreach (ITextStorage storage in loader.Load())
            {
                content = content.Insert(content.Length, storage);
            }
            return(content);
        }
예제 #2
0
        public static ChangeString CreateChangeString(IList <SnapshotSpan> sourceSpans, Span selected)
        {
            if (selected.Length == 0)
            {
                return(ChangeString.EmptyChangeString);
            }
            else if (selected.Length == 1)
            {
                return(ReferenceChangeString.CreateChangeString(sourceSpans[selected.Start]));
            }
            else
            {
                StringRebuilder builder = SimpleStringRebuilder.Create(String.Empty);
                for (int i = 0; (i < selected.Length); ++i)
                {
                    builder = builder.Insert(builder.Length, BufferFactoryService.StringRebuilderFromSnapshotSpan(sourceSpans[selected.Start + i]));
                }

                return(ReferenceChangeString.CreateChangeString(builder));
            }
        }
예제 #3
0
        internal static StringRebuilder Load(TextReader reader, long fileSize,
                                             out NewlineState newlineState,
                                             out LeadingWhitespaceState leadingWhitespaceState,
                                             out int longestLineLength,
                                             int blockSize = 0,
                                             int minCompressedBlockSize    = TextImageLoader.BlockSize,                                          // Exposed for unit tests
                                             bool throwOnInvalidCharacters = false)
        {
            newlineState           = new NewlineState();
            leadingWhitespaceState = new LeadingWhitespaceState();

            int currentLineLength = 0;

            longestLineLength = 0;
            char thresholdForInvalidCharacters = throwOnInvalidCharacters ? '\u0001' : '\0';    // Basically the only invalid character is \0, if we are looking for invalid characters.

            bool useCompressedStringRebuilders = (fileSize >= TextModelOptions.CompressedStorageFileSizeThreshold);

            if (blockSize == 0)
            {
                blockSize = useCompressedStringRebuilders ? TextModelOptions.CompressedStoragePageSize : TextImageLoader.BlockSize;
            }

            PageManager pageManager = null;

            char[] buffer;
            if (useCompressedStringRebuilders)
            {
                pageManager = new PageManager();
                buffer      = new char[blockSize];
            }
            else
            {
                buffer = TextImageLoader.AcquireBuffer(blockSize);
            }

            StringRebuilder content = StringRebuilderForChars.Empty;

            try
            {
                bool nextCharIsStartOfLine = true;
                while (true)
                {
                    int read = TextImageLoader.LoadNextBlock(reader, buffer);

                    if (read == 0)
                    {
                        break;
                    }

                    var lineBreaks = TextImageLoader.ParseBlock(
                        buffer,
                        read,
                        thresholdForInvalidCharacters,
                        ref newlineState,
                        ref leadingWhitespaceState,
                        ref currentLineLength,
                        ref longestLineLength,
                        ref nextCharIsStartOfLine);

                    char[] bufferForStringBuilder = buffer;
                    if (read < (buffer.Length / 2))
                    {
                        // We read far less characters than buffer so copy the contents to a new buffer and reuse the original buffer.
                        bufferForStringBuilder = new char[read];
                        Array.Copy(buffer, bufferForStringBuilder, read);
                    }
                    else
                    {
                        // We're using most of buffer so allocate a new block for the next chunk.
                        buffer = new char[blockSize];
                    }

                    var newContent = (useCompressedStringRebuilders && (read > minCompressedBlockSize))
                                     ? StringRebuilderForCompressedChars.Create(new Page(pageManager, bufferForStringBuilder, read), lineBreaks)
                                     : StringRebuilderForChars.Create(bufferForStringBuilder, read, lineBreaks);

                    content = content.Insert(content.Length, newContent);
                }

                longestLineLength = Math.Max(longestLineLength, currentLineLength);
            }
            finally
            {
                if (!useCompressedStringRebuilders)
                {
                    TextImageLoader.ReleaseBuffer(buffer);
                }
            }

            return(content);
        }
예제 #4
0
        internal static StringRebuilder Load(TextReader reader, long fileSize, string id,
                                             out bool hasConsistentLineEndings, out int longestLineLength,
                                             int blockSize = 0,
                                             int minCompressedBlockSize = TextImageLoader.BlockSize)                                             // Exposed for unit tests
        {
            LineEndingState lineEnding        = LineEndingState.Unknown;
            int             currentLineLength = 0;

            longestLineLength = 0;

            bool useCompressedStringRebuilders = (fileSize >= TextModelOptions.CompressedStorageFileSizeThreshold);

            if (blockSize == 0)
            {
                blockSize = useCompressedStringRebuilders ? TextModelOptions.CompressedStoragePageSize : TextImageLoader.BlockSize;
            }

            PageManager pageManager = null;

            char[] buffer;
            if (useCompressedStringRebuilders)
            {
                pageManager = new PageManager();
                buffer      = new char[blockSize];
            }
            else
            {
                buffer = TextImageLoader.AcquireBuffer(blockSize);
            }

            StringRebuilder content = StringRebuilderForChars.Empty;

            try
            {
                while (true)
                {
                    int read = TextImageLoader.LoadNextBlock(reader, buffer);

                    if (read == 0)
                    {
                        break;
                    }

                    var lineBreaks = LineBreakManager.CreateLineBreakEditor(read);
                    TextImageLoader.ParseBlock(buffer, read, lineBreaks, ref lineEnding, ref currentLineLength, ref longestLineLength);

                    char[] bufferForStringBuilder = buffer;
                    if (read < (buffer.Length / 2))
                    {
                        // We read far less characters than buffer so copy the contents to a new buffer and reuse the original buffer.
                        bufferForStringBuilder = new char[read];
                        Array.Copy(buffer, bufferForStringBuilder, read);
                    }
                    else
                    {
                        // We're using most of bufferForStringRebuilder so allocate a new block for the next chunk.
                        buffer = new char[blockSize];
                    }

                    var newContent = (useCompressedStringRebuilders && (read > minCompressedBlockSize))
                                     ? StringRebuilderForCompressedChars.Create(new Page(pageManager, bufferForStringBuilder, read), lineBreaks)
                                     : StringRebuilderForChars.Create(bufferForStringBuilder, read, lineBreaks);

                    content = content.Insert(content.Length, newContent);
                }

                longestLineLength        = Math.Max(longestLineLength, currentLineLength);
                hasConsistentLineEndings = lineEnding != LineEndingState.Inconsistent;
            }
            finally
            {
                if (!useCompressedStringRebuilders)
                {
                    TextImageLoader.ReleaseBuffer(buffer);
                }
            }

            return(content);
        }