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

BinaryStringRebuilder.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: 26139fc9ff44b9183fa1968444913e5907480795 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
//  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.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Globalization;
using System.Collections;
using System.IO;
using System.Threading;
using Microsoft.VisualStudio.Text;
using Microsoft.VisualStudio.Text.Utilities;

namespace Microsoft.VisualStudio.Text.Implementation
{
    internal sealed class BinaryStringRebuilder : StringRebuilder
    {
        internal readonly StringRebuilder _left;
        internal readonly StringRebuilder _right;

#if DEBUG
        private static int _totalCreated = 0;
        public static int TotalCreated { get { return _totalCreated; } }
#endif

        #region Private
        private static StringRebuilder _crlf = StringRebuilder.Create("\r\n");

        // internal for unit tests, a \r\n can't be spanned by left and right
        internal BinaryStringRebuilder(StringRebuilder left, StringRebuilder right)
            : base(left.Length + right.Length, left.LineBreakCount + right.LineBreakCount, left.FirstCharacter, right.LastCharacter)
        {
            Debug.Assert(left.Length > 0);
            Debug.Assert(right.Length > 0);
            Debug.Assert(Math.Abs(left.Depth - right.Depth) <= 1);
            Debug.Assert(left.LastCharacter != '\r' || right.FirstCharacter != '\n');

#if DEBUG
            Interlocked.Increment(ref _totalCreated);
#endif

            _left = left;
            _right = right;
            this.Depth = 1 + Math.Max(left.Depth, right.Depth);
        }

        private static StringRebuilder ConsolidateOrBalanceTreeNode(StringRebuilder left, StringRebuilder right)
        {
            if ((left.Length + right.Length < TextModelOptions.StringRebuilderMaxCharactersToConsolidate) &&
                (left.LineBreakCount + right.LineBreakCount <= TextModelOptions.StringRebuilderMaxLinesToConsolidate))
            {
                //Consolidate the two rebuilders into a single simple string rebuilder
                return StringRebuilder.Consolidate(left, right);
            }
            else
                return BinaryStringRebuilder.BalanceTreeNode(left, right);
        }

        private static StringRebuilder BalanceStringRebuilder(StringRebuilder left, StringRebuilder right)
        {
            return BinaryStringRebuilder.BalanceTreeNode(left, right);
        }

        private static StringRebuilder BalanceTreeNode(StringRebuilder left, StringRebuilder right)
        {
            if (left.Depth > right.Depth + 1)
                return BinaryStringRebuilder.Pivot(left, right, false);
            else if (right.Depth > left.Depth + 1)
                return BinaryStringRebuilder.Pivot(right, left, true);
            else
                return new BinaryStringRebuilder(left, right);
        }

        private static StringRebuilder Pivot(StringRebuilder child, StringRebuilder other, bool deepOnRightSide)
        {
            Debug.Assert(child.Depth > 0);  //child's depth is greater than other's depth.
            StringRebuilder grandchildOutside = child.Child(deepOnRightSide);
            StringRebuilder grandchildInside = child.Child(!deepOnRightSide);

            if (grandchildOutside.Depth >= grandchildInside.Depth)
            {
                //Simple pivot.
                //From this (case deepOnRightSide)
                //                    this
                //                   /    \
                //                other    child
                //                 ...    /      \
                //                       gcI    gcO
                //                       ...    ...
                //
                //To this:
                //                    child'
                //                   /      \
                //                 this'      gcO
                //                /    \     ...
                //             other   gcI

                StringRebuilder newThis;
                StringRebuilder newChild;
                if (deepOnRightSide)
                {
                    newThis = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(other, grandchildInside);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(newThis, grandchildOutside);
                }
                else
                {
                    newThis = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(grandchildInside, other);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(grandchildOutside, newThis);
                }

                return newChild;
            }
            else
            {
                //Complex pivot.
                //From this (case !deepOnRightSide)
                //                    this
                //                   /    \
                //                other    child
                //                 ...    /      \
                //                       gcI      gcO
                //                     /     \    ...
                //                   ggcI   ggcO
                //                    ...    ...
                //
                //To this:
                //                     gcI'
                //                   /     \
                //                 this'     child'
                //                /    \    /     \
                //              other ggcI ggcO   gcO
                //              ...  ...   ...    ...
                Debug.Assert(grandchildInside.Depth > 0);  //The inside's grandchild depth is > the outside grandchild's.
                StringRebuilder greatgrandchildOutside = grandchildInside.Child(deepOnRightSide);
                StringRebuilder greatgrandchildInside = grandchildInside.Child(!deepOnRightSide);

                StringRebuilder newThis;
                StringRebuilder newChild;
                StringRebuilder newGcI;

                if (deepOnRightSide)
                {
                    newThis = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(other, greatgrandchildInside);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(greatgrandchildOutside, grandchildOutside);
                    newGcI = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(newThis, newChild);
                }
                else
                {
                    newThis = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(greatgrandchildInside, other);
                    newChild = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(grandchildOutside, greatgrandchildOutside);
                    newGcI = BinaryStringRebuilder.ConsolidateOrBalanceTreeNode(newChild, newThis);
                }

                return newGcI;
            }
        }
        #endregion

        public static StringRebuilder Create(StringRebuilder left, StringRebuilder right)
        {
            if (left == null)
                throw new ArgumentNullException(nameof(left));
            if (right == null)
                throw new ArgumentNullException(nameof(right));

            if (left.Length == 0)
                return right;
            else if (right.Length == 0)
                return left;
            else if ((left.Length + right.Length < TextModelOptions.StringRebuilderMaxCharactersToConsolidate) &&
                    (left.LineBreakCount + right.LineBreakCount <= TextModelOptions.StringRebuilderMaxLinesToConsolidate))
            {
                //Consolidate the two rebuilders into a single simple string rebuilder
                return StringRebuilder.Consolidate(left, right);
            }
            else if ((right.FirstCharacter == '\n') && (left.LastCharacter == '\r'))
            {
                //Don't allow a line break to be broken across the seam
                return BinaryStringRebuilder.Create(BinaryStringRebuilder.Create(left.GetSubText(new Span(0, left.Length - 1)),
                                                                                 _crlf),
                                                    right.GetSubText(Span.FromBounds(1, right.Length)));
            }
            else
            {
                return BinaryStringRebuilder.BalanceStringRebuilder(left, right);
            }
        }

        public override string ToString()
        {
            return string.Format(System.Globalization.CultureInfo.InvariantCulture, this.Depth % 2 == 0 ? "({0})({1})" : "[{0}][{1}]",
                                 _left.ToString(), _right.ToString());
        }

        public override int Depth { get; }

        #region StringRebuilder Members
        public override int GetLineNumberFromPosition(int position)
        {
            if ((position < 0) || (position > this.Length))
                throw new ArgumentOutOfRangeException(nameof(position));

            return (position <= _left.Length)
                   ? _left.GetLineNumberFromPosition(position)
                   : (_left.LineBreakCount +
                      _right.GetLineNumberFromPosition(position - _left.Length));
        }

        public override void GetLineFromLineNumber(int lineNumber, out Span extent, out int lineBreakLength)
        {
            if ((lineNumber < 0) || (lineNumber > this.LineBreakCount))
                throw new ArgumentOutOfRangeException(nameof(lineNumber));

            if (lineNumber < _left.LineBreakCount)
            {
                _left.GetLineFromLineNumber(lineNumber, out extent, out lineBreakLength);
            }
            else if (lineNumber > _left.LineBreakCount)
            {
                _right.GetLineFromLineNumber(lineNumber - _left.LineBreakCount, out extent, out lineBreakLength);
                extent = new Span(extent.Start + _left.Length, extent.Length);
            }
            else
            {
                // The line crosses the seam.
                int start = 0;
                if (lineNumber != 0)
                {
                    _left.GetLineFromLineNumber(lineNumber, out extent, out lineBreakLength);   // ignore the returned extend.Length

                    start = extent.Start;
                    Debug.Assert(lineBreakLength == 0);
                }

                int end;

                if (lineNumber == this.LineBreakCount)
                {
                    end = this.Length;
                    lineBreakLength = 0;
                }
                else
                {
                    _right.GetLineFromLineNumber(0, out extent, out lineBreakLength);
                    end = extent.End + _left.Length;
                }

                extent = Span.FromBounds(start, end);
            }
        }

        public override StringRebuilder GetLeaf(int position, out int offset)
        {
            if (position < _left.Length)
            {
                return _left.GetLeaf(position, out offset);
            }
            else
            {
                var leaf = _right.GetLeaf(position - _left.Length, out offset);
                offset += _left.Length;
                return leaf;
            }
        }

        public override char this[int index]
        {
            get
            {
                if ((index < 0) || (index >= this.Length))
                    throw new ArgumentOutOfRangeException(nameof(index));

                return (index < _left.Length)
                        ? _left[index]
                        : _right[index - _left.Length];
            }
        }

        public override string GetText(Span span)
        {
            if (span.End > this.Length)
                throw new ArgumentOutOfRangeException(nameof(span));

            if (span.End <= _left.Length)
                return _left.GetText(span);
            else if (span.Start >= _left.Length)
                return _right.GetText(new Span(span.Start - _left.Length, span.Length));
            else
            {
                char[] result = new char[span.Length];

                int leftLength = _left.Length - span.Start;
                _left.CopyTo(span.Start, result, 0, leftLength);
                _right.CopyTo(0, result, leftLength, span.Length - leftLength);

                return new string(result);
            }
        }

        public override void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count)
        {
            //These tests get executed a lot and are redundant: if there is an error, then the corresponding exception
            //will be thrown when we reach a leaf node.

            //if (sourceIndex < 0)
            //    throw new ArgumentOutOfRangeException("sourceIndex");
            //if (destination == null)
            //    throw new ArgumentNullException("destination");
            //if (destinationIndex < 0)
            //    throw new ArgumentOutOfRangeException("destinationIndex");
            //if (count < 0)
            //    throw new ArgumentOutOfRangeException("count");

            //if ((sourceIndex + count > this.Length) || (sourceIndex + count < 0))
            //    throw new ArgumentOutOfRangeException("count");

            //if ((destinationIndex + count > destination.Length) || (destinationIndex + count < 0))
            //    throw new ArgumentOutOfRangeException("count");

            if (sourceIndex >= _left.Length)
                _right.CopyTo(sourceIndex - _left.Length, destination, destinationIndex, count);
            else if (sourceIndex + count <= _left.Length)
                _left.CopyTo(sourceIndex, destination, destinationIndex, count);
            else
            {
                int leftLength = _left.Length - sourceIndex;

                _left.CopyTo(sourceIndex, destination, destinationIndex, leftLength);
                _right.CopyTo(0, destination, destinationIndex + leftLength, count - leftLength);
            }
        }

        public override void Write(TextWriter writer, Span span)
        {
            if (writer == null)
                throw new ArgumentNullException(nameof(writer));
            if (span.End > this.Length)
                throw new ArgumentOutOfRangeException(nameof(span));

            if (span.Start >= _left.Length)
                _right.Write(writer, new Span(span.Start - _left.Length, span.Length));
            else if (span.End <= _left.Length)
                _left.Write(writer, span);
            else
            {
                _left.Write(writer, Span.FromBounds(span.Start, _left.Length));
                _right.Write(writer, Span.FromBounds(0, span.End - _left.Length));
            }
        }

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

            if (span.Length == this.Length)
                return this;
            else if (span.End <= _left.Length)
                return _left.GetSubText(span);
            else if (span.Start >= _left.Length)
                return _right.GetSubText(new Span(span.Start - _left.Length, span.Length));
            else
                return BinaryStringRebuilder.Create(_left.GetSubText(Span.FromBounds(span.Start, _left.Length)),
                                                    _right.GetSubText(Span.FromBounds(0, span.End - _left.Length)));
        }

        public override StringRebuilder Child(bool rightSide)
        {
            return rightSide ? _right : _left;
        }
        #endregion
    }
}