Skip to content

Commit

Permalink
Read char instead of byte for remainder
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK committed Dec 17, 2020
1 parent 9189e56 commit addd4e2
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions csharp/src/Google.Protobuf/WritingPrimitives.cs
Expand Up @@ -182,7 +182,8 @@ public static void WriteString(ref Span<byte> 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<char, byte>(ref MemoryMarshal.GetReference(value.AsSpan()));
ref char sourceChars = ref MemoryMarshal.GetReference(value.AsSpan());
ref byte sourceBytes = ref Unsafe.As<char, byte>(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.
Expand All @@ -201,7 +202,7 @@ public static void WriteString(ref Span<byte> 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
Expand Down

0 comments on commit addd4e2

Please sign in to comment.