@@ -199,10 +199,7 @@ public override int Read(Span<char> buffer)
199
199
charsRead += charsRemaining ;
200
200
count -= charsRemaining ;
201
201
202
- if ( count > 0 )
203
- {
204
- buffer = buffer . Slice ( charsRemaining , count ) ;
205
- }
202
+ buffer = buffer . Slice ( charsRemaining , count ) ;
206
203
207
204
// If we got back fewer chars than we asked for, then it's likely the underlying stream is blocked.
208
205
// Send the data back to the caller so they can process it.
@@ -254,10 +251,10 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
254
251
while ( count > 0 )
255
252
{
256
253
// n is the characters available in _charBuffer
257
- var charsAvailable = _charsRead - _charBufferIndex ;
254
+ var charsRemaining = _charsRead - _charBufferIndex ;
258
255
259
256
// charBuffer is empty, let's read from the stream
260
- if ( charsAvailable == 0 )
257
+ if ( charsRemaining == 0 )
261
258
{
262
259
_charsRead = 0 ;
263
260
_charBufferIndex = 0 ;
@@ -267,7 +264,7 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
267
264
// We break out of the loop if the stream is blocked (EOF is reached).
268
265
do
269
266
{
270
- Debug . Assert ( charsAvailable == 0 ) ;
267
+ Debug . Assert ( charsRemaining == 0 ) ;
271
268
_bytesRead = await _stream . ReadAsync (
272
269
_byteBuffer ,
273
270
0 ,
@@ -281,47 +278,43 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
281
278
// _isBlocked == whether we read fewer bytes than we asked for.
282
279
_isBlocked = ( _bytesRead < _byteBufferSize ) ;
283
280
284
- Debug . Assert ( charsAvailable == 0 ) ;
281
+ Debug . Assert ( charsRemaining == 0 ) ;
285
282
286
283
_charBufferIndex = 0 ;
287
- charsAvailable = _decoder . GetChars (
284
+ charsRemaining = _decoder . GetChars (
288
285
_byteBuffer ,
289
286
0 ,
290
287
_bytesRead ,
291
288
_charBuffer ,
292
289
0 ) ;
293
290
294
- Debug . Assert ( charsAvailable > 0 ) ;
291
+ Debug . Assert ( charsRemaining > 0 ) ;
295
292
296
- _charsRead += charsAvailable ; // Number of chars in StreamReader's buffer.
293
+ _charsRead += charsRemaining ; // Number of chars in StreamReader's buffer.
297
294
}
298
- while ( charsAvailable == 0 ) ;
295
+ while ( charsRemaining == 0 ) ;
299
296
300
- if ( charsAvailable == 0 )
297
+ if ( charsRemaining == 0 )
301
298
{
302
299
break ; // We're at EOF
303
300
}
304
301
}
305
302
306
303
// Got more chars in charBuffer than the user requested
307
- if ( charsAvailable > count )
304
+ if ( charsRemaining > count )
308
305
{
309
- charsAvailable = count ;
306
+ charsRemaining = count ;
310
307
}
311
308
312
- var source = new Memory < char > ( _charBuffer , _charBufferIndex , charsAvailable ) ;
309
+ var source = new Memory < char > ( _charBuffer , _charBufferIndex , charsRemaining ) ;
313
310
source . CopyTo ( buffer ) ;
314
311
315
- if ( charsAvailable < count )
316
- {
317
- // update the buffer to the remaining portion
318
- buffer = buffer . Slice ( charsAvailable ) ;
319
- }
312
+ _charBufferIndex += charsRemaining ;
320
313
321
- _charBufferIndex += charsAvailable ;
314
+ charsRead += charsRemaining ;
315
+ count -= charsRemaining ;
322
316
323
- charsRead += charsAvailable ;
324
- count -= charsAvailable ;
317
+ buffer = buffer . Slice ( charsRemaining , count ) ;
325
318
326
319
// This function shouldn't block for an indefinite amount of time,
327
320
// or reading from a network stream won't work right. If we got
@@ -399,16 +392,17 @@ public override string ReadLine()
399
392
{
400
393
return stepResult . Result ?? sb ? . ToString ( ) ;
401
394
}
402
-
403
- continue ;
404
395
}
405
396
}
406
397
407
398
private ReadLineStepResult ReadLineStep ( ref StringBuilder sb , ref bool consumeLineFeed )
408
399
{
400
+ const char carriageReturn = '\r ' ;
401
+ const char lineFeed = '\n ' ;
402
+
409
403
if ( consumeLineFeed )
410
404
{
411
- if ( _charBuffer [ _charBufferIndex ] == ' \n ' )
405
+ if ( _charBuffer [ _charBufferIndex ] == lineFeed )
412
406
{
413
407
_charBufferIndex ++ ;
414
408
}
@@ -417,19 +411,19 @@ private ReadLineStepResult ReadLineStep(ref StringBuilder sb, ref bool consumeLi
417
411
418
412
var span = new Span < char > ( _charBuffer , _charBufferIndex , _charsRead - _charBufferIndex ) ;
419
413
420
- var index = span . IndexOfAny ( ' \r ' , ' \n ' ) ;
414
+ var index = span . IndexOfAny ( carriageReturn , lineFeed ) ;
421
415
422
416
if ( index != - 1 )
423
417
{
424
- if ( span [ index ] == ' \r ' )
418
+ if ( span [ index ] == carriageReturn )
425
419
{
426
420
span = span . Slice ( 0 , index ) ;
427
421
_charBufferIndex += index + 1 ;
428
422
429
423
if ( _charBufferIndex < _charsRead )
430
424
{
431
- // consume following \n
432
- if ( _charBuffer [ _charBufferIndex ] == ' \n ' )
425
+ // consume following line feed
426
+ if ( _charBuffer [ _charBufferIndex ] == lineFeed )
433
427
{
434
428
_charBufferIndex ++ ;
435
429
}
@@ -451,7 +445,7 @@ private ReadLineStepResult ReadLineStep(ref StringBuilder sb, ref bool consumeLi
451
445
return ReadLineStepResult . Continue ;
452
446
}
453
447
454
- if ( span [ index ] == ' \n ' )
448
+ if ( span [ index ] == lineFeed )
455
449
{
456
450
span = span . Slice ( 0 , index ) ;
457
451
_charBufferIndex += index + 1 ;
0 commit comments