-
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathExtensions.cs
More file actions
161 lines (144 loc) · 3.13 KB
/
Extensions.cs
File metadata and controls
161 lines (144 loc) · 3.13 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* 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.Globalization;
using System.IO;
using System.Linq;
using System.Text;
namespace FirebirdSql.Data.Common;
internal static class Extensions
{
extension(IntPtr ptr)
{
public int AsInt()
{
return (int)ptr.ToInt64();
}
}
extension(BinaryReader binaryReader)
{
public IntPtr ReadIntPtr()
{
if (IntPtr.Size == sizeof(int))
{
return new IntPtr(binaryReader.ReadInt32());
}
else if (IntPtr.Size == sizeof(long))
{
return new IntPtr(binaryReader.ReadInt64());
}
else
{
throw new NotSupportedException();
}
}
}
extension(byte[] b)
{
public string ToHexString()
{
return Convert.ToHexString(b);
}
}
extension<T>(T[] array)
{
public IEnumerable<IEnumerable<T>> Split(int size)
{
for (var i = 0; i < (float)array.Length / size; i++)
{
yield return array.Skip(i * size).Take(size);
}
}
}
extension(string s)
{
public IEnumerable<char[]> EnumerateRunesToChars()
{
if (s == null)
throw new ArgumentNullException(nameof(s));
return s.EnumerateRunes().Select(r =>
{
var result = new char[r.Utf16SequenceLength];
r.EncodeToUtf16(result);
return result;
});
}
}
extension(Encoding)
{
public static Encoding GetANSIEncoding()
{
try
{
return Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.ANSICodePage);
}
catch (Exception)
{
return Encoding.Default;
}
}
}
public static int CountRunes(this ReadOnlySpan<char> text)
{
var length = text.Length;
if(length == 0)
return 0;
var i = text.IndexOfAnyInRange('\uD800', '\uDBFF');
if(i < 0)
return length;
var count = i;
while(i < length)
{
if(char.IsHighSurrogate(text[i]) && i + 1 < length && char.IsLowSurrogate(text[i + 1]))
{
i += 2;
}
else
{
i++;
}
count++;
}
return count;
}
public static ReadOnlySpan<char> TruncateStringToRuneCount(this ReadOnlySpan<char> text, int maxRuneCount)
{
if(maxRuneCount <= 0 || text.IsEmpty)
return ReadOnlySpan<char>.Empty;
var length = text.Length;
if(maxRuneCount >= length)
return text;
var prefix = text[..maxRuneCount];
var i = prefix.IndexOfAnyInRange('\uD800', '\uDBFF');
if(i < 0)
return prefix;
var remaining = maxRuneCount - i;
while(i < length && remaining > 0)
{
if(char.IsHighSurrogate(text[i]) && i + 1 < length && char.IsLowSurrogate(text[i + 1]))
{
i += 2;
}
else
{
i++;
}
remaining--;
}
return text[..i];
}
}