-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SpanBasedStringBuilder.cs
274 lines (249 loc) · 8.72 KB
/
SpanBasedStringBuilder.cs
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
namespace Microsoft.NET.StringTools
{
/// <summary>
/// A StringBuilder replacement that keeps a list of <see cref="ReadOnlyMemory{T}"/> spans making up the intermediate string rather
/// than a copy of its characters. This has positive impact on both memory (no need to allocate space for the intermediate string)
/// and time (no need to copy characters to the intermediate string).
/// </summary>
/// <remarks>
/// The <see cref="ToString"/> method tries to intern the resulting string without even allocating it if it's already interned.
/// Use <see cref="Strings.GetSpanBasedStringBuilder"/> to take advantage of pooling to eliminate allocation overhead of this class.
/// </remarks>
public class SpanBasedStringBuilder : IDisposable
{
/// <summary>
/// Enumerator for the top-level class. Enumerates characters of the string.
/// </summary>
public struct Enumerator
{
/// <summary>
/// The spans being enumerated.
/// </summary>
private readonly List<ReadOnlyMemory<char>> _spans;
/// <summary>
/// Index of the current span.
/// </summary>
private int _spanIndex;
/// <summary>
/// Index of the current character in the current span, -1 if MoveNext has not been called yet.
/// </summary>
private int _charIndex;
internal Enumerator(List<ReadOnlyMemory<char>> spans)
{
_spans = spans;
_spanIndex = 0;
_charIndex = -1;
}
/// <summary>
/// Returns the current character.
/// </summary>
public readonly char Current
{
get
{
ReadOnlyMemory<char> span = _spans[_spanIndex];
return span.Span[_charIndex];
}
}
/// <summary>
/// Moves to the next character.
/// </summary>
/// <returns>True if there is another character, false if the enumerator reached the end.</returns>
public bool MoveNext()
{
int newCharIndex = _charIndex + 1;
while (_spanIndex < _spans.Count)
{
if (newCharIndex < _spans[_spanIndex].Length)
{
_charIndex = newCharIndex;
return true;
}
_spanIndex++;
newCharIndex = 0;
}
return false;
}
}
/// <summary>
/// Spans making up the rope.
/// </summary>
private readonly List<ReadOnlyMemory<char>> _spans;
/// <summary>
/// Internal getter to get the list of spans out of the SpanBasedStringBuilder.
/// </summary>
internal List<ReadOnlyMemory<char>> Spans => _spans;
/// <summary>
/// Constructs a new SpanBasedStringBuilder containing the given string.
/// </summary>
/// <param name="str">The string to wrap, must be non-null.</param>
public SpanBasedStringBuilder(string str)
: this()
{
if (str == null)
{
throw new ArgumentNullException(nameof(str));
}
Append(str);
}
/// <summary>
/// Constructs a new empty SpanBasedStringBuilder with the given expected number of spans.
/// </summary>
public SpanBasedStringBuilder(int capacity = 4)
{
_spans = new List<ReadOnlyMemory<char>>(capacity);
Length = 0;
}
/// <summary>
/// Gets the length of the string.
/// </summary>
public int Length { get; private set; }
/// <summary>
/// Gets the capacity of the SpanBasedStringBuilder in terms of number of spans it can hold without allocating.
/// </summary>
public int Capacity => _spans.Capacity;
/// <summary>
/// Creates a new enumerator for enumerating characters in this string. Does not allocate.
/// </summary>
/// <returns>The enumerator.</returns>
public Enumerator GetEnumerator()
{
return new Enumerator(_spans);
}
/// <summary>
/// Converts this instance to a System.String while first searching for a match in the intern table.
/// </summary>
/// <remarks>
/// May allocate depending on whether the string has already been interned.
/// </remarks>
public override string ToString()
{
return new InternableString(this).ToString();
}
/// <summary>
/// Releases this instance.
/// </summary>
public void Dispose()
{
Strings.ReturnSpanBasedStringBuilder(this);
}
#region Public mutating methods
/// <summary>
/// Appends a string.
/// </summary>
/// <param name="value">The string to append.</param>
public void Append(string value)
{
if (!string.IsNullOrEmpty(value))
{
_spans.Add(value.AsMemory());
Length += value.Length;
}
}
/// <summary>
/// Appends a substring.
/// </summary>
/// <param name="value">The string to append.</param>
/// <param name="startIndex">The start index of the substring within <paramref name="value"/> to append.</param>
/// <param name="count">The length of the substring to append.</param>
public void Append(string value, int startIndex, int count)
{
if (value != null)
{
if (count > 0)
{
_spans.Add(value.AsMemory(startIndex, count));
Length += count;
}
}
else
{
if (startIndex != 0 || count != 0)
{
throw new ArgumentNullException(nameof(value));
}
}
}
/// <summary>
/// Appends a character span represented by <see cref="ReadOnlyMemory{T}" />.
/// </summary>
/// <param name="span">The character span to append.</param>
public void Append(ReadOnlyMemory<char> span)
{
if (!span.IsEmpty)
{
_spans.Add(span);
Length += span.Length;
}
}
/// <summary>
/// Removes leading white-space characters from the string.
/// </summary>
public void TrimStart()
{
for (int spanIdx = 0; spanIdx < _spans.Count; spanIdx++)
{
ReadOnlySpan<char> span = _spans[spanIdx].Span;
int i = 0;
while (i < span.Length && char.IsWhiteSpace(span[i]))
{
i++;
}
if (i > 0)
{
_spans[spanIdx] = _spans[spanIdx].Slice(i);
Length -= i;
}
if (!_spans[spanIdx].IsEmpty)
{
return;
}
}
}
/// <summary>
/// Removes trailing white-space characters from the string.
/// </summary>
public void TrimEnd()
{
for (int spanIdx = _spans.Count - 1; spanIdx >= 0; spanIdx--)
{
ReadOnlySpan<char> span = _spans[spanIdx].Span;
int i = span.Length - 1;
while (i >= 0 && char.IsWhiteSpace(span[i]))
{
i--;
}
if (i + 1 < span.Length)
{
_spans[spanIdx] = _spans[spanIdx].Slice(0, i + 1);
Length -= span.Length - (i + 1);
}
if (!_spans[spanIdx].IsEmpty)
{
return;
}
}
}
/// <summary>
/// Removes leading and trailing white-space characters from the string.
/// </summary>
public void Trim()
{
TrimStart();
TrimEnd();
}
/// <summary>
/// Clears this instance making it represent an empty string.
/// </summary>
public void Clear()
{
_spans.Clear();
Length = 0;
}
#endregion
}
}