forked from ReClassNET/ReClass.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cs
More file actions
120 lines (101 loc) · 2.73 KB
/
Copy pathUtil.cs
File metadata and controls
120 lines (101 loc) · 2.73 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Drawing;
using System.Globalization;
namespace ReClassNET.Util
{
public static class Utils
{
public static T Min<T, U>(T item1, T item2, Func<T, U> keySelector) where U : IComparable
{
Contract.Requires(keySelector != null);
return Min(item1, item2, keySelector, Comparer<U>.Default);
}
public static T Min<T, U>(T item1, T item2, Func<T, U> keySelector, IComparer<U> comparer)
{
Contract.Requires(keySelector != null);
Contract.Requires(comparer != null);
if (comparer.Compare(keySelector(item1), keySelector(item2)) < 0)
{
return item1;
}
return item2;
}
public static T Max<T, U>(T item1, T item2, Func<T, U> keySelector) where U : IComparable
{
Contract.Requires(keySelector != null);
return Max(item1, item2, keySelector, Comparer<U>.Default);
}
public static T Max<T, U>(T item1, T item2, Func<T, U> keySelector, IComparer<U> comparer)
{
Contract.Requires(keySelector != null);
Contract.Requires(comparer != null);
if (comparer.Compare(keySelector(item1), keySelector(item2)) > 0)
{
return item1;
}
return item2;
}
public static void Swap<T>(ref T lhs, ref T rhs)
{
var temp = lhs;
lhs = rhs;
rhs = temp;
}
public static Size AggregateNodeSizes(Size baseSize, Size newSize)
{
return new Size(Math.Max(baseSize.Width, newSize.Width), baseSize.Height + newSize.Height);
}
public static NumberFormatInfo GuessNumberFormat(string input)
{
Contract.Requires(input != null);
Contract.Ensures(Contract.Result<NumberFormatInfo>() != null);
if (input.Contains(",") && !input.Contains("."))
{
return new NumberFormatInfo
{
NumberDecimalSeparator = ",",
NumberGroupSeparator = "."
};
}
return new NumberFormatInfo
{
NumberDecimalSeparator = ".",
NumberGroupSeparator = ","
};
}
private static readonly uint[] hexLookup = CreateHexLookup();
private static uint[] CreateHexLookup()
{
var result = new uint[256];
for (var i = 0; i < 256; i++)
{
var s = i.ToString("X2");
result[i] = (uint)s[0] + ((uint)s[1] << 16);
}
return result;
}
public static string ByteArrayToHexString(byte[] data)
{
Contract.Requires(data != null);
if (data.Length == 0)
{
return string.Empty;
}
var lookup = hexLookup;
var result = new char[data.Length * 2 + data.Length - 1];
var val = lookup[data[0]];
result[0] = (char)val;
result[1] = (char)(val >> 16);
for (var i = 1; i < data.Length; i++)
{
val = lookup[data[i]];
result[3 * i - 1] = ' ';
result[3 * i] = (char)val;
result[3 * i + 1] = (char)(val >> 16);
}
return new string(result);
}
}
}