blob: c103ee7bc261b684ce46d4e09f8abc3e72c85bea (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
using System;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
namespace Impostor.Api
{
/// <summary>
/// Priovides a StreamReader-like api throught extensions
/// </summary>
public static class SpanReaderExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte ReadByte(this ref ReadOnlySpan<byte> input)
{
var original = Advance<byte>(ref input);
return original[0];
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int ReadInt32(this ref ReadOnlySpan<byte> input)
{
var original = Advance<int>(ref input);
return BinaryPrimitives.ReadInt32LittleEndian(original);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static uint ReadUInt32(this ref ReadOnlySpan<byte> input)
{
var original = Advance<uint>(ref input);
return BinaryPrimitives.ReadUInt32LittleEndian(original);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static float ReadSingle(this ref ReadOnlySpan<byte> input)
{
var original = Advance<float>(ref input);
// BitConverter.Int32BitsToSingle
// Doesn't exist in net 2.0 for some reason
return Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(original));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool ReadBoolean(this ref ReadOnlySpan<byte> input)
{
return input.ReadByte() != 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe float Int32BitsToSingle(int value)
{
return *((float*)&value);
}
/// <summary>
/// Advances the position of <see cref="input"/> by the size of <see cref="T"/>.
/// </summary>
/// <typeparam name="T">Type that will be read.</typeparam>
/// <param name="input">input "stream"/span.</param>
/// <returns>The original input</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static unsafe ReadOnlySpan<byte> Advance<T>(ref ReadOnlySpan<byte> input)
where T : unmanaged
{
var original = input;
input = input.Slice(sizeof(T));
return original;
}
}
}
|