Skip to content

Commit 710a63c

Browse files
committed
Better implementation for Append(bool) and new method
1 parent 5b6d46d commit 710a63c

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,22 @@ public ref partial struct ValueStringBuilder
99
/// </summary>
1010
/// <param name="value">Bool value to add.</param>
1111
[MethodImpl(MethodImplOptions.AggressiveInlining)]
12-
public void Append(bool value) => Append(value.ToString());
12+
public void Append(bool value)
13+
{
14+
// 5 is the length of the string "False"
15+
// So we can check if we have enough space in the buffer
16+
if (bufferPosition + 5 > buffer.Length)
17+
{
18+
Grow(bufferPosition * 2);
19+
}
20+
21+
if (!value.TryFormat(buffer[bufferPosition..], out var charsWritten))
22+
{
23+
throw new InvalidOperationException($"Could not add {value} to the builder.");
24+
}
25+
26+
bufferPosition += charsWritten;
27+
}
1328

1429
/// <summary>
1530
/// Appends the string representation of the character to the builder.
@@ -60,6 +75,15 @@ public void AppendLine()
6075
Append(Environment.NewLine);
6176
}
6277

78+
/// <summary>
79+
/// Appends a slice of memory.
80+
/// </summary>
81+
/// <param name="memory">The memory to add.</param>
82+
public void Append(ReadOnlyMemory<char> memory)
83+
{
84+
Append(memory.Span);
85+
}
86+
6387
/// <summary>
6488
/// Does the same as <see cref="Append(char)"/> but adds a newline at the end.
6589
/// </summary>

tests/LinkDotNet.StringBuilder.Benchmarks/AppendFormatBenchmark.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,31 @@ namespace LinkDotNet.StringBuilder.Benchmarks;
55
[MemoryDiagnoser]
66
public class AppendFormatBenchmark
77
{
8-
[Benchmark(Baseline = true)]
9-
public string DotNetStringBuilderAppendFormat()
8+
[Benchmark]
9+
public string ValueStringBuilderAppendFormat1()
1010
{
11-
var builder = new System.Text.StringBuilder();
12-
for (var i = 0; i < 10; i++)
11+
using var builder = new ValueStringBuilder();
12+
for (var i = 0; i < 100; i++)
1313
{
14-
builder.AppendFormat("Hello {0} dear {1}. {2}", 2, "world", 30);
14+
builder.Append(true);
15+
builder.Append(false);
16+
builder.Append(true);
17+
builder.Append(false);
1518
}
1619

1720
return builder.ToString();
1821
}
1922

2023
[Benchmark]
21-
public string ValueStringBuilderAppendFormat()
24+
public string ValueStringBuilderAppendFormat2()
2225
{
2326
using var builder = new ValueStringBuilder();
24-
for (var i = 0; i < 10; i++)
27+
for (var i = 0; i < 100; i++)
2528
{
26-
builder.AppendFormat("Hello {0} dear {1}. {2}", 2, "world", 30);
29+
builder.Append(true);
30+
builder.Append(false);
31+
builder.Append(true);
32+
builder.Append(false);
2733
}
2834

2935
return builder.ToString();

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilder.Append.Tests.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,17 @@ public unsafe void ShouldAddCharPointer()
155155

156156
builder.ToString().Should().Be("Hello");
157157
}
158+
159+
[Fact]
160+
public void GivenMemorySlice_ShouldAppend()
161+
{
162+
using var builder = new ValueStringBuilder();
163+
var memory = new Memory<char>(new char[100]);
164+
var slice = memory[..5];
165+
slice.Span.Fill('c');
166+
167+
builder.Append(slice);
168+
169+
builder.ToString().Should().Be("ccccc");
170+
}
158171
}

0 commit comments

Comments
 (0)