@@ -40,21 +40,20 @@ public static ArraySegment<byte> GetArray(this ReadOnlyMemory<byte> memory)
40
40
return result ;
41
41
}
42
42
43
- internal static unsafe void WriteAscii ( ref this BufferWriter < PipeWriter > buffer , string data )
43
+ internal static void WriteAscii ( ref this BufferWriter < PipeWriter > buffer , string data )
44
44
{
45
45
if ( string . IsNullOrEmpty ( data ) )
46
46
{
47
47
return ;
48
48
}
49
49
50
- var dataLength = data . Length ;
51
- var bytes = buffer . Span ;
52
-
50
+ var dest = buffer . Span ;
51
+ var sourceLength = data . Length ;
53
52
// Fast path, try encoding to the available memory directly
54
- if ( dataLength <= bytes . Length )
53
+ if ( sourceLength <= dest . Length )
55
54
{
56
- Encoding . ASCII . GetBytes ( data , bytes ) ;
57
- buffer . Advance ( dataLength ) ;
55
+ Encoding . ASCII . GetBytes ( data , dest ) ;
56
+ buffer . Advance ( sourceLength ) ;
58
57
}
59
58
else
60
59
{
@@ -134,35 +133,31 @@ private static void WriteNumericMultiWrite(ref this BufferWriter<PipeWriter> buf
134
133
}
135
134
136
135
[ MethodImpl ( MethodImplOptions . NoInlining ) ]
137
- private unsafe static void WriteAsciiMultiWrite ( ref this BufferWriter < PipeWriter > buffer , string data )
136
+ private static void WriteAsciiMultiWrite ( ref this BufferWriter < PipeWriter > buffer , string data )
138
137
{
139
138
var dataLength = data . Length ;
140
139
var offset = 0 ;
141
140
var bytes = buffer . Span ;
142
141
do
143
142
{
144
143
var writable = Math . Min ( dataLength - offset , bytes . Length ) ;
145
- // Zero length spans are possible
146
- if ( writable > 0 )
147
- {
148
- Encoding . ASCII . GetBytes ( data . AsSpan ( offset , writable ) , bytes ) ;
149
-
150
- buffer . Advance ( writable ) ;
151
- offset += writable ;
152
- }
153
-
154
- // Get new span if more to encode, and reset bytesLength
155
- if ( offset < dataLength )
156
- {
157
- buffer . Ensure ( ) ;
158
- bytes = buffer . Span ;
159
- continue ;
160
- }
161
- else
144
+ // Zero length spans are possible, though unlikely.
145
+ // ASCII.GetBytes and .Advance will both handle them so we won't special case for them.
146
+ Encoding . ASCII . GetBytes ( data . AsSpan ( offset , writable ) , bytes ) ;
147
+ buffer . Advance ( writable ) ;
148
+
149
+ // The `add` will macro-op fuse with `if` test.
150
+ offset += writable ;
151
+ if ( offset >= dataLength )
162
152
{
153
+ Debug . Assert ( offset == dataLength ) ;
163
154
// Encoded everything
164
155
break ;
165
156
}
157
+
158
+ // Get new span, more to encode.
159
+ buffer . Ensure ( ) ;
160
+ bytes = buffer . Span ;
166
161
} while ( true ) ;
167
162
}
168
163
0 commit comments