Skip to content

Instantly share code, notes, and snippets.

@terrajobst
Created July 9, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terrajobst/6e1bea5bec4591edd7c5fe5416ce7f56 to your computer and use it in GitHub Desktop.
Save terrajobst/6e1bea5bec4591edd7c5fe5416ce7f56 to your computer and use it in GitHub Desktop.
string GetAsciiString(ReadOnlySequence<byte> buffer)
{
if (buffer.IsSingleSegment)
{
return Encoding.ASCII.GetString(buffer.First.Span);
}
return string.Create((int)buffer.Length, buffer, (span, sequence) =>
{
foreach (var segment in sequence)
{
Encoding.ASCII.GetChars(segment.Span, span);
span = span.Slice(segment.Length);
}
});
}
@mungojam
Copy link

mungojam commented Jul 9, 2018

Looks like it should be yield returning span maybe?

        foreach (var segment in sequence)
        {
            Encoding.ASCII.GetChars(segment.Span, span);

            span = span.Slice(segment.Length);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment