-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuiteUtility.cs
88 lines (65 loc) · 2.31 KB
/
SuiteUtility.cs
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// BRR Suite is licensed under the MIT license.
global using System;
global using System.IO;
global using static BRRSuite.Conversion;
using System.Runtime.CompilerServices;
//using System.Buffers;
[assembly: CLSCompliant(true)]
namespace BRRSuite;
/// <summary>
/// Just holds stuff common to the library.
/// </summary>
internal static class SuiteUtility {
// TODO look into implementing this to reduce garbage collection
//internal static readonly ArrayPool<int> SamplesPool = ArrayPool<int>.Create(100000, 50);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int ReadInt(byte[] data, int index) {
return Unsafe.As<byte, int>(ref data[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static int ReadShort(byte[] data, int index) {
return Unsafe.As<byte, ushort>(ref data[index]);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteInt(byte[] data, int index, int value) {
Unsafe.As<byte, int>(ref data[index]) = value;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void WriteShort(byte[] data, int index, int value) {
Unsafe.As<byte, ushort>(ref data[index]) = (ushort) value;
}
internal static void WriteString(byte[] data, int index, string text) {
foreach (var c in text) {
data[index++] = (byte) c;
}
}
internal static bool TestSubstring(byte[] data, int start, string test, out string? message) {
string result = GetLatin1String(data, start, test.Length);
if (string.Equals(result, test, StringComparison.Ordinal)) {
message = null;
return true;
}
message = $"Bad string at {start}: {result} | Expected: {test}";
return false;
}
internal static string GetLatin1String(byte[] data, int start, int length) {
char[] charList = new char[length];
for (int i = 0; i < length; i++) {
char charAdd = (char) data[start + i];
switch (charAdd) {
case < '\x20': // control characters
case >= '\x7F' and < '\xA0': // DEL + more control characters
case '\xAD': // SHY
case '\xA0': // NBSP
charAdd = '?';
break;
}
charList[i] = charAdd;
}
return new string(charList);
}
/// <summary>
/// Permanent copy for the library.
/// </summary>
internal static readonly System.Collections.Immutable.ImmutableArray<int> SuiteGaussTable = [.. GetGaussTable()];
}