forked from SamboyCoding/Cpp2IL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
90 lines (74 loc) · 2.88 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
using System.Collections.Generic;
using System.Text;
using Iced.Intel;
namespace LibCpp2IL
{
public static class Extensions
{
public static bool IsImmediate(this OpKind opKind) => opKind >= OpKind.Immediate8 && opKind <= OpKind.Immediate32to64;
public static ulong GetRipBasedInstructionMemoryAddress(this Instruction instruction) => instruction.IPRelativeMemoryAddress;
public static T[] SubArray<T>(this T[] data, int index, int length)
{
var result = new T[length];
Array.Copy(data, index, result, 0, length);
return result;
}
public static T RemoveAndReturn<T>(this List<T> data, int index)
{
var result = data[index];
data.RemoveAt(index);
return result;
}
public static string Repeat(this string source, int count)
{
var res = new StringBuilder();
for (var i = 0; i < count; i++)
{
res.Append(source);
}
return res.ToString();
}
public static string ToStringEnumerable<T>(this IEnumerable<T> enumerable)
{
var builder = new StringBuilder("[");
builder.Append(string.Join(", ", enumerable));
builder.Append("]");
return builder.ToString();
}
public static TValue? GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue? defaultValue)
{
if (dictionary == null)
{
throw new ArgumentNullException(nameof(dictionary));
}
TValue value;
if (dictionary.TryGetValue(key, out value))
return value;
return defaultValue;
}
public static TValue? GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key) => dictionary.GetValueOrDefault(key, default);
public static void Deconstruct<TKey, TValue>(this KeyValuePair<TKey, TValue> pair, out TKey one, out TValue two)
{
one = pair.Key;
two = pair.Value;
}
public static uint Bits(this uint x, int low, int count) => (x >> low) & (uint) ((1 << count) - 1);
public static bool TryAdd<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
if (dictionary.ContainsKey(key))
return false;
dictionary.Add(key, value);
return true;
}
public static void SortByExtractedKey<T, K>(this List<T> list, Func<T, K> keyObtainer) where K : IComparable<K>
{
list.Sort((a, b) =>
{
var aKey = keyObtainer(a);
var bKey = keyObtainer(b);
return aKey.CompareTo(bKey);
});
}
}
}