Skip to content

simplify WriteShortstr #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions projects/RabbitMQ.Client/client/impl/WireFormatting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,11 +293,11 @@ public static int WriteFieldValue(Memory<byte> memory, object value)
{
case string val:
memory.Span[0] = (byte)'S';
if (MemoryMarshal.TryGetArray(memory.Slice(5, Encoding.UTF8.GetByteCount(val)), out ArraySegment<byte> segment))
if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
{
NetworkOrderSerializer.WriteUInt32(slice, (uint)segment.Count);
Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset);
return segment.Count + 5;
int bytesWritten = Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset + 5);
NetworkOrderSerializer.WriteUInt32(slice, (uint)bytesWritten);
return 5 + bytesWritten;
}

throw new WireFormattingException("Unable to get array segment from memory.");
Expand Down Expand Up @@ -429,12 +429,11 @@ public static int WriteShort(Memory<byte> memory, ushort val)

public static int WriteShortstr(Memory<byte> memory, string val)
{
int stringBytesNeeded = Encoding.UTF8.GetByteCount(val);
if (MemoryMarshal.TryGetArray(memory.Slice(1, stringBytesNeeded), out ArraySegment<byte> segment))
if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
{
memory.Span[0] = (byte)stringBytesNeeded;
Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset);
return stringBytesNeeded + 1;
int bytesWritten = Encoding.UTF8.GetBytes(val, 0, val.Length, segment.Array, segment.Offset + 1);
memory.Span[0] = (byte)bytesWritten;
return bytesWritten + 1;
}

throw new WireFormattingException("Unable to get array segment from memory.");
Expand Down