-
Notifications
You must be signed in to change notification settings - Fork 1
/
ValueStringBuilder.cs
467 lines (393 loc) · 14.9 KB
/
ValueStringBuilder.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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#nullable enable
using System;
using System.Buffers;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Nucs.Text;
internal ref struct ValueStringBuilderDebugView {
public ValueStringBuilderDebugView(ValueStringBuilder sb) : this() {
Value = sb.ToString();
}
[DebuggerBrowsable(DebuggerBrowsableState.RootHidden)]
public string Value;
}
/// <summary>
/// A value <see cref="StringBuilder"/> taken from internals of .NET 7. Rents array buffer from <see cref="ArrayPool{T}.Shared"/> which supports resizable by bucket indexing.
/// </summary>
[DebuggerTypeProxy(typeof(ValueStringBuilderDebugView))]
[DebuggerDisplay("{ToString(),raw}")]
public ref struct ValueStringBuilder {
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private char[]? _arrayToReturnToPool;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Span<char> _chars;
/// <summary>
/// The number of characters present in the <see cref="ValueStringBuilder"/>.
/// </summary>
private int _length;
public ValueStringBuilder(Span<char> initialBuffer) {
_arrayToReturnToPool = null;
_chars = initialBuffer;
_length = 0;
}
public ValueStringBuilder(Span<char> initialBuffer, int startIndex) {
_arrayToReturnToPool = null;
_chars = initialBuffer;
_length = startIndex;
}
public ValueStringBuilder(int initialCapacity) {
_arrayToReturnToPool = ArrayPool<char>.Shared.Rent(initialCapacity);
_chars = _arrayToReturnToPool.AsSpan();
_length = 0;
}
/// <summary>
/// The length of characters written by now.
/// </summary>
/// <remarks>Write this property or call <see cref="AddWrittenLength"/> to report external writes to this builder.</remarks>
public int Length {
get => _length;
set {
Debug.Assert(value >= 0);
Debug.Assert(value <= _chars.Length);
_length = value;
}
}
public int Capacity => _chars.Length;
/// <summary>
/// Makes sure there is a string of atleast <paramref name="capacity"/> can initialized
/// </summary>
/// <param name="capacity">How many chars are needed in total in this stringbuilder including what was already built</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EnsureTotalCapacity(int capacity) {
if (capacity > _chars.Length)
Grow(capacity - _length);
}
/// <summary>
/// Makes sure there is a string of atleast <paramref name="capacity"/> can initialized
/// </summary>
/// <param name="capacity">How many chars are needed in total in this stringbuilder including what was already built</param>
[Obsolete("Use EnsureTotalCapacity")]
public void EnsureCapacity(int capacity) {
EnsureTotalCapacity(capacity);
}
/// <summary>
/// Out of remaining empty size, make sure there is at-least <paramref name="required"/> space
/// </summary>
/// <param name="required">The required space in chars in this builder</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void EnsureRemainderCapacity(int required) {
var leftover = _chars.Length - _length;
if (required > leftover)
Grow(required - leftover);
}
/// <summary>
/// Get a pinnable reference to the builder.
/// Does not ensure there is a null char after <see cref="Length"/>
/// This overload is pattern matched in the C# 7.3+ compiler so you can omit
/// the explicit method call, and write eg "fixed (char* c = builder)"
/// </summary>
public ref char GetPinnableReference() {
return ref MemoryMarshal.GetReference(_chars);
}
/// <summary>
/// Get a pinnable reference to the builder.
/// </summary>
/// <param name="terminate">Ensures that the builder has a null char after <see cref="Length"/></param>
public ref char GetPinnableReference(bool terminate) {
if (terminate) {
EnsureTotalCapacity(Length + 1);
_chars[Length] = '\0';
}
return ref MemoryMarshal.GetReference(_chars);
}
public ref char this[int index] {
get {
Debug.Assert(index < _length);
return ref _chars[index];
}
}
/// <summary>
/// Gets the built string so far without disposing the internal buffer.
/// </summary>
public override string ToString() {
var length = _length;
if (length == 0)
return string.Empty;
return _chars.Slice(0, length).ToString();
}
/// <summary>
/// Gets the built string and disposes internal buffers.
/// </summary>
/// <returns>The built string so far.</returns>
public string ToStringFinalize() {
string s = _chars.Slice(0, _length).ToString();
Dispose();
return s;
}
/// <summary>Returns the underlying storage of the builder.</summary>
public Span<char> RawChars => _chars;
/// <summary>
/// Returns a span around the contents of the builder.
/// </summary>
/// <param name="terminate">Ensures that the builder has a null char after <see cref="Length"/></param>
public ReadOnlySpan<char> AsSpan(bool terminate) {
if (terminate) {
EnsureTotalCapacity(Length + 1);
_chars[Length] = '\0';
}
return _chars.Slice(0, _length);
}
public ReadOnlySpan<char> AsSpan() =>
_chars.Slice(0, _length);
public ReadOnlySpan<char> AsSpan(int start) =>
_chars.Slice(start, _length - start);
public ReadOnlySpan<char> AsSpan(int start, int length) =>
_chars.Slice(start, length);
public bool TryCopyTo(Span<char> destination, out int charsWritten) {
if (_chars.Slice(0, _length).TryCopyTo(destination)) {
charsWritten = _length;
Dispose();
return true;
} else {
charsWritten = 0;
Dispose();
return false;
}
}
public void Insert(int index, char value, int count) {
if (_length > _chars.Length - count) {
Grow(count);
}
int remaining = _length - index;
_chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
_chars.Slice(index, count).Fill(value);
_length += count;
}
public void Insert(int index, string? s) {
if (s == null) {
return;
}
int count = s.Length;
if (_length > (_chars.Length - count)) {
Grow(count);
}
int remaining = _length - index;
_chars.Slice(index, remaining).CopyTo(_chars.Slice(index + count));
s.AsSpan().CopyTo(_chars.Slice(index));
_length += count;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(char c) {
int pos = _length;
if ((uint) pos < (uint) _chars.Length) {
_chars[pos] = c;
_length = pos + 1;
} else {
GrowAndAppend(c);
}
}
/// <summary>
/// Appends a character without checking if length is enough
/// </summary>
/// <param name="c"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendUnsafe(char c) {
_chars[_length++] = c;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(string? s) {
if (s == null || s.Length == 0) {
return;
}
int pos = _length;
if (s.Length == 1 && (uint) pos < (uint) _chars.Length) // very common case, e.g. appending strings from NumberFormatInfo like separators, percent symbols, etc.
{
_chars[pos] = s[0];
_length = pos + 1;
} else {
AppendSlow(s);
}
}
private void AppendSlow(string s) {
int pos = _length;
if (pos > _chars.Length - s.Length) {
Grow(s.Length);
}
s.AsSpan().CopyTo(_chars.Slice(pos));
_length += s.Length;
}
public void Append(char c, int count) {
if (_length > _chars.Length - count) {
Grow(count);
}
Span<char> dst = _chars.Slice(_length, count);
for (int i = 0; i < dst.Length; i++) {
dst[i] = c;
}
_length += count;
}
public unsafe void Append(char* value, int length) {
int pos = _length;
if (pos > _chars.Length - length) {
Grow(length);
}
Span<char> dst = _chars.Slice(_length, length);
for (int i = 0; i < dst.Length; i++) {
dst[i] = *value++;
}
_length += length;
}
public unsafe int Append(Span<byte> encodedString, Encoding encoding) {
//decode
Span<char> decodedString = stackalloc char[encoding.GetMaxCharCount(encodedString.Length)];
var length = encoding.GetChars(bytes: encodedString, chars: decodedString);
//ensure capacity with decodedString's length
int pos = _length;
if (pos > _chars.Length - length) {
Grow(length);
}
//append by copying
decodedString.Slice(0, length /*excludes \0 at end*/).CopyTo(_chars.Slice(pos));
_length += length;
return length; //return the written span
}
public void RemoveStart(int length) {
length = Math.Min(length, _length);
_chars.Slice(length, _length - length)
.CopyTo(_chars);
_length -= length;
}
public void Clear() {
_length = 0;
}
public Span<char> Append(ReadOnlySpan<char> value) {
int currentLength = _length;
if (currentLength > _chars.Length - value.Length) {
Grow(value.Length);
}
var dst = _chars.Slice(_length, value.Length);
value.CopyTo(dst);
_length += value.Length;
return dst;
}
public Span<char> Append(ReadOnlySpan<char> value, char delimiter) {
int currentLength = _length;
if (currentLength > _chars.Length - value.Length - 1) {
Grow(value.Length + 1);
}
var dst = _chars.Slice(_length, value.Length);
value.CopyTo(dst);
_length += value.Length;
_chars[_length++] = delimiter;
return dst;
}
/// <summary>
/// Appends <paramref name="delimiter"/> and then <paramref name="value"/>
/// </summary>
/// <returns>The span representing what was added</returns>
public void Append(char delimiter, ReadOnlySpan<char> value) {
int currentLength = _length;
if (currentLength > _chars.Length - value.Length - 1) {
Grow(value.Length + 1);
}
_chars[currentLength++] = delimiter;
value.CopyTo(_chars.Slice(currentLength, value.Length));
_length += value.Length + 1;
}
/// <summary>
/// Appends <paramref name="length"/> and returns the target span for writing.
/// </summary>
/// <param name="length">Length of characters</param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<char> AppendSpan(int length) {
int origPos = _length;
if (origPos > _chars.Length - length) {
Grow(length);
}
_length = origPos + length;
return _chars.Slice(origPos, length);
}
/// <summary>
/// Gets the remaining buffer for writing.
/// </summary>
/// <param name="requiredChars"></param>
/// <returns></returns>
/// <remarks>Use <see cref="AddWrittenLength"/> to report the changes written.</remarks>
public Span<char> GetBuffer(int requiredChars) {
EnsureRemainderCapacity(requiredChars);
return _chars.Slice(_length);
}
/// <summary>
/// Gets the remaining buffer for writing.
/// </summary>
/// <remarks>Use <see cref="AddWrittenLength"/> to report the changes written.</remarks>
public Span<char> GetBuffer() {
return _chars.Slice(_length);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public void GrowAndAppend(char c) {
Grow(1);
Append(c);
}
/// <summary>
/// Resize the internal buffer either by doubling current buffer size or
/// by adding <paramref name="additionalCapacityBeyondPos"/> to
/// <see cref="_length"/> whichever is greater.
/// </summary>
/// <param name="additionalCapacityBeyondPos">
/// Number of chars requested beyond current position.
/// </param>
[MethodImpl(MethodImplOptions.NoInlining)]
public void Grow(int additionalCapacityBeyondPos) {
Debug.Assert(additionalCapacityBeyondPos > 0);
Debug.Assert(_length > _chars.Length - additionalCapacityBeyondPos, "Grow called incorrectly, no resize is needed.");
char[] poolArray = ArrayPool<char>.Shared.Rent(Math.Max(_length + additionalCapacityBeyondPos, _chars.Length * 2));
_chars.Slice(0, _length).CopyTo(poolArray);
char[]? toReturn = _arrayToReturnToPool;
_chars = _arrayToReturnToPool = poolArray;
if (toReturn != null) {
ArrayPool<char>.Shared.Return(toReturn);
}
}
/// <summary>
/// Resize the internal buffer either by doubling current buffer size or
/// by adding <paramref name="additionalCapacityBeyondPos"/> to
/// <see cref="_length"/> whichever is greater.
/// </summary>
/// <param name="additionalCapacityBeyondPos">
/// Number of chars requested beyond current position.
/// </param>
[MethodImpl(MethodImplOptions.NoInlining)]
private void GrowAndDisposeLength(int additionalCapacityBeyondPos, int disposableLength) {
Debug.Assert(additionalCapacityBeyondPos > 0);
Debug.Assert(_length > _chars.Length - additionalCapacityBeyondPos, "Grow called incorrectly, no resize is needed.");
char[] poolArray = ArrayPool<char>.Shared.Rent(Math.Max(_length + additionalCapacityBeyondPos, _chars.Length * 2));
_chars.Slice(0, _length).CopyTo(poolArray);
char[]? toReturn = _arrayToReturnToPool;
_chars = _arrayToReturnToPool = poolArray;
if (toReturn != null) {
ArrayPool<char>.Shared.Return(toReturn);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Dispose() {
char[]? toReturn = _arrayToReturnToPool;
this = default; // for safety, to avoid using pooled array if this instance is erroneously appended to again
if (toReturn != null) {
ArrayPool<char>.Shared.Return(toReturn);
}
}
/// <summary>
/// Writes to <see cref="Length"/> to report changes in <see cref="GetBuffer(int)"/>
/// </summary>
/// <param name="chars"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AddWrittenLength(int chars) {
_length += chars;
}
}