Skip to content

Commit 718e283

Browse files
committed
Added the scoped keyword to simplify code
1 parent 0cbc293 commit 718e283

File tree

4 files changed

+10
-11
lines changed

4 files changed

+10
-11
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
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+
- Added the `scoped` keyword to simplify code and allow more scenarios for the user
11+
912
## [1.3.0] - 2022-07-25
1013

1114
### Fixed

src/LinkDotNet.StringBuilder/ValueStringBuilder.Append.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public ref partial struct ValueStringBuilder
8080
/// </summary>
8181
/// <param name="str">String, which will be added to this builder.</param>
8282
[MethodImpl(MethodImplOptions.AggressiveInlining)]
83-
public void Append(ReadOnlySpan<char> str)
83+
public void Append(scoped ReadOnlySpan<char> str)
8484
{
8585
var newSize = str.Length + bufferPosition;
8686
if (newSize > buffer.Length)
@@ -106,7 +106,7 @@ public void AppendLine()
106106
/// </summary>
107107
/// <param name="str">String, which will be added to this builder.</param>
108108
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109-
public void AppendLine(ReadOnlySpan<char> str)
109+
public void AppendLine(scoped ReadOnlySpan<char> str)
110110
{
111111
Append(str);
112112
Append(Environment.NewLine);

src/LinkDotNet.StringBuilder/ValueStringBuilder.Insert.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public ref partial struct ValueStringBuilder
9090
/// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
9191
/// <param name="value">String to insert into this builder.</param>
9292
[MethodImpl(MethodImplOptions.AggressiveInlining)]
93-
public void Insert(int index, ReadOnlySpan<char> value)
93+
public void Insert(int index, scoped ReadOnlySpan<char> value)
9494
{
9595
if (index < 0)
9696
{

src/LinkDotNet.StringBuilder/ValueStringBuilder.Replace.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.Buffers;
2-
using System.Runtime.CompilerServices;
1+
using System.Runtime.CompilerServices;
32

43
namespace LinkDotNet.StringBuilder;
54

@@ -86,14 +85,11 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
8685
{
8786
if (newValue is ISpanFormattable spanFormattable)
8887
{
89-
// Maybe we want to have a stackalloc here and more or less inline the whole function
90-
var tempBuffer = ArrayPool<char>.Shared.Rent(24);
88+
Span<char> tempBuffer = stackalloc char[24];
9189
if (spanFormattable.TryFormat(tempBuffer, out var written, default, null))
9290
{
93-
Replace(oldValue, tempBuffer.AsSpan(0, written), startIndex, count);
91+
Replace(oldValue, tempBuffer[..written], startIndex, count);
9492
}
95-
96-
ArrayPool<char>.Shared.Return(tempBuffer);
9793
}
9894
else
9995
{
@@ -113,7 +109,7 @@ public void ReplaceGeneric<T>(ReadOnlySpan<char> oldValue, T newValue, int start
113109
/// are removed from this builder.
114110
/// </remarks>
115111
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116-
public void Replace(ReadOnlySpan<char> oldValue, ReadOnlySpan<char> newValue, int startIndex, int count)
112+
public void Replace(scoped ReadOnlySpan<char> oldValue, scoped ReadOnlySpan<char> newValue, int startIndex, int count)
117113
{
118114
var length = startIndex + count;
119115
var slice = buffer[startIndex..length];

0 commit comments

Comments
 (0)