Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Common/src/CoreLib/System/Text/ValueStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ public ValueStringBuilder(Span<char> initialBuffer)
_pos = 0;
}

public ValueStringBuilder(int initialCapacity)
{
_arrayToReturnToPool = ArrayPool<char>.Shared.Rent(initialCapacity);
_chars = _arrayToReturnToPool;
_pos = 0;
}

public int Length
{
get => _pos;
Expand Down
52 changes: 52 additions & 0 deletions src/Common/tests/Tests/System/Text/ValueStringBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ public void Ctor_Span_CanAppend()
Assert.Equal("a", vsb.ToString());
}

[Fact]
public void Ctor_InitialCapacity_CanAppend()
{
var vsb = new ValueStringBuilder(1);
Assert.Equal(0, vsb.Length);

vsb.Append('a');
Assert.Equal(1, vsb.Length);
Assert.Equal("a", vsb.ToString());
}

[Fact]
public void Append_Char_MatchesStringBuilder()
{
Expand Down Expand Up @@ -152,6 +163,26 @@ public void Insert_IntCharInt_MatchesStringBuilder()
Assert.Equal(sb.ToString(), vsb.ToString());
}

[Fact]
public void AsSpan_ReturnsCorrectValue_DoesntClearBuilder()
{
var sb = new StringBuilder();
var vsb = new ValueStringBuilder();

for (int i = 1; i <= 100; i++)
{
string s = i.ToString();
sb.Append(s);
vsb.Append(s);
}

var resultString = new string(vsb.AsSpan());
Assert.Equal(sb.ToString(), resultString);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: You could do something like this instead of using the string constructor and storing in a local:

Assert.Equal(sb.ToString(), vsb.AsSpan().ToString());


Assert.NotEqual(0, sb.Length);
Assert.Equal(sb.Length, vsb.Length);
}

[Fact]
public void ToString_ClearsBuilder_ThenReusable()
{
Expand Down Expand Up @@ -213,6 +244,27 @@ public void TryCopyTo_ClearsBuilder_ThenReusable()
Assert.Equal(Text2, vsb.ToString());
}

[Fact]
public void Dispose_ClearsBuilder_ThenReusable()
{
const string Text1 = "test";
var vsb = new ValueStringBuilder();

vsb.Append(Text1);
Assert.Equal(Text1.Length, vsb.Length);

vsb.Dispose();

Assert.Equal(0, vsb.Length);
Assert.Equal(string.Empty, vsb.ToString());
Assert.True(vsb.TryCopyTo(Span<char>.Empty, out _));

const string Text2 = "another test";
vsb.Append(Text2);
Assert.Equal(Text2.Length, vsb.Length);
Assert.Equal(Text2, vsb.ToString());
}

[Fact]
public unsafe void Indexer()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@
<Compile Include="$(CommonPath)\CoreLib\System\Threading\Tasks\TaskToApm.cs">
<Link>Common\CoreLib\System\Threading\Tasks\TaskToApm.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\CoreLib\System\Text\StringBuilderCache.cs">
<Link>Common\System\Text\StringBuilderCache.cs</Link>
</Compile>
<Compile Include="$(CommonPath)\System\HResults.cs">
<Link>Common\System\HResults.cs</Link>
</Compile>
Expand Down
Loading