dotnet / dotnet/docs

A VERY small modification in code sample needed

Open
#33,273 0 comments 0 reactions 0 assignees View on GitHub
dotnet-fundamentals/svc
Dominant language
No language data
Stars
4.8k
Forks
6.1k
Avg merge
15h 21m
Merged PRs (30d)
370

Description

In one of sample of this page in section Process Text Data and method `TryParseLine`, in case of finding a byte with `'\r' `content which is not the last byte of segment too, we check for the next byte to be` '\n'` and `break` in this case. But in case that the next byte not be` '\n' `we continue loop which is okay. The point is that we should reset index variable to `-1` here to identify the code that no line yet found in code. Of course I am not sure that how adding `'\r' `without a `'\n' `after it could be happen but it is may be the case in test texts.
The edited code could be as followed:

```
while (buffer.TryGet(ref position, out ReadOnlyMemory segment))
{

var span = segment.Span;
index = span.IndexOf((byte)'\r');
if (index > -1)
{
// Does the index is last byte of segment?
if (index == span.Length - 1)
{
// We should search next byte in next segment for \n
var next = position;
if (buffer.TryGet(ref next, out ReadOnlyMemory nextSegment))
{
if (nextSegment.Span[0] == (byte)'\n')
{
// line found
break;
}
}
else
{
return false;
}
}
else
{
// The index is not the last byte of span so compare next byte in this segment

if (span[index + 1] == (byte)'\n')
{
// line found inside one segment
break;
}
else
{
index = -1;
}
}
}
copy = position;// this should be for all non-break cases
}
```

---
#### Document Details

⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*

* ID: b9a7a159-0d11-531b-a444-cb154cd09967
* Version Independent ID: 73a2ecb2-17d6-4f36-4b59-5e3cf619edf1
* Content: [System.Buffers - .NET](https://learn.microsoft.com/en-us/dotnet/standard/io/buffers)
* Content Source: [docs/standard/io/buffers.md](https://github.com/dotnet/docs/blob/main/docs/standard/io/buffers.md)
* Product: **dotnet-fundamentals**
* GitHub Login: @Rick-Anderson
* Microsoft Alias: **riande**

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.