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
|
//
// 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
{
internal abstract class UnaryStringRebuilder : StringRebuilder
{
internal readonly ILineBreaks _lineBreaks;
#if DEBUG
private static int _totalCreated = 0;
public static int TotalCreated { get { return _totalCreated; } }
#endif
protected readonly int _textSpanStart; //subspan of _storage contained in this StringRebuilderForChars
protected readonly int _lineBreakSpanStart; //subspan of _storage.LineBreaks that contains all line breaks in this StringRebuilderForChars
protected int TextSpanEnd { get { return _textSpanStart + this.Length; } }
protected int LineBreakSpanEnd { get { return _lineBreakSpanStart + this.LineBreakCount; } }
protected UnaryStringRebuilder(ILineBreaks lineBreaks, int start, int length, int linebreaksStart, int linebreaksLength, char first, char last)
: base(length, linebreaksLength, first, last)
{
#if DEBUG
Interlocked.Increment(ref _totalCreated);
#endif
_lineBreaks = lineBreaks;
_textSpanStart = start;
_lineBreakSpanStart = linebreaksStart;
}
internal void FindFirstAndLastLines(Span span, out int firstLineNumber, out int lastLineNumber)
{
firstLineNumber = this.GetLineNumberFromPosition(span.Start) + _lineBreakSpanStart;
lastLineNumber = this.GetLineNumberFromPosition(span.End) + _lineBreakSpanStart;
//Handle the special case where the end position falls in the middle of a linebreak.
if ((lastLineNumber < this.LineBreakSpanEnd) &&
(span.End > _lineBreaks.StartOfLineBreak(lastLineNumber) - _textSpanStart))
{
++lastLineNumber;
}
}
#region StringRebuilder Members
public override int GetLineNumberFromPosition(int position)
{
if ((position < 0) || (position > this.Length))
throw new ArgumentOutOfRangeException("position");
//Convert position to a position relative to the start of _text.
if (position == this.Length)
{
//Handle positions at the end of the span as a special case since otherwise we
//return the incorrect value if the last line break extends past the end of _textSpan.
return this.LineBreakCount;
}
position += _textSpanStart;
int start = _lineBreakSpanStart;
int end = this.LineBreakSpanEnd;
while (start < end)
{
int middle = (start + end) / 2;
if (position < _lineBreaks.EndOfLineBreak(middle))
end = middle;
else
start = middle + 1;
}
return start - _lineBreakSpanStart;
}
public override void GetLineFromLineNumber(int lineNumber, out Span extent, out int lineBreakLength)
{
if ((lineNumber < 0) || (lineNumber > this.LineBreakCount))
throw new ArgumentOutOfRangeException("lineNumber");
int absoluteLineNumber = _lineBreakSpanStart + lineNumber;
int start = (lineNumber == 0)
? 0
: (Math.Min(this.TextSpanEnd, _lineBreaks.EndOfLineBreak(absoluteLineNumber - 1)) - _textSpanStart);
int end;
if (lineNumber < this.LineBreakCount)
{
end = Math.Max(_textSpanStart, _lineBreaks.StartOfLineBreak(absoluteLineNumber));
lineBreakLength = Math.Min(this.TextSpanEnd, _lineBreaks.EndOfLineBreak(absoluteLineNumber)) - end;
end -= _textSpanStart;
}
else
{
end = this.Length;
lineBreakLength = 0;
}
extent = Span.FromBounds(start, end);
}
public override StringRebuilder GetLeaf(int position, out int offset)
{
offset = 0;
return this;
}
protected char GetChar(char[] content, int index)
{
if ((index < 0) || (index >= this.Length))
throw new ArgumentOutOfRangeException("index");
#if DEBUG
Interlocked.Increment(ref _totalCharactersReturned);
#endif
return content[index + _textSpanStart];
}
protected string GetText(char[] content, Span span)
{
if (span.End > this.Length)
throw new ArgumentOutOfRangeException("span");
#if DEBUG
Interlocked.Add(ref _totalCharactersReturned, span.Length);
#endif
return new string(content, span.Start + _textSpanStart, span.Length);
}
protected void CopyTo(char[] content, int sourceIndex, char[] destination, int destinationIndex, int count)
{
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 DEBUG
Interlocked.Add(ref _totalCharactersCopied, count);
#endif
Array.Copy(content, sourceIndex + _textSpanStart, destination, destinationIndex, count);
}
protected void Write(char[] content, TextWriter writer, Span span)
{
if (writer == null)
throw new ArgumentNullException("writer");
if (span.End > this.Length)
throw new ArgumentOutOfRangeException("span");
writer.Write(content, span.Start + _textSpanStart, span.Length);
}
public override StringRebuilder Child(bool rightSide)
{
throw new InvalidOperationException();
}
#endregion
}
}
|