Skip to content

Commit e9b83c2

Browse files
authored
Add AsSpan overloads (#239)
* Add AsSpan overloads * Update CHANGELOG.md
1 parent 21d99d2 commit e9b83c2

File tree

2 files changed

+55
-12
lines changed

2 files changed

+55
-12
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ All notable changes to **ValueStringBuilder** will be documented in this file. T
66

77
## [Unreleased]
88

9+
### Added
10+
11+
- Added `ToString(int)` (by @Joy-less in #239)
12+
- Added `AsSpan(int)`, `AsSpan(int, int)`, `AsSpan(Range)` (by @Joy-less in #239)
13+
914
### Changed
1015

1116
- Optimized and simplified `Replace` (by @Joy-less in #238)

src/LinkDotNet.StringBuilder/ValueStringBuilder.cs

+50-12
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public ValueStringBuilder(int initialCapacity)
7070
public readonly int Length => bufferPosition;
7171

7272
/// <summary>
73-
/// Gets the current maximum capacity before growing the array.
73+
/// Gets the current maximum capacity before the span must be resized.
7474
/// </summary>
7575
/// <value>
76-
/// The current maximum capacity before growing the array.
76+
/// The current maximum capacity before the span must be resized.
7777
/// </value>
7878
public readonly int Capacity => buffer.Length;
7979

@@ -100,39 +100,77 @@ public ValueStringBuilder(int initialCapacity)
100100
#pragma warning restore CA2225
101101

102102
/// <summary>
103-
/// Creates a <see cref="string"/> instance from that builder.
103+
/// Creates a <see cref="string"/> instance from the builder.
104104
/// </summary>
105105
/// <returns>The <see cref="string"/> instance.</returns>
106106
[MethodImpl(MethodImplOptions.AggressiveInlining)]
107-
public readonly override string ToString() => new(buffer[..bufferPosition]);
107+
public readonly override string ToString() => AsSpan().ToString();
108108

109109
/// <summary>
110-
/// Creates a <see cref="string"/> instance from that builder.
110+
/// Creates a <see cref="string"/> instance from the builder.
111+
/// </summary>
112+
/// <param name="startIndex">The starting position of the substring in this instance.</param>
113+
/// <returns>The <see cref="string"/> instance.</returns>
114+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
115+
public readonly string ToString(int startIndex) => AsSpan(startIndex).ToString();
116+
117+
/// <summary>
118+
/// Creates a <see cref="string"/> instance from the builder.
111119
/// </summary>
112120
/// <param name="startIndex">The starting position of the substring in this instance.</param>
113121
/// <param name="length">The length of the substring.</param>
114122
/// <returns>The <see cref="string"/> instance.</returns>
115123
[MethodImpl(MethodImplOptions.AggressiveInlining)]
116-
public readonly string ToString(int startIndex, int length) => new(buffer[startIndex..(startIndex + length)]);
124+
public readonly string ToString(int startIndex, int length) => AsSpan(startIndex, length).ToString();
117125

118126
/// <summary>
119-
/// Creates a <see cref="string"/> instance from that builder in the given range.
127+
/// Creates a <see cref="string"/> instance from the builder in the given range.
120128
/// </summary>
121-
/// <param name="range">The range that will be retrieved.</param>
129+
/// <param name="range">The range to be retrieved.</param>
122130
/// <returns>The <see cref="string"/> instance.</returns>
123131
[MethodImpl(MethodImplOptions.AggressiveInlining)]
124-
public readonly string ToString(Range range)
132+
public readonly string ToString(Range range) => AsSpan(range).ToString();
133+
134+
/// <summary>
135+
/// Returns the string as an <see cref="ReadOnlySpan{T}"/>.
136+
/// </summary>
137+
/// <returns>The filled array as <see cref="ReadOnlySpan{T}"/>.</returns>
138+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
139+
public readonly ReadOnlySpan<char> AsSpan() => buffer[..bufferPosition];
140+
141+
/// <summary>
142+
/// Returns the string as an <see cref="ReadOnlySpan{T}"/>.
143+
/// </summary>
144+
/// <param name="startIndex">The starting position of the substring in this instance.</param>
145+
/// <returns>The filled array as <see cref="ReadOnlySpan{T}"/>.</returns>
146+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
147+
public readonly ReadOnlySpan<char> AsSpan(int startIndex) => buffer[startIndex..bufferPosition];
148+
149+
/// <summary>
150+
/// Returns the string as an <see cref="ReadOnlySpan{T}"/>.
151+
/// </summary>
152+
/// <param name="startIndex">The starting position of the substring in this instance.</param>
153+
/// <param name="length">The length of the substring.</param>
154+
/// <returns>The filled array as <see cref="ReadOnlySpan{T}"/>.</returns>
155+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
156+
public readonly ReadOnlySpan<char> AsSpan(int startIndex, int length)
125157
{
126-
var (offset, length) = range.GetOffsetAndLength(bufferPosition);
127-
return new string(buffer.Slice(offset, length));
158+
ArgumentOutOfRangeException.ThrowIfGreaterThan(length, bufferPosition);
159+
160+
return buffer.Slice(startIndex, length);
128161
}
129162

130163
/// <summary>
131164
/// Returns the string as an <see cref="ReadOnlySpan{T}"/>.
132165
/// </summary>
166+
/// <param name="range">The range to be retrieved.</param>
133167
/// <returns>The filled array as <see cref="ReadOnlySpan{T}"/>.</returns>
134168
[MethodImpl(MethodImplOptions.AggressiveInlining)]
135-
public readonly ReadOnlySpan<char> AsSpan() => buffer[..bufferPosition];
169+
public readonly ReadOnlySpan<char> AsSpan(Range range)
170+
{
171+
var (offset, length) = range.GetOffsetAndLength(bufferPosition);
172+
return AsSpan(offset, length);
173+
}
136174

137175
/// <summary>
138176
/// Gets a pinnable reference to the represented string from this builder.

0 commit comments

Comments
 (0)