-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathUtilities.cs
61 lines (51 loc) · 1.61 KB
/
Utilities.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace uTorrentNotifier
{
class Utilities
{
private Utilities()
{
}
public static string FormatBytes(long bytes)
{
const int scale = 1024;
string[] orders = new string[] { "PB", "TB", "GB", "MB", "KB", "B" };
long max = (long)Math.Pow(scale, orders.Length - 1);
foreach (string order in orders)
{
if (bytes > max)
return string.Format("{0:##.##} {1}", decimal.Divide(bytes, max), order);
max /= scale;
}
return "0 B";
}
}
}
namespace ExtensionMethods
{
public static class Extensions
{
public static List<KeyValuePair<string, string>> Merge(this List<KeyValuePair<string, string>> major,
List<KeyValuePair<string, string>> defaults)
{
List<KeyValuePair<string, string>> merged = new List<KeyValuePair<string, string>>();
List<KeyValuePair<string, string>> updated = new List<KeyValuePair<string, string>>();
updated.AddRange(defaults);
foreach (KeyValuePair<string, string> kv in defaults)
{
KeyValuePair<string, string> found = major.Find(item => item.Key == kv.Key);
if (found.Key != null)
{
updated.Remove(kv);
}
}
merged.AddRange(major);
merged.AddRange(updated);
return merged;
}
}
}