forked from SimonRichards/clang-sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Util.cs
102 lines (91 loc) · 3.59 KB
/
Util.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
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
namespace ClangSharp {
public static class Util {
public static IEnumerable<T> Append<T>(this IEnumerable<T> collection, T tail) {
foreach (T val in collection) {
yield return val;
}
yield return tail;
}
public static IEnumerable<T> Prepend<T>(this IEnumerable<T> collection, T head) {
yield return head;
foreach (T val in collection) {
yield return val;
}
}
public static string Substring(this string source, SourceRange range) {
return source.Substring(range.Start.Offset, range.End.Offset - range.Start.Offset);
}
public static T[] Slice<T>(this IList<T> collection, SourceRange range) {
return Slice(collection, range.Start.Offset, range.End.Offset - range.Start.Offset);
}
public static bool All(this IEnumerable<bool> collection) {
foreach (bool item in collection) {
if (!item) {
return false;
}
}
return true;
}
public static T[] Slice<T>(this IList<T> collection, int start, int length) {
var res = new T[length];
for (int index = start, count = 0; count < length; ++count, ++index) {
res[count] = collection[index];
}
return res;
}
static readonly int[][] _boms = new[] {
new[] { 0xef, 0xbb, 0xbf },
new[] { 0xfe, 0xff, 0x00, 0x00 },
new[] { 0x00, 0x00, 0xfe, 0xff },
new[] { 0xff, 0xfe }
};
public static int GetBomLength(this byte[] input) {
foreach (var bom in _boms) {
if (input.Length >= bom.Length && bom.Select((value, i) => input[i] == value).All()) {
return bom.Length;
}
}
return 0;
}
public static IEnumerable<uint> Range(uint start, uint count) {
for (uint i = start, j = 0; j < count; ++i, ++j) {
yield return i;
}
}
public static T MaxBy<T, C>(this IEnumerable<T> collection, Converter<T, C> converter) where C : IComparable {
var enumerator = collection.GetEnumerator();
if (enumerator.MoveNext()) {
T best = enumerator.Current;
C bestScore = converter(best);
while (enumerator.MoveNext()) {
C score = converter(enumerator.Current);
if (score.CompareTo(bestScore) > 0) {
best = enumerator.Current;
}
}
return best;
} else {
throw new ArgumentException("Cannot search empty collection");
}
}
public static T MinBy<T, C>(this IEnumerable<T> collection, Converter<T, C> converter) where C : IComparable {
var enumerator = collection.GetEnumerator();
if (enumerator.MoveNext()) {
T best = enumerator.Current;
C bestScore = converter(best);
while (enumerator.MoveNext()) {
C score = converter(enumerator.Current);
if (score.CompareTo(bestScore) < 0) {
best = enumerator.Current;
}
}
return best;
} else {
throw new ArgumentException("Cannot search empty collection");
}
}
}
}