Skip to content

Commit 740d4fc

Browse files
author
LinkDotNet Bot
committed
Updating to newest release
2 parents abb1ef4 + a38010d commit 740d4fc

File tree

7 files changed

+38
-29
lines changed

7 files changed

+38
-29
lines changed

CHANGELOG.md

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

77
## [Unreleased]
88

9+
## [1.18.6] - 2023-11-03
10+
11+
### Changed
12+
13+
- `Dispose` resets the `ValueStringBuilder` to the initial state, so it doesn't lead to undefined behavior when used again
14+
- Use different approach for `Grow` to be a bit more performant
15+
916
## [1.18.5] - 2023-10-19
1017

1118
### Changed
@@ -374,7 +381,9 @@ This release brings extensions to the `ValueStringBuilder` API. For `v1.0` the `
374381

375382
- Initial release
376383

377-
[Unreleased]: https://github.com/linkdotnet/StringBuilder/compare/1.18.5...HEAD
384+
[Unreleased]: https://github.com/linkdotnet/StringBuilder/compare/1.18.6...HEAD
385+
386+
[1.18.6]: https://github.com/linkdotnet/StringBuilder/compare/1.18.5...1.18.6
378387

379388
[1.18.5]: https://github.com/linkdotnet/StringBuilder/compare/1.18.4...1.18.5
380389

src/LinkDotNet.StringBuilder/LinkDotNet.StringBuilder.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
</PropertyGroup>
4141

4242
<ItemGroup>
43-
<PackageReference Include="Meziantou.Analyzer" Version="2.0.93">
43+
<PackageReference Include="Meziantou.Analyzer" Version="2.0.103">
4444
<PrivateAssets>all</PrivateAssets>
4545
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
4646
</PackageReference>

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,12 +286,14 @@ public readonly int LastIndexOf(ReadOnlySpan<char> word, int startIndex)
286286
/// Disposes the instance and returns rented buffer from an array pool if needed.
287287
/// </summary>
288288
[MethodImpl(MethodImplOptions.AggressiveInlining)]
289-
public readonly void Dispose()
289+
public void Dispose()
290290
{
291291
if (arrayFromPool != null)
292292
{
293293
ArrayPool<char>.Shared.Return(arrayFromPool);
294294
}
295+
296+
this = default;
295297
}
296298

297299
/// <summary>
@@ -311,7 +313,18 @@ private void Grow(int capacity = 0)
311313
}
312314

313315
var rented = ArrayPool<char>.Shared.Rent(size);
314-
buffer[..bufferPosition].CopyTo(rented);
316+
317+
if (bufferPosition > 0)
318+
{
319+
ref var sourceRef = ref MemoryMarshal.GetReference(buffer);
320+
ref var destinationRef = ref MemoryMarshal.GetReference(rented.AsSpan());
321+
322+
Unsafe.CopyBlock(
323+
ref Unsafe.As<char, byte>(ref destinationRef),
324+
ref Unsafe.As<char, byte>(ref sourceRef),
325+
(uint)(bufferPosition * sizeof(char)));
326+
}
327+
315328
var oldBufferFromPool = arrayFromPool;
316329
buffer = arrayFromPool = rented;
317330

tests/LinkDotNet.StringBuilder.Benchmarks/AppendBenchmark.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,6 @@ namespace LinkDotNet.StringBuilder.Benchmarks;
55
[MemoryDiagnoser]
66
public class AppendBenchmarks
77
{
8-
[Benchmark(Baseline = true)]
9-
public string DotNetStringBuilder()
10-
{
11-
var builder = new System.Text.StringBuilder();
12-
builder.AppendLine("That is the first line of our benchmark.");
13-
builder.AppendLine("We can multiple stuff in here if want.");
14-
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
15-
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
16-
return builder.ToString();
17-
}
18-
198
[Benchmark]
209
public string ValueStringBuilder()
2110
{
@@ -27,16 +16,4 @@ public string ValueStringBuilder()
2716
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
2817
return builder.ToString();
2918
}
30-
31-
[Benchmark]
32-
public string ValueStringBuilderPreAllocated()
33-
{
34-
using var builder = new ValueStringBuilder(stackalloc char[256]);
35-
builder.AppendLine("That is the first line of our benchmark.");
36-
builder.AppendLine("We can multiple stuff in here if want.");
37-
builder.AppendLine("We can multiple stuff in here if want.");
38-
builder.AppendLine("The idea is that we can resize the internal structure from time to time.");
39-
builder.AppendLine("We can also add other Append method if we want. But we keep it easy for now.");
40-
return builder.ToString();
41-
}
4219
}

tests/LinkDotNet.StringBuilder.Benchmarks/LinkDotNet.StringBuilder.Benchmarks.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="BenchmarkDotNet" Version="0.13.9" />
12+
<PackageReference Include="BenchmarkDotNet" Version="0.13.10" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

tests/LinkDotNet.StringBuilder.UnitTests/LinkDotNet.StringBuilder.UnitTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<ItemGroup>
1111
<PackageReference Include="FluentAssertions" Version="6.12.0" />
1212
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
13-
<PackageReference Include="xunit" Version="2.5.3" />
13+
<PackageReference Include="xunit" Version="2.6.1" />
1414
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
1515
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1616
<PrivateAssets>all</PrivateAssets>

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,4 +503,14 @@ public void GivenAString_WhenEnumerating_ThenShouldReturnCharacters()
503503

504504
output.Should().Be("Hello World");
505505
}
506+
507+
[Fact]
508+
public void GivenStringBuilder_WhenDisposed_ThenEmtpyStringReturned()
509+
{
510+
var builder = new ValueStringBuilder("Hello World");
511+
512+
builder.Dispose();
513+
514+
builder.ToString().Should().Be(string.Empty);
515+
}
506516
}

0 commit comments

Comments
 (0)