@@ -16,91 +16,13 @@ public ref partial struct ValueStringBuilder
1616 /// Insert the string representation of the char to the builder at the given index.
1717 /// </summary>
1818 /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
19- /// <param name="value">Character to insert into this builder.</param>
19+ /// <param name="value">Formattable span to insert into this builder.</param>
2020 /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
21+ /// <param name="bufferSize">Size of the buffer allocated on the stack.</param>
22+ /// <typeparam name="T">Any <see cref="ISpanFormattable"/>.</typeparam>
2123 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
22- public void Insert ( int index , char value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
23-
24- /// <summary>
25- /// Insert the string representation of the signed byte to the builder at the given index.
26- /// </summary>
27- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
28- /// <param name="value">Signed byte to insert into this builder.</param>
29- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
30- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
31- public void Insert ( int index , sbyte value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
32-
33- /// <summary>
34- /// Insert the string representation of the byte to the builder at the given index.
35- /// </summary>
36- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
37- /// <param name="value">Byte to insert into this builder.</param>
38- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
39- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
40- public void Insert ( int index , byte value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
41-
42- /// <summary>
43- /// Insert the string representation of the short to the builder at the given index.
44- /// </summary>
45- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
46- /// <param name="value">Short to insert into this builder.</param>
47- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
48- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
49- public void Insert ( int index , short value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
50-
51- /// <summary>
52- /// Insert the string representation of the integer to the builder at the given index.
53- /// </summary>
54- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
55- /// <param name="value">Integer to insert into this builder.</param>
56- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
57- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
58- public void Insert ( int index , int value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
59-
60- /// <summary>
61- /// Insert the string representation of the long to the builder at the given index.
62- /// </summary>
63- /// <param name="index">Long where <paramref name="value"/> should be inserted.</param>
64- /// <param name="value">String to insert into this builder.</param>
65- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
66- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
67- public void Insert ( int index , long value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
68-
69- /// <summary>
70- /// Insert the string representation of the float to the builder at the given index.
71- /// </summary>
72- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
73- /// <param name="value">Float to insert into this builder.</param>
74- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
75- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
76- public void Insert ( int index , float value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
77-
78- /// <summary>
79- /// Insert the string representation of the double to the builder at the given index.
80- /// </summary>
81- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
82- /// <param name="value">Double to insert into this builder.</param>
83- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
84- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
85- public void Insert ( int index , double value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
86-
87- /// <summary>
88- /// Insert the string representation of the decimal to the builder at the given index.
89- /// </summary>
90- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
91- /// <param name="value">Decimal to insert into this builder.</param>
92- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
93- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
94- public void Insert ( int index , decimal value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
95-
96- /// <summary>
97- /// Insert the string representation of the Guid to the builder at the given index.
98- /// </summary>
99- /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
100- /// <param name="value">Guid to insert into this builder.</param>
101- /// <param name="format">Optional formatter. If not provided the default of the given instance is taken.</param>
102- [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
103- public void Insert ( int index , Guid value , ReadOnlySpan < char > format = default ) => InsertSpanFormattable ( index , value , format ) ;
24+ public void Insert < T > ( int index , T value , ReadOnlySpan < char > format = default , int bufferSize = 36 )
25+ where T : ISpanFormattable => InsertSpanFormattable ( index , value , format , bufferSize ) ;
10426
10527 /// <summary>
10628 /// Appends the string representation of the boolean to the builder.
@@ -136,7 +58,7 @@ public void Insert(int index, scoped ReadOnlySpan<char> value)
13658 }
13759
13860 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
139- private void InsertSpanFormattable < T > ( int index , T value , ReadOnlySpan < char > format = default )
61+ private void InsertSpanFormattable < T > ( int index , T value , ReadOnlySpan < char > format , int bufferSize )
14062 where T : ISpanFormattable
14163 {
14264 if ( index < 0 )
@@ -149,7 +71,7 @@ private void InsertSpanFormattable<T>(int index, T value, ReadOnlySpan<char> for
14971 throw new ArgumentOutOfRangeException ( nameof ( index ) , "The given index can't be bigger than the string itself." ) ;
15072 }
15173
152- Span < char > tempBuffer = stackalloc char [ 36 ] ;
74+ Span < char > tempBuffer = stackalloc char [ bufferSize ] ;
15375 if ( value . TryFormat ( tempBuffer , out var written , format , null ) )
15476 {
15577 bufferPosition += written ;
@@ -166,5 +88,9 @@ private void InsertSpanFormattable<T>(int index, T value, ReadOnlySpan<char> for
16688 // Add new word
16789 tempBuffer [ ..written ] . CopyTo ( buffer [ index ..shift ] ) ;
16890 }
91+ else
92+ {
93+ throw new InvalidOperationException ( $ "Could not insert { value } into given buffer. Is the buffer large enough?") ;
94+ }
16995 }
17096}
0 commit comments