Commit 3d658a77 authored by Marc Gravell's avatar Marc Gravell

GetString(ReadOnlySequence<byte> buffer) API

parent 25e6f67a
using System; using System;
using System.Buffers;
using System.Globalization; using System.Globalization;
using System.Net; using System.Net;
using System.Numerics; using System.Numerics;
...@@ -261,5 +262,24 @@ internal static EndPoint TryParseEndPoint(string addressWithPort) ...@@ -261,5 +262,24 @@ internal static EndPoint TryParseEndPoint(string addressWithPort)
} }
return new DnsEndPoint(addressPart, port ?? 0); return new DnsEndPoint(addressPart, port ?? 0);
} }
internal static string GetString(ReadOnlySequence<byte> buffer)
{
if (buffer.IsSingleSegment) return GetString(buffer.First.Span);
var arr = ArrayPool<byte>.Shared.Rent(checked((int)buffer.Length));
var span = new Span<byte>(arr, 0, (int)buffer.Length);
buffer.CopyTo(span);
string s = GetString(span);
ArrayPool<byte>.Shared.Return(arr);
return s;
}
internal static unsafe string GetString(ReadOnlySpan<byte> span)
{
fixed (byte* ptr = &MemoryMarshal.GetReference(span))
{
return Encoding.UTF8.GetString(ptr, span.Length);
}
}
} }
} }
...@@ -1514,10 +1514,12 @@ private static RawResult ReadLineTerminatedString(ResultType type, ref BufferRea ...@@ -1514,10 +1514,12 @@ private static RawResult ReadLineTerminatedString(ResultType type, ref BufferRea
reader.Consume(1); reader.Consume(1);
return ReadArray(in buffer, ref reader, includeDetilInExceptions, server); return ReadArray(in buffer, ref reader, includeDetilInExceptions, server);
default: default:
// string s = Format.GetString(buffer);
if (allowInlineProtocol) return ParseInlineProtocol(ReadLineTerminatedString(ResultType.SimpleString, ref reader)); if (allowInlineProtocol) return ParseInlineProtocol(ReadLineTerminatedString(ResultType.SimpleString, ref reader));
throw new InvalidOperationException("Unexpected response prefix: " + (char)prefix); throw new InvalidOperationException("Unexpected response prefix: " + (char)prefix);
} }
} }
private static RawResult ParseInlineProtocol(RawResult line) private static RawResult ParseInlineProtocol(RawResult line)
{ {
if (!line.HasValue) return RawResult.Nil; // incomplete line if (!line.HasValue) return RawResult.Nil; // incomplete line
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment