Skip to content

Commit 4c96dc5

Browse files
committed
New ToString(Range) method
1 parent 98938f0 commit 4c96dc5

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
88

99
### Added
1010

11-
- New overload for `Append` that accepts a `ReadOnlyMemory`object
11+
- New overload for `Append` that accepts a `ReadOnlyMemory` object
12+
- New `ToString` overload that accepts a `Range` object
1213

1314
### Changed
1415

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,17 @@ public ValueStringBuilder(ReadOnlySpan<char> initialText)
109109
[MethodImpl(MethodImplOptions.AggressiveInlining)]
110110
public readonly string ToString(int startIndex, int length) => new(buffer[startIndex..(startIndex + length)]);
111111

112+
/// <summary>
113+
/// Creates a <see cref="string"/> instance from that builder in the given range.
114+
/// </summary>
115+
/// <param name="range">The range that will be retrieved.</param>
116+
/// <returns>The <see cref="string"/> instance.</returns>
117+
public readonly string ToString(Range range)
118+
{
119+
var (offset, length) = range.GetOffsetAndLength(bufferPosition);
120+
return new string(buffer.Slice(offset, length));
121+
}
122+
112123
/// <summary>
113124
/// Returns the string as an <see cref="ReadOnlySpan{T}"/>.
114125
/// </summary>

tests/LinkDotNet.StringBuilder.UnitTests/ValueStringBuilderTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,12 @@ public void ShouldReverseString()
481481

482482
builder.ToString().Should().Be("olleH");
483483
}
484+
485+
[Fact]
486+
public void GivenAString_WhenCallingToStringWithRange_ThenShouldReturnSubstring()
487+
{
488+
using var builder = new ValueStringBuilder("Hello World");
489+
490+
builder.ToString(1..4).Should().Be("ell");
491+
}
484492
}

0 commit comments

Comments
 (0)