@@ -70,10 +70,10 @@ public ValueStringBuilder(int initialCapacity)
70
70
public readonly int Length => bufferPosition;
71
71
72
72
/// <summary>
73
- /// Gets the current maximum capacity before growing the array .
73
+ /// Gets the current maximum capacity before the span must be resized .
74
74
/// </summary>
75
75
/// <value>
76
- /// The current maximum capacity before growing the array .
76
+ /// The current maximum capacity before the span must be resized .
77
77
/// </value>
78
78
public readonly int Capacity => buffer. Length;
79
79
@@ -100,39 +100,77 @@ public ValueStringBuilder(int initialCapacity)
100
100
#pragma warning restore CA2225
101
101
102
102
/// <summary>
103
- /// Creates a <see cref="string"/> instance from that builder.
103
+ /// Creates a <see cref="string"/> instance from the builder.
104
104
/// </summary>
105
105
/// <returns>The <see cref="string"/> instance.</returns>
106
106
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
107
- public readonly override string ToString ( ) => new ( buffer [ .. bufferPosition ] ) ;
107
+ public readonly override string ToString ( ) => AsSpan ( ) . ToString ( ) ;
108
108
109
109
/// <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.
111
119
/// </summary>
112
120
/// <param name="startIndex">The starting position of the substring in this instance.</param>
113
121
/// <param name="length">The length of the substring.</param>
114
122
/// <returns>The <see cref="string"/> instance.</returns>
115
123
[ 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 ( ) ;
117
125
118
126
/// <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.
120
128
/// </summary>
121
- /// <param name="range">The range that will be retrieved.</param>
129
+ /// <param name="range">The range to be retrieved.</param>
122
130
/// <returns>The <see cref="string"/> instance.</returns>
123
131
[ 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 )
125
157
{
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 ) ;
128
161
}
129
162
130
163
/// <summary>
131
164
/// Returns the string as an <see cref="ReadOnlySpan{T}"/>.
132
165
/// </summary>
166
+ /// <param name="range">The range to be retrieved.</param>
133
167
/// <returns>The filled array as <see cref="ReadOnlySpan{T}"/>.</returns>
134
168
[ 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
+ }
136
174
137
175
/// <summary>
138
176
/// Gets a pinnable reference to the represented string from this builder.
0 commit comments