Skip to content

Commit 327aa1f

Browse files
committed
Feedback
1 parent e48e7e1 commit 327aa1f

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

src/Http/WebUtilities/src/HttpRequestStreamReader.cs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -199,10 +199,7 @@ public override int Read(Span<char> buffer)
199199
charsRead += charsRemaining;
200200
count -= charsRemaining;
201201

202-
if (count > 0)
203-
{
204-
buffer = buffer.Slice(charsRemaining, count);
205-
}
202+
buffer = buffer.Slice(charsRemaining, count);
206203

207204
// If we got back fewer chars than we asked for, then it's likely the underlying stream is blocked.
208205
// 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
254251
while (count > 0)
255252
{
256253
// n is the characters available in _charBuffer
257-
var charsAvailable = _charsRead - _charBufferIndex;
254+
var charsRemaining = _charsRead - _charBufferIndex;
258255

259256
// charBuffer is empty, let's read from the stream
260-
if (charsAvailable == 0)
257+
if (charsRemaining == 0)
261258
{
262259
_charsRead = 0;
263260
_charBufferIndex = 0;
@@ -267,7 +264,7 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
267264
// We break out of the loop if the stream is blocked (EOF is reached).
268265
do
269266
{
270-
Debug.Assert(charsAvailable == 0);
267+
Debug.Assert(charsRemaining == 0);
271268
_bytesRead = await _stream.ReadAsync(
272269
_byteBuffer,
273270
0,
@@ -281,47 +278,43 @@ public override async ValueTask<int> ReadAsync(Memory<char> buffer, Cancellation
281278
// _isBlocked == whether we read fewer bytes than we asked for.
282279
_isBlocked = (_bytesRead < _byteBufferSize);
283280

284-
Debug.Assert(charsAvailable == 0);
281+
Debug.Assert(charsRemaining == 0);
285282

286283
_charBufferIndex = 0;
287-
charsAvailable = _decoder.GetChars(
284+
charsRemaining = _decoder.GetChars(
288285
_byteBuffer,
289286
0,
290287
_bytesRead,
291288
_charBuffer,
292289
0);
293290

294-
Debug.Assert(charsAvailable > 0);
291+
Debug.Assert(charsRemaining > 0);
295292

296-
_charsRead += charsAvailable; // Number of chars in StreamReader's buffer.
293+
_charsRead += charsRemaining; // Number of chars in StreamReader's buffer.
297294
}
298-
while (charsAvailable == 0);
295+
while (charsRemaining == 0);
299296

300-
if (charsAvailable == 0)
297+
if (charsRemaining == 0)
301298
{
302299
break; // We're at EOF
303300
}
304301
}
305302

306303
// Got more chars in charBuffer than the user requested
307-
if (charsAvailable > count)
304+
if (charsRemaining > count)
308305
{
309-
charsAvailable = count;
306+
charsRemaining = count;
310307
}
311308

312-
var source = new Memory<char>(_charBuffer, _charBufferIndex, charsAvailable);
309+
var source = new Memory<char>(_charBuffer, _charBufferIndex, charsRemaining);
313310
source.CopyTo(buffer);
314311

315-
if (charsAvailable < count)
316-
{
317-
// update the buffer to the remaining portion
318-
buffer = buffer.Slice(charsAvailable);
319-
}
312+
_charBufferIndex += charsRemaining;
320313

321-
_charBufferIndex += charsAvailable;
314+
charsRead += charsRemaining;
315+
count -= charsRemaining;
322316

323-
charsRead += charsAvailable;
324-
count -= charsAvailable;
317+
buffer = buffer.Slice(charsRemaining, count);
325318

326319
// This function shouldn't block for an indefinite amount of time,
327320
// or reading from a network stream won't work right. If we got
@@ -399,16 +392,17 @@ public override string ReadLine()
399392
{
400393
return stepResult.Result ?? sb?.ToString();
401394
}
402-
403-
continue;
404395
}
405396
}
406397

407398
private ReadLineStepResult ReadLineStep(ref StringBuilder sb, ref bool consumeLineFeed)
408399
{
400+
const char carriageReturn = '\r';
401+
const char lineFeed = '\n';
402+
409403
if (consumeLineFeed)
410404
{
411-
if (_charBuffer[_charBufferIndex] == '\n')
405+
if (_charBuffer[_charBufferIndex] == lineFeed)
412406
{
413407
_charBufferIndex++;
414408
}
@@ -417,19 +411,19 @@ private ReadLineStepResult ReadLineStep(ref StringBuilder sb, ref bool consumeLi
417411

418412
var span = new Span<char>(_charBuffer, _charBufferIndex, _charsRead - _charBufferIndex);
419413

420-
var index = span.IndexOfAny('\r', '\n');
414+
var index = span.IndexOfAny(carriageReturn, lineFeed);
421415

422416
if (index != -1)
423417
{
424-
if (span[index] == '\r')
418+
if (span[index] == carriageReturn)
425419
{
426420
span = span.Slice(0, index);
427421
_charBufferIndex += index + 1;
428422

429423
if (_charBufferIndex < _charsRead)
430424
{
431-
// consume following \n
432-
if (_charBuffer[_charBufferIndex] == '\n')
425+
// consume following line feed
426+
if (_charBuffer[_charBufferIndex] == lineFeed)
433427
{
434428
_charBufferIndex++;
435429
}
@@ -451,7 +445,7 @@ private ReadLineStepResult ReadLineStep(ref StringBuilder sb, ref bool consumeLi
451445
return ReadLineStepResult.Continue;
452446
}
453447

454-
if (span[index] == '\n')
448+
if (span[index] == lineFeed)
455449
{
456450
span = span.Slice(0, index);
457451
_charBufferIndex += index + 1;

0 commit comments

Comments
 (0)