From addd4e2d620f51494fb281255d419bb3092c27cd Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 17 Dec 2020 13:02:52 +1300 Subject: [PATCH] Read char instead of byte for remainder --- csharp/src/Google.Protobuf/WritingPrimitives.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/csharp/src/Google.Protobuf/WritingPrimitives.cs b/csharp/src/Google.Protobuf/WritingPrimitives.cs index 823cdb7f49db..70f96e965de8 100644 --- a/csharp/src/Google.Protobuf/WritingPrimitives.cs +++ b/csharp/src/Google.Protobuf/WritingPrimitives.cs @@ -182,7 +182,8 @@ public static void WriteString(ref Span buffer, ref WriterInternalState st // If 64bit, and value has at least 4 chars, process 4 chars at a time. if (IntPtr.Size == 8 && value.Length >= 4) { - ref byte sourceBytes = ref Unsafe.As(ref MemoryMarshal.GetReference(value.AsSpan())); + ref char sourceChars = ref MemoryMarshal.GetReference(value.AsSpan()); + ref byte sourceBytes = ref Unsafe.As(ref sourceChars); ref byte destinationBytes = ref MemoryMarshal.GetReference(buffer.Slice(state.position)); // Process 4 chars at a time until there are less than 4 remaining. @@ -201,7 +202,7 @@ public static void WriteString(ref Span buffer, ref WriterInternalState st // so use them to perform array operations without bounds checks. for (; currentIndex < length; currentIndex++) { - Unsafe.AddByteOffset(ref destinationBytes, (IntPtr)currentIndex) = Unsafe.AddByteOffset(ref sourceBytes, (IntPtr)(currentIndex * 2)); + Unsafe.AddByteOffset(ref destinationBytes, (IntPtr)currentIndex) = (byte)Unsafe.AddByteOffset(ref sourceChars, (IntPtr)(currentIndex * 2)); } } else