Skip to content

Commit 389d244

Browse files
committed
Added c'tor with initial buffer
1 parent 8bacbb1 commit 389d244

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
- `ValueStringBuilder` constructor can take initial buffer instead of creating it itself.
11+
912
## [1.1.0] - 2022-04-16
1013

1114
### Added
@@ -87,4 +90,4 @@ This release brings extensions to the `ValueStringBuilder` API. For `v1.0` the `
8790

8891
[1.0.0]: https://github.com/linkdotnet/StringBuilder/compare/0.9.5...1.0.0
8992

90-
[0.9.5]: https://github.com/linkdotnet/StringBuilder/compare/0.9.4...0.9.5
93+
[0.9.5]: https://github.com/linkdotnet/StringBuilder/compare/0.9.4...0.9.5

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public ValueStringBuilder()
2525
buffer = new char[32];
2626
}
2727

28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ValueStringBuilder"/> struct.
30+
/// </summary>
31+
/// <param name="initialBuffer">Initial buffer for the string builder to begin with.</param>
32+
public ValueStringBuilder(Span<char> initialBuffer)
33+
{
34+
bufferPosition = 0;
35+
buffer = initialBuffer;
36+
}
37+
2838
/// <summary>
2939
/// Gets the current length of the represented string.
3040
/// </summary>

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,15 @@ public void ShouldReturnIfStringIsPresent(string word, bool expected)
323323

324324
index.Should().Be(expected);
325325
}
326+
327+
[Fact]
328+
public void ShouldUseInitialBuffer()
329+
{
330+
Span<char> buffer = stackalloc char[16];
331+
var builder = new ValueStringBuilder(buffer);
332+
333+
builder.Append("Hello");
334+
335+
builder.ToString().Should().Be("Hello");
336+
}
326337
}

0 commit comments

Comments
 (0)