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

StringRebuilder.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: 1fe8b6332d35f68789dcf36c6048d3ef0e79136a (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//
//  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.Diagnostics;
using System.IO;
using System.Threading;

namespace Microsoft.VisualStudio.Text.Implementation
{
    /// <summary>
    /// An immutable variation on the StringBuilder class.
    /// </summary>
    internal abstract class StringRebuilder
    {
        public readonly static StringRebuilder Empty = new StringRebuilderForString();

#if DEBUG
        protected static int _totalCharactersScanned = 0;
        public static int TotalCharactersScanned { get { return _totalCharactersScanned; } }

        protected static int _totalCharactersReturned = 0;
        public static int TotalCharactersReturned { get { return _totalCharactersReturned; } }

        protected static int _totalCharactersCopied = 0;
        public static int TotalCharactersCopied { get { return _totalCharactersCopied; } }
#endif

        public static StringRebuilder Create(string text)
        {
            if (text == null)
                throw new ArgumentNullException("text");
#if DEBUG
            Interlocked.Add(ref _totalCharactersScanned, text.Length);
#endif

            return (text.Length == 0)
                   ? StringRebuilder.Empty
                   : StringRebuilderForString.Create(text, text.Length, LineBreakManager.CreateLineBreaks(text));
        }

        public static StringRebuilder Create(ITextImage image)
        {
            if (image == null)
                throw new ArgumentNullException(nameof(image));

            var cti = image as CachingTextImage;
            if (cti != null)
                return cti.Builder;

            // This shouldn't happen but as a fallback, create a new string rebuilder from the text of the provided image.
            return StringRebuilder.Create(image.GetText(0, image.Length));
        }

        /// <summary>
        /// Consolidate two string rebuilders, taking advantage of the fact that they have already extracted the line breaks.
        /// </summary>
        public static StringRebuilder Consolidate(StringRebuilder left, StringRebuilder right)
        {
            Debug.Assert(left.Length > 0);
            Debug.Assert(right.Length > 0);

            int length = left.Length + right.Length;
            char[] result = new char[length];

            left.CopyTo(0, result, 0, left.Length);
            right.CopyTo(0, result, left.Length, right.Length);

            ILineBreaks lineBreaks;
            if ((left.LineBreakCount == 0) && (right.LineBreakCount == 0))
            {
                lineBreaks = LineBreakManager.Empty;
                //_lineBreakSpan defaults to 0, 0 which is what we want
            }
            else
            {
                ILineBreaksEditor breaks = LineBreakManager.CreateLineBreakEditor(length, left.LineBreakCount + right.LineBreakCount);

                int offset = 0;
                if ((result[left.Length] == '\n') && (result[left.Length - 1] == '\r'))
                {
                    //We have a \r\n spanning the seam ... add that as a special linebreak later.
                    offset = 1;
                }

                int leftLines = left.LineBreakCount - offset;
                for (int i = 0; (i < leftLines); ++i)
                {
                    Span extent;
                    int lineBreakLength;
                    left.GetLineFromLineNumber(i, out extent, out lineBreakLength);
                    breaks.Add(extent.End, lineBreakLength);
                }

                if (offset == 1)
                {
                    breaks.Add(left.Length - 1, 2);
                }

                for (int i = offset; (i < right.LineBreakCount); ++i)
                {
                    Span extent;
                    int lineBreakLength;
                    right.GetLineFromLineNumber(i, out extent, out lineBreakLength);
                    breaks.Add(extent.End + left.Length, lineBreakLength);
                }

                lineBreaks = breaks;
            }

            return StringRebuilderForChars.Create(result, length, lineBreaks);
        }

        protected StringRebuilder(int length, int lineBreakCount, char first, char last)
        {
            this.Length = length;
            this.LineBreakCount = lineBreakCount;
            this.FirstCharacter = first;
            this.LastCharacter = last;
        }

        /// <summary>
        /// Number of characters in this <see cref="StringRebuilder"/>.
        /// </summary>
        public readonly int Length;

        /// <summary>
        /// Number of line breaks in this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <remarks>Line breaks consist of any of '\r', '\n', 0x85,
        /// or a "\r\n" pair (which is treated as a single line break).</remarks>
        public int LineBreakCount;

        public virtual int Depth => 0;

        /// <summary>
        /// The first character of the StringRebuilder. \0 for a zero length StringRebuilder.
        /// </summary>
        public readonly char FirstCharacter;

        /// <summary>
        /// The last character of the StringRebuilder. \0 for a zero length StringRebuilder.
        /// </summary>
        public readonly char LastCharacter;

#region Abstract methods
        /// <summary>
        /// Get the zero-based line number that contains <paramref name="position"/>.
        /// </summary>
        /// <param name="position">Position of the character for which to get the line number.</param>
        /// <returns>Number of the line that contains <paramref name="position"/>.</returns>
        /// <remarks>
        /// Lines are bounded by line breaks and the start and end of this <see cref="StringRebuilder"/>.
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="position"/> is less than zero or greater than <see cref="Length"/>.</exception>
        public abstract int GetLineNumberFromPosition(int position);

        /// <summary>
        /// Get the TextImageLine associated with a zero-based line number.
        /// </summary>
        /// <param name="lineNumber">Line number for which to get the TextImageLine.</param>
        /// <remarks>
        /// <para>The last "line" in the StringRebuilder has an implicit line break length of zero.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="lineNumber"/> is less than zero or greater than <see cref="LineBreakCount"/>.</exception>
        public abstract void GetLineFromLineNumber(int lineNumber, out Span extent, out int lineBreakLength);

        /// <summary>
        /// Get the "leaf" node of the string rebuilder that contains position.
        /// </summary>
        /// <param name="position">position for which to get the leaf.</param>
        /// <param name="offset">number of characters to the left of the leaf.</param>
        /// <returns>leaf node from the string rebuilder.</returns>
        public abstract StringRebuilder GetLeaf(int position, out int offset);

        /// <summary>
        /// Character at the given index.
        /// </summary>
        /// <param name="index">Index to get the character for.</param>
        /// <returns>Character at position <paramref name="index"/>.</returns>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="index"/> is less than zero or greater than or equal to <see cref="Length"/>.</exception>
        public abstract char this[int index] { get; }

        /// <summary>
        /// Copy a range of text to a destination character array.
        /// </summary>
        /// <param name="sourceIndex">
        /// The starting index to copy from.
        /// </param>
        /// <param name="destination">
        /// The destination array.
        /// </param>
        /// <param name="destinationIndex">
        /// The index in the destination of the first position to be copied to.
        /// </param>
        /// <param name="count">
        /// The number of characters to copy.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="sourceIndex"/> is less than zero or greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="count"/> is less than zero or <paramref name="sourceIndex"/> + <paramref name="count"/> is greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="destinationIndex"/> is less than zero or <paramref name="destinationIndex"/> + <paramref name="count"/> is greater than the length of <paramref name="destination"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="destination"/> is null.</exception>
        public abstract void CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);

        /// <summary>
        /// Write a substring of the contents of this <see cref="StringRebuilder"/> to a TextWriter.
        /// </summary>
        /// <param name="writer">TextWriter to use.</param>
        /// <param name="span">Span to write.</param>
        /// <exception cref="ArgumentNullException"><paramref name="writer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="span"/>.End is greater than <see cref="Length"/>.</exception>
        public abstract void Write(TextWriter writer, Span span);

        /// <summary>
        /// Create a new StringRebuilder that corresponds to a substring of this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <param name="span">span that defines the desired substring.</param>
        /// <returns>A new StringRebuilder containing the substring.</returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="span"/>.End is greater than <see cref="Length"/>.</exception>
        public abstract StringRebuilder GetSubText(Span span);

        /// <summary>
        /// Get the string that contains all of the characters in the specified span.
        /// </summary>
        /// <param name="span">Span for which to get the text.</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> can contain millions of characters. Be careful what you
        /// ask for: you might get it.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="span"/>.End is greater than <see cref="Length"/>.</exception>
        public abstract string GetText(Span span);

        public abstract StringRebuilder Child(bool rightSide);
#endregion

        /// <summary>
        /// Convert a range of text to a character array.
        /// </summary>
        /// <param name="startIndex">
        /// The starting index of the range of text.
        /// </param>
        /// <param name="length">
        /// The length of the text.
        /// </param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="startIndex"/> is less than zero or greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="length"/> is less than zero or <paramref name="startIndex"/> + <paramref name="length"/> is greater than <see cref="Length"/>.</exception>
        public char[] ToCharArray(int startIndex, int length)
        {
            if (startIndex < 0)
                throw new ArgumentOutOfRangeException("startIndex");

            if ((length < 0) || (startIndex + length > this.Length) || (startIndex + length < 0))
                throw new ArgumentOutOfRangeException("length");

            char[] copy = new char[length];
            this.CopyTo(startIndex, copy, 0, length);

            return copy;
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to appending text into this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <param name="text">Text to append.</param>
        /// <returns>A new StringRebuilder containing the insertion.</returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is null.</exception>
        public StringRebuilder Append(string text)
        {
            return this.Insert(this.Length, text);
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to appending text into this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <param name="text">Text to append.</param>
        /// <returns>A new StringRebuilder containing the insertion.</returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is null.</exception>
        public StringRebuilder Append(StringRebuilder text)
        {
            return this.Insert(this.Length, text);
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to inserting text into this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <param name="position">Position at which to insert.</param>
        /// <param name="text">Text to insert.</param>
        /// <returns>A new StringRebuilder containing the insertion.</returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="position"/> is less than zero or greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is null.</exception>
        public StringRebuilder Insert(int position, string text)
        {
            return this.Insert(position, StringRebuilder.Create(text));
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to inserting text into this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <param name="position">Position at which to insert.</param>
        /// <param name="text">Text to insert.</param>
        /// <returns>A new StringRebuilder containing the insertion.</returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="position"/> is less than zero or greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is null.</exception>
        public StringRebuilder Insert(int position, StringRebuilder text)
        {
            if ((position < 0) || (position > this.Length))
                throw new ArgumentOutOfRangeException("position");
            if (text == null)
                throw new ArgumentNullException("text");

            return this.Assemble(Span.FromBounds(0, position), text, Span.FromBounds(position, this.Length));
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to deleting text from this <see cref="StringRebuilder"/>.
        /// </summary>
        /// <param name="span">Span of text to delete.</param>
        /// <returns>A new StringRebuilder containing the deletion.</returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="span"/>.End is greater than <see cref="Length"/>.</exception>
        public StringRebuilder Delete(Span span)
        {
            if (span.End > this.Length)
                throw new ArgumentOutOfRangeException("span");

            return this.Assemble(Span.FromBounds(0, span.Start), Span.FromBounds(span.End, this.Length));
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to replacing a contiguous span of characters
        /// with different text.
        /// </summary>
        /// <param name="span">
        /// Span of text in this <see cref="StringRebuilder"/> to replace.
        /// </param>
        /// <param name="text">
        /// The new text to replace the old.
        /// </param>
        /// <returns>
        /// A new string rebuilder containing the replacement.
        /// </returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="span"/>.End is greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is null.</exception>
        public StringRebuilder Replace(Span span, string text)
        {
            return this.Replace(span, StringRebuilder.Create(text));
        }

        /// <summary>
        /// Create a new StringRebuilder equivalent to replacing a contiguous span of characters
        /// with different text.
        /// </summary>
        /// <param name="span">
        /// Span of text in this <see cref="StringRebuilder"/> to replace.
        /// </param>
        /// <param name="text">
        /// The new text to replace the old.
        /// </param>
        /// <returns>
        /// A new string rebuilder containing the replacement.
        /// </returns>
        /// <remarks>
        /// <para>this <see cref="StringRebuilder"/> is not modified.</para>
        /// <para>This operation can be performed simultaneously on multiple threads.</para>
        /// </remarks>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="span"/>.End is greater than <see cref="Length"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="text"/> is null.</exception>
        public StringRebuilder Replace(Span span, StringRebuilder text)
        {
            if (span.End > this.Length)
                throw new ArgumentOutOfRangeException("span");
            if (text == null)
                throw new ArgumentNullException("text");

            return this.Assemble(Span.FromBounds(0, span.Start), text, Span.FromBounds(span.End, this.Length));
        }

#region Private
        private StringRebuilder Assemble(Span left, Span right)
        {
            if (left.Length == 0)
                return this.GetSubText(right);
            else if (right.Length == 0)
                return this.GetSubText(left);
            else if (left.Length + right.Length == this.Length)
                return this;
            else
                return BinaryStringRebuilder.Create(this.GetSubText(left), this.GetSubText(right));
        }

        private StringRebuilder Assemble(Span left, StringRebuilder text, Span right)
        {
            if (text.Length == 0)
                return Assemble(left, right);
            else if (left.Length == 0)
                return (right.Length == 0) ? text : BinaryStringRebuilder.Create(text, this.GetSubText(right));
            else if (right.Length == 0)
                return BinaryStringRebuilder.Create(this.GetSubText(left), text);
            else if (left.Length < right.Length)
                return BinaryStringRebuilder.Create(BinaryStringRebuilder.Create(this.GetSubText(left), text),
                                                    this.GetSubText(right));
            else
                return BinaryStringRebuilder.Create(this.GetSubText(left),
                                                    BinaryStringRebuilder.Create(text, this.GetSubText(right)));
        }
#endregion
    }
}