Welcome to mirror list, hosted at ThFree Co, Russian Federation.

StringRebuilderForCompressedChars.cs « StringRebuilder « TextModel « Impl « Text « src - github.com/microsoft/vs-editor-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca80b09fa8d925dd46e18b226e94d5d7a7cbeb9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//  Licensed under the MIT License. See License.txt in the project root for license information.
//
// This file contain implementations details that are subject to change without notice.
// Use at your own risk.
//
using System;
using System.Diagnostics;
using System.IO;

namespace Microsoft.VisualStudio.Text.Implementation
{
    internal class StringRebuilderForCompressedChars : UnaryStringRebuilder
    {
        private readonly Page _content;

        internal static StringRebuilder Create(Page content, ILineBreaks lineBreaks)
        {
            return StringRebuilderForCompressedChars.Create(content, lineBreaks, 0, content.Length, 0, lineBreaks.Length);
        }

        private static StringRebuilder Create(Page content, ILineBreaks lineBreaks, int start, int length, int lineBreaksStart, int linebreaksLength)
        {
            Debug.Assert(content.Length > 0);

            var expanded = content.Expand();

            return new StringRebuilderForCompressedChars(content, lineBreaks, start, length, lineBreaksStart, linebreaksLength, expanded[start], expanded[start + length - 1]);
        }

        private StringRebuilderForCompressedChars(Page content, ILineBreaks lineBreaks, int start, int length, int lineBreaksStart, int linebreaksLength, char first, char last)
            : base(lineBreaks, start, length, lineBreaksStart, linebreaksLength, first, last)
        {
            _content = content;
        }

        #region StringRebuilder Members
        public override char this[int index]
        {
            get
            {
                return this.GetChar(_content.Expand(), index);
            }
        }

        public override string GetText(Span span)
        {
            return this.GetText(_content.Expand(), span);
        }

        public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            this.CopyTo(_content.Expand(), sourceIndex, destination, destinationIndex, count);
        }

        public override void Write(TextWriter writer, Span span)
        {
            this.Write(_content.Expand(), writer, span);
        }

        public override StringRebuilder GetSubText(Span span)
        {
            if (span.End > this.Length)
                throw new ArgumentOutOfRangeException("span");

            if (span.Length == this.Length)
                return this;
            else if (span.Length == 0)
                return StringRebuilder.Empty;
            else
            {
                int firstLineNumber;
                int lastLineNumber;
                this.FindFirstAndLastLines(span, out firstLineNumber, out lastLineNumber);

                return StringRebuilderForCompressedChars.Create(_content, _lineBreaks, span.Start + _textSpanStart, span.Length, firstLineNumber, lastLineNumber - firstLineNumber);
            }
        }
        #endregion
    }
}