-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
70 lines (64 loc) · 2.44 KB
/
Extensions.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
using System.Runtime.CompilerServices;
namespace AssEmbly
{
public static class Extensions
{
/// <summary>
/// Convert raw text to its equivalent form as an AssEmbly string literal.
/// </summary>
/// <remarks>Neither the input nor the output to this function have surrounding quote marks.</remarks>
public static string EscapeCharacters(this string unescaped)
{
return unescaped.Replace("\\", "\\\\").Replace("\"", "\\\"").Replace("@", "\\@");
}
/// <summary>
/// Convert a char to its equivalent form as an escaped AssEmbly character literal.
/// </summary>
/// <remarks>Neither the input nor the output to this function have surrounding quote marks.</remarks>
public static string EscapeCharacter(this char character)
{
return character is '\\' or '\'' or '@' ? $"\\{character}" : $"{character}";
}
public static void SetContentTo<T>(this Stack<T> target, Stack<T> source)
{
target.Clear();
foreach (T frame in source.Reverse())
{
target.Push(frame);
}
}
public static Stack<T> NestedCopy<T>(this Stack<T> stack) where T : ICloneable
{
return new Stack<T>(stack.Select(i => (T)i.Clone()).Reverse());
}
#if DISPLACEMENT
public static string GetMultiplierString(this DisplacementMultiplier multiplier)
{
return multiplier switch
{
DisplacementMultiplier.x1 => "1",
DisplacementMultiplier.x2 => "2",
DisplacementMultiplier.x4 => "4",
DisplacementMultiplier.x8 => "8",
DisplacementMultiplier.x16 => "16",
DisplacementMultiplier.x32 => "32",
DisplacementMultiplier.x64 => "64",
DisplacementMultiplier.x128 => "128",
_ => "?" // Invalid value - won't assemble
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ulong GetByteCount(this DisplacementMode mode)
{
return mode switch
{
DisplacementMode.NoDisplacement => 1,
DisplacementMode.Constant => 9,
DisplacementMode.Register => 2,
DisplacementMode.ConstantAndRegister => 10,
_ => 0
};
}
#endif
}
}