@@ -60,9 +60,9 @@ static int Write7BitEncodedInteger(ref SpanWriter<byte> writer, int value)
60
60
61
61
internal static int WriteLength ( this IBufferWriter < byte > buffer , int length , LengthFormat lengthFormat )
62
62
{
63
- var writer = new SpanWriter < byte > ( buffer . GetSpan ( SevenBitEncodedInt . MaxSizeInBytes ) ) ;
64
- buffer . Advance ( writer . WriteLength ( length , lengthFormat ) ) ;
65
- return writer . WrittenCount ;
63
+ var bytesWritten = WriteLength ( buffer . GetSpan ( SevenBitEncodedInt . MaxSizeInBytes ) , length , lengthFormat ) ;
64
+ buffer . Advance ( bytesWritten ) ;
65
+ return bytesWritten ;
66
66
}
67
67
68
68
internal static int WriteLength ( Span < byte > buffer , int length , LengthFormat lengthFormat )
@@ -71,21 +71,28 @@ internal static int WriteLength(Span<byte> buffer, int length, LengthFormat leng
71
71
return writer . WriteLength ( length , lengthFormat ) ;
72
72
}
73
73
74
+ private static int WriteLength ( this ref BufferWriterSlim < byte > buffer , int length , LengthFormat lengthFormat )
75
+ {
76
+ var bytesWritten = WriteLength ( buffer . GetSpan ( SevenBitEncodedInt . MaxSizeInBytes ) , length , lengthFormat ) ;
77
+ buffer . Advance ( bytesWritten ) ;
78
+ return bytesWritten ;
79
+ }
80
+
74
81
/// <summary>
75
82
/// Encodes string using the specified encoding.
76
83
/// </summary>
77
84
/// <param name="writer">The buffer writer.</param>
78
- /// <param name="value ">The sequence of characters.</param>
85
+ /// <param name="chars ">The sequence of characters.</param>
79
86
/// <param name="context">The encoding context.</param>
80
87
/// <param name="lengthFormat">String length encoding format; or <see langword="null"/> to prevent encoding of string length.</param>
81
88
/// <returns>The number of written bytes.</returns>
82
- public static long Encode ( this IBufferWriter < byte > writer , ReadOnlySpan < char > value , in EncodingContext context , LengthFormat ? lengthFormat = null )
89
+ public static long Encode ( this IBufferWriter < byte > writer , ReadOnlySpan < char > chars , in EncodingContext context , LengthFormat ? lengthFormat = null )
83
90
{
84
91
var result = lengthFormat . HasValue
85
- ? writer . WriteLength ( context . Encoding . GetByteCount ( value ) , lengthFormat . GetValueOrDefault ( ) )
92
+ ? writer . WriteLength ( context . Encoding . GetByteCount ( chars ) , lengthFormat . GetValueOrDefault ( ) )
86
93
: 0L ;
87
94
88
- context . GetEncoder ( ) . Convert ( value , writer , true , out var bytesWritten , out _ ) ;
95
+ context . GetEncoder ( ) . Convert ( chars , writer , true , out var bytesWritten , out _ ) ;
89
96
result += bytesWritten ;
90
97
91
98
return result ;
@@ -95,19 +102,19 @@ public static long Encode(this IBufferWriter<byte> writer, ReadOnlySpan<char> va
95
102
/// Encodes string using the specified encoding.
96
103
/// </summary>
97
104
/// <param name="writer">The buffer writer.</param>
98
- /// <param name="value ">The sequence of characters.</param>
105
+ /// <param name="chars ">The sequence of characters.</param>
99
106
/// <param name="context">The encoding context.</param>
100
107
/// <param name="lengthFormat">String length encoding format; or <see langword="null"/> to prevent encoding of string length.</param>
101
108
/// <returns>The number of written bytes.</returns>
102
- public static int Encode ( this ref SpanWriter < byte > writer , ReadOnlySpan < char > value , in EncodingContext context , LengthFormat ? lengthFormat = null )
109
+ public static int Encode ( this ref SpanWriter < byte > writer , scoped ReadOnlySpan < char > chars , in EncodingContext context , LengthFormat ? lengthFormat = null )
103
110
{
104
111
var result = lengthFormat . HasValue
105
- ? writer . WriteLength ( context . Encoding . GetByteCount ( value ) , lengthFormat . GetValueOrDefault ( ) )
112
+ ? writer . WriteLength ( context . Encoding . GetByteCount ( chars ) , lengthFormat . GetValueOrDefault ( ) )
106
113
: 0 ;
107
114
108
115
var bytesWritten = context . TryGetEncoder ( ) is { } encoder
109
- ? encoder . GetBytes ( value , writer . RemainingSpan , flush : true )
110
- : context . Encoding . GetBytes ( value , writer . RemainingSpan ) ;
116
+ ? encoder . GetBytes ( chars , writer . RemainingSpan , flush : true )
117
+ : context . Encoding . GetBytes ( chars , writer . RemainingSpan ) ;
111
118
result += bytesWritten ;
112
119
writer . Advance ( bytesWritten ) ;
113
120
@@ -128,6 +135,66 @@ public static int Write(this ref SpanWriter<byte> writer, scoped ReadOnlySpan<by
128
135
129
136
return result ;
130
137
}
138
+
139
+ /// <summary>
140
+ /// Encodes string using the specified encoding.
141
+ /// </summary>
142
+ /// <param name="writer">The buffer writer.</param>
143
+ /// <param name="chars">The sequence of characters.</param>
144
+ /// <param name="context">The encoding context.</param>
145
+ /// <param name="lengthFormat">String length encoding format; or <see langword="null"/> to prevent encoding of string length.</param>
146
+ /// <returns>The number of written bytes.</returns>
147
+ public static int Encode ( this ref BufferWriterSlim < byte > writer , scoped ReadOnlySpan < char > chars , in EncodingContext context ,
148
+ LengthFormat ? lengthFormat = null )
149
+ {
150
+ Span < byte > buffer ;
151
+ int byteCount , result ;
152
+ if ( lengthFormat . HasValue )
153
+ {
154
+ byteCount = context . Encoding . GetByteCount ( chars ) ;
155
+ result = writer . WriteLength ( byteCount , lengthFormat . GetValueOrDefault ( ) ) ;
156
+
157
+ buffer = writer . GetSpan ( byteCount ) ;
158
+ byteCount = context . TryGetEncoder ( ) is { } encoder
159
+ ? encoder . GetBytes ( chars , buffer , flush : true )
160
+ : context . Encoding . GetBytes ( chars , buffer ) ;
161
+
162
+ result += byteCount ;
163
+ writer . Advance ( byteCount ) ;
164
+ }
165
+ else
166
+ {
167
+ result = 0 ;
168
+ var encoder = context . GetEncoder ( ) ;
169
+ byteCount = context . Encoding . GetMaxByteCount ( 1 ) ;
170
+ for ( int charsUsed , bytesWritten ; ! chars . IsEmpty ; chars = chars . Slice ( charsUsed ) , result += bytesWritten )
171
+ {
172
+ buffer = writer . GetSpan ( byteCount ) ;
173
+ var maxChars = buffer . Length / byteCount ;
174
+
175
+ encoder . Convert ( chars , buffer , chars . Length <= maxChars , out charsUsed , out bytesWritten , out _ ) ;
176
+ writer . Advance ( bytesWritten ) ;
177
+ }
178
+ }
179
+
180
+ return result ;
181
+ }
182
+
183
+ /// <summary>
184
+ /// Writes a sequence of bytes prefixed with the length.
185
+ /// </summary>
186
+ /// <param name="writer">The buffer writer.</param>
187
+ /// <param name="value">A sequence of bytes to be written.</param>
188
+ /// <param name="lengthFormat">A format of the buffer length to be written.</param>
189
+ /// <returns>A number of bytes written.</returns>
190
+ public static int Write ( this ref BufferWriterSlim < byte > writer , scoped ReadOnlySpan < byte > value , LengthFormat lengthFormat )
191
+ {
192
+ var result = writer . WriteLength ( value . Length , lengthFormat ) ;
193
+ writer . Write ( value ) ;
194
+ result += value . Length ;
195
+
196
+ return result ;
197
+ }
131
198
132
199
private static bool TryFormat < T > ( IBufferWriter < byte > writer , T value , Span < char > buffer , in EncodingContext context , LengthFormat ? lengthFormat , ReadOnlySpan < char > format , IFormatProvider ? provider , out long bytesWritten )
133
200
where T : notnull , ISpanFormattable
0 commit comments