1+ namespace LinkDotNet . StringBuilder ;
2+
3+ public ref partial struct ValueStringBuilder
4+ {
5+ /// <summary>
6+ /// Insert the string representation of the boolean to the builder at the given index.
7+ /// </summary>
8+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
9+ /// <param name="value">String to insert into this builder.</param>
10+ public void Insert ( int index , bool value ) => Insert ( index , value . ToString ( ) ) ;
11+
12+ /// <summary>
13+ /// Insert the string representation of the char to the builder at the given index.
14+ /// </summary>
15+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
16+ /// <param name="value">String to insert into this builder.</param>
17+ public void Insert ( int index , char value ) => InsertSpanFormattable ( index , value ) ;
18+
19+ /// <summary>
20+ /// Insert the string representation of the signed byte to the builder at the given index.
21+ /// </summary>
22+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
23+ /// <param name="value">String to insert into this builder.</param>
24+ public void Insert ( int index , sbyte value ) => InsertSpanFormattable ( index , value ) ;
25+
26+ /// <summary>
27+ /// Insert the string representation of the byte to the builder at the given index.
28+ /// </summary>
29+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
30+ /// <param name="value">String to insert into this builder.</param>
31+ public void Insert ( int index , byte value ) => InsertSpanFormattable ( index , value ) ;
32+
33+ /// <summary>
34+ /// Insert the string representation of the short 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">String to insert into this builder.</param>
38+ public void Insert ( int index , short value ) => InsertSpanFormattable ( index , value ) ;
39+
40+ /// <summary>
41+ /// Insert the string representation of the integer to the builder at the given index.
42+ /// </summary>
43+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
44+ /// <param name="value">String to insert into this builder.</param>
45+ public void Insert ( int index , int value ) => InsertSpanFormattable ( index , value ) ;
46+
47+ /// <summary>
48+ /// Insert the string representation of the long to the builder at the given index.
49+ /// </summary>
50+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
51+ /// <param name="value">String to insert into this builder.</param>
52+ public void Insert ( int index , long value ) => InsertSpanFormattable ( index , value ) ;
53+
54+ /// <summary>
55+ /// Insert the string representation of the float to the builder at the given index.
56+ /// </summary>
57+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
58+ /// <param name="value">String to insert into this builder.</param>
59+ public void Insert ( int index , float value ) => InsertSpanFormattable ( index , value ) ;
60+
61+ /// <summary>
62+ /// Insert the string representation of the double to the builder at the given index.
63+ /// </summary>
64+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
65+ /// <param name="value">String to insert into this builder.</param>
66+ public void Insert ( int index , double value ) => InsertSpanFormattable ( index , value ) ;
67+
68+ /// <summary>
69+ /// Insert the string representation of the decimal to the builder at the given index.
70+ /// </summary>
71+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
72+ /// <param name="value">String to insert into this builder.</param>
73+ public void Insert ( int index , decimal value ) => InsertSpanFormattable ( index , value ) ;
74+
75+ /// <summary>
76+ /// Appends the string representation of the boolean to the builder.
77+ /// </summary>
78+ /// <param name="index">Index where <paramref name="value"/> should be inserted.</param>
79+ /// <param name="value">String to insert into this builder.</param>
80+ public void Insert ( int index , ReadOnlySpan < char > value )
81+ {
82+ if ( index < 0 )
83+ {
84+ throw new ArgumentOutOfRangeException ( nameof ( index ) , "The given index can't be negative." ) ;
85+ }
86+
87+ if ( index > bufferPosition )
88+ {
89+ throw new ArgumentOutOfRangeException ( nameof ( index ) , "The given index can't be bigger than the string itself." ) ;
90+ }
91+
92+ bufferPosition += value . Length ;
93+ if ( bufferPosition > buffer . Length )
94+ {
95+ Grow ( bufferPosition * 2 ) ;
96+ }
97+
98+ // Move Slice at beginning index
99+ var oldPosition = bufferPosition - value . Length ;
100+ var shift = index + value . Length ;
101+ buffer [ index ..oldPosition ] . CopyTo ( buffer [ shift ..bufferPosition ] ) ;
102+
103+ // Add new word
104+ value . CopyTo ( buffer [ index ..shift ] ) ;
105+ }
106+
107+ private void InsertSpanFormattable < T > ( int index , T value )
108+ where T : ISpanFormattable
109+ {
110+ if ( index < 0 )
111+ {
112+ throw new ArgumentOutOfRangeException ( nameof ( index ) , "The given index can't be negative." ) ;
113+ }
114+
115+ if ( index > bufferPosition )
116+ {
117+ throw new ArgumentOutOfRangeException ( nameof ( index ) , "The given index can't be bigger than the string itself." ) ;
118+ }
119+
120+ Span < char > tempBuffer = stackalloc char [ 24 ] ;
121+ if ( value . TryFormat ( tempBuffer , out var written , default , null ) )
122+ {
123+ bufferPosition += written ;
124+ if ( bufferPosition > buffer . Length )
125+ {
126+ Grow ( bufferPosition * 2 ) ;
127+ }
128+
129+ // Move Slice at beginning index
130+ var oldPosition = bufferPosition - written ;
131+ var shift = index + written ;
132+ buffer [ index ..oldPosition ] . CopyTo ( buffer [ shift ..bufferPosition ] ) ;
133+
134+ // Add new word
135+ tempBuffer [ ..written ] . CopyTo ( buffer [ index ..shift ] ) ;
136+ }
137+ }
138+ }
0 commit comments