Skip to content

Commit 14ef4f0

Browse files
committed
feat: add append(char value)
1 parent 3bc5134 commit 14ef4f0

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
99
## Added
1010

1111
- Support for `net9.0`
12+
- New `Append` overload that accepts a single character
1213

1314
## [1.18.6] - 2023-11-03
1415

src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,29 @@ public unsafe void Append(char* value, int length)
7878
/// Appends a slice of memory.
7979
/// </summary>
8080
/// <param name="memory">The memory to add.</param>
81+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8182
public void Append(ReadOnlyMemory<char> memory)
8283
{
8384
Append(memory.Span);
8485
}
8586

87+
/// <summary>
88+
/// Appends a single character to the string builder.
89+
/// </summary>
90+
/// <param name="value">Character to add.</param>
91+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
92+
public void Append(char value)
93+
{
94+
var newSize = bufferPosition + 1;
95+
if (newSize > buffer.Length)
96+
{
97+
Grow(newSize);
98+
}
99+
100+
buffer[bufferPosition] = value;
101+
bufferPosition++;
102+
}
103+
86104
/// <summary>
87105
/// Adds the default new line separator.
88106
/// </summary>

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,13 @@ public void GivenMultipleValues_WhenCallingAppend_NotCrashing()
196196

197197
builder.ToString().Should().NotBeNull();
198198
}
199+
200+
[Fact]
201+
public void GivenStringBuilder_WhenAddingSingleCharacter_ThenShouldBeAdded()
202+
{
203+
using var builder = new ValueStringBuilder();
204+
builder.Append('c');
205+
206+
builder.ToString().Should().Be("c");
207+
}
199208
}

0 commit comments

Comments
 (0)