-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathValueStringBuilder.Append.cs
200 lines (176 loc) · 6.26 KB
/
ValueStringBuilder.Append.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
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace LinkDotNet.StringBuilder;
public ref partial struct ValueStringBuilder
{
/// <summary>
/// Appends the string representation of the boolean.
/// </summary>
/// <param name="value">Bool value to add.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Append(bool value)
{
const int trueLength = 4;
const int falseLength = 5;
var newSize = bufferPosition + falseLength;
if (newSize > buffer.Length)
{
EnsureCapacity(newSize);
}
fixed (char* dest = &buffer[bufferPosition])
{
if (value)
{
*(dest + 0) = 'T';
*(dest + 1) = 'r';
*(dest + 2) = 'u';
*(dest + 3) = 'e';
bufferPosition += trueLength;
}
else
{
*(dest + 0) = 'F';
*(dest + 1) = 'a';
*(dest + 2) = 'l';
*(dest + 3) = 's';
*(dest + 4) = 'e';
bufferPosition += falseLength;
}
}
}
/// <summary>
/// Appends the string representation of the value.
/// </summary>
/// <param name="value">Formattable span to add.</param>
/// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
/// <param name="bufferSize">Size of the buffer allocated. If you have a custom type that implements <see cref="ISpanFormattable"/> that
/// requires more space than the default (36 characters), adjust the value.</param>
/// <typeparam name="T">Any <see cref="ISpanFormattable"/>.</typeparam>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append<T>(T value, ReadOnlySpan<char> format = default, int bufferSize = 36)
where T : ISpanFormattable => AppendSpanFormattable(value, format, bufferSize);
/// <summary>
/// Appends a string.
/// </summary>
/// <param name="str">String to be added to this builder.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(scoped ReadOnlySpan<char> str)
{
if (str.IsEmpty)
{
return;
}
var newSize = str.Length + bufferPosition;
if (newSize > buffer.Length)
{
EnsureCapacity(newSize);
}
ref var strRef = ref MemoryMarshal.GetReference(str);
ref var bufferRef = ref MemoryMarshal.GetReference(buffer[bufferPosition..]);
Unsafe.CopyBlock(
ref Unsafe.As<char, byte>(ref bufferRef),
ref Unsafe.As<char, byte>(ref strRef),
(uint)(str.Length * sizeof(char)));
bufferPosition += str.Length;
}
/// <summary>
/// Appends a character buffer.
/// </summary>
/// <param name="value">The pointer to the start of the buffer.</param>
/// <param name="length">The number of characters in the buffer.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe void Append(char* value, int length)
{
Append(new ReadOnlySpan<char>(value, length));
}
/// <summary>
/// Appends a slice of memory.
/// </summary>
/// <param name="memory">The memory to add.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(ReadOnlyMemory<char> memory)
{
Append(memory.Span);
}
/// <summary>
/// Appends a single character.
/// </summary>
/// <param name="value">Character to add.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(char value)
{
var newSize = bufferPosition + 1;
if (newSize > buffer.Length)
{
EnsureCapacity(newSize);
}
buffer[bufferPosition] = value;
bufferPosition++;
}
/// <summary>
/// Appends a single rune to the string builder.
/// </summary>
/// <param name="value">Rune to add.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Append(Rune value)
{
Span<char> valueChars = stackalloc char[2];
var valueCharsWritten = value.EncodeToUtf16(valueChars);
ReadOnlySpan<char> valueCharsSlice = valueChars[..valueCharsWritten];
Append(valueCharsSlice);
}
/// <summary>
/// Appends <see cref="Environment.NewLine"/>.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine()
{
Append(Environment.NewLine);
}
/// <summary>
/// Calls <see cref="Append(ReadOnlySpan{char})"/> and appends <see cref="Environment.NewLine"/>.
/// </summary>
/// <param name="str">String to be added to this builder.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void AppendLine(scoped ReadOnlySpan<char> str)
{
Append(str);
Append(Environment.NewLine);
}
/// <summary>
/// Appends a span of the given length, which can be written to later.
/// </summary>
/// <param name="length">Integer representing the number of characters to be appended.</param>
/// <returns>A span with the characters appended.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<char> AppendSpan(int length)
{
if (length == 0)
{
return [];
}
var origPos = bufferPosition;
if (origPos > buffer.Length - length)
{
EnsureCapacity(length);
}
bufferPosition = origPos + length;
return buffer.Slice(origPos, length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AppendSpanFormattable<T>(T value, ReadOnlySpan<char> format = default, int bufferSize = 36)
where T : ISpanFormattable
{
var newSize = bufferSize + bufferPosition;
if (newSize >= Capacity)
{
EnsureCapacity(newSize);
}
if (!value.TryFormat(buffer[bufferPosition..], out var written, format, null))
{
throw new InvalidOperationException($"Could not insert {value} into given buffer. Is the buffer (size: {bufferSize}) large enough?");
}
bufferPosition += written;
}
}