-
-
Notifications
You must be signed in to change notification settings - Fork 75
Expand file tree
/
Copy pathExtensions.cs
More file actions
79 lines (69 loc) · 2.03 KB
/
Extensions.cs
File metadata and controls
79 lines (69 loc) · 2.03 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
/*
* The contents of this file are subject to the Initial
* Developer's Public License Version 1.0 (the "License");
* you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
* https://github.com/FirebirdSQL/NETProvider/raw/master/license.txt.
*
* Software distributed under the License is distributed on
* an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
* express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* All Rights Reserved.
*/
//$Authors = Jiri Cincura (jiri@cincura.net)
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
namespace FirebirdSql.Data.Common;
internal static class Extensions
{
public static int AsInt(this IntPtr ptr)
{
return (int)ptr.ToInt64();
}
public static IntPtr ReadIntPtr(this BinaryReader self)
{
if (IntPtr.Size == sizeof(int))
{
return new IntPtr(self.ReadInt32());
}
else if (IntPtr.Size == sizeof(long))
{
return new IntPtr(self.ReadInt64());
}
else
{
throw new NotSupportedException();
}
}
public static string ToHexString(this byte[] b)
{
return BitConverter.ToString(b).Replace("-", string.Empty);
}
public static IDictionary<short, ulong> GetTableStatistics(this byte[] b, int length)
{
var capacity = length > 3 ? (length - 3) / 6 + 1 : 0;
var tableStatistics = new Dictionary<short, ulong>(capacity);
for (var i = 3; i < length; i += 6)
{
var tableId = (short)IscHelper.VaxInteger(b, i, 2);
var count = (ulong)IscHelper.VaxInteger(b, i + 2, 4);
tableStatistics.Add(tableId, count);
}
return tableStatistics;
}
public static IEnumerable<IEnumerable<T>> Split<T>(this T[] array, int size)
{
for (var i = 0; i < (float)array.Length / size; i++)
{
yield return array.Skip(i * size).Take(size);
}
}
#if NETSTANDARD2_0
public static HashSet<T> ToHashSet<T>(this IEnumerable<T> source) => new HashSet<T>(source);
#endif
}