Skip to content
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
38 changes: 5 additions & 33 deletions src/DataFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// See the UNLICENSE file in the project root for more information.

using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;

namespace Neliva
Expand All @@ -29,45 +28,18 @@ public static partial class DataFormat
(byte)'r', (byte)'s', (byte)'t', (byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z', // base32
};

private static Guid ReadGuidBigEndian(Span<byte> value)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Guid ReadGuidBigEndian(ReadOnlySpan<byte> value)
{
var v15 = value[15];

return new Guid(
BinaryPrimitives.ReadUInt32BigEndian(value),
BinaryPrimitives.ReadUInt16BigEndian(value.Slice(4)),
BinaryPrimitives.ReadUInt16BigEndian(value.Slice(6)),
value[8],
value[9],
value[10],
value[11],
value[12],
value[13],
value[14],
v15);
return new Guid(value, true);
}

private static void WriteGuidBigEndian(Guid value, Span<byte> destination)
{
if (!value.TryWriteBytes(destination))
if (!value.TryWriteBytes(destination, true, out _))
{
throw new ArgumentOutOfRangeException(nameof(destination));
}

// Make the GUID bytes appear in the big endian format.
Swap(destination, 0, 3);
Swap(destination, 1, 2);
Swap(destination, 4, 5);
Swap(destination, 6, 7);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Swap(Span<byte> value, int idx1, int idx2)
{
var tmp = value[idx1];

value[idx1] = value[idx2];
value[idx2] = tmp;
}
}
}
}