/
Codepoint.cs
264 lines (226 loc) ยท 7.56 KB
/
Codepoint.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
๏ปฟusing System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Globalization;
namespace NeoSmart.Unicode
{
public struct Codepoint : IComparable<Codepoint>, IComparable<UInt32>, IEquatable<Codepoint>,
IEquatable<string>, IComparable<string>, IEquatable<char>
{
public readonly UInt32 Value;
public Codepoint(UInt32 value)
{
Value = value;
}
public Codepoint(long value)
: this ((UInt32) value)
{ }
/// <summary>
/// Create a unicode codepoint from hexadecimal representation, supporting U+xxxx and 0xYYYY notation.
/// </summary>
/// <param name="hexValue"></param>
public Codepoint(string hexValue)
{
if ((hexValue.StartsWith("0x") || hexValue.StartsWith("U+") || hexValue.StartsWith("u+")))
{
hexValue = hexValue.Substring(2);
}
if (!uint.TryParse(hexValue, NumberStyles.HexNumber, CultureInfo.CurrentCulture.NumberFormat, out Value))
{
throw new UnsupportedCodepointException();
}
}
public uint AsUtf32() => Value;
/// <summary>
/// Returns an iterator that will enumerate over the big endian bytes in the UTF32 encoding of this codepoint.
/// </summary>
public IEnumerable<byte> AsUtf32Bytes()
{
//from highest to lowest
var utf32 = AsUtf32();
var b1 = (byte) (utf32 >> 24);
yield return b1;
var b2 = (byte) ((utf32 & 0x00FFFFFF) >> 16);
yield return b2;
var b3 = (byte) (((UInt16) utf32) >> 8);
yield return b3;
var b4 = (byte) utf32;
yield return b4;
}
//https://en.wikipedia.org/wiki/UTF-16
public IEnumerable<ushort> AsUtf16()
{
//U+0000 to U+D7FF and U+E000 to U+FFFF
if (Value <= 0xFFFF)
{
yield return (UInt16) Value;
}
//U+10000 to U+10FFFF
else if (Value >= 0x10000 && Value <= 0x10FFFF)
{
UInt32 newVal = Value - 0x010000; //leaving 20 bits
UInt16 high = (UInt16) ((newVal >> 10) + 0xD800);
System.Diagnostics.Debug.Assert(high <= 0xDBFF && high >= 0xD800);
yield return high;
UInt16 low = (UInt16) ((newVal & 0x03FF) + 0xDC00);
System.Diagnostics.Debug.Assert(low <= 0xDFFF && low >= 0xDC00);
yield return low;
}
else
{
throw new UnsupportedCodepointException();
}
}
/// <summary>
/// Returns an iterator that will enumerate over the big endian bytes in the UTF16 encoding of this codepoint.
/// </summary>
public IEnumerable<byte> AsUtf16Bytes()
{
var utf16 = AsUtf16();
foreach (var u16 in utf16)
{
var high = (byte) (u16 >> 8);
yield return high;
var low = (byte) u16;
yield return low;
}
}
//https://en.wikipedia.org/wiki/UTF-8
public IEnumerable<byte> AsUtf8()
{
//up to 7 bits
if (Value <= 0x007F)
{
yield return (byte) Value;
yield break;
}
//up to 11 bits
if (Value <= 0x07FF)
{
yield return (byte) (0b11000000 | (0b00011111 & (Value >> 6))); //tag + upper 5 bits
yield return (byte) (0b10000000 | (0b00111111 & Value)); //tag + lower 6 bits
yield break;
}
//up to 16 bits
if (Value <= 0x0FFFF)
{
yield return (byte) (0b11100000 | (0b00001111 & (Value >> 12))); //tag + upper 4 bits
yield return (byte) (0b10000000 | (0b00111111 & (Value >> 6))); //tag + next 6 bits
yield return (byte) (0b10000000 | (0b00111111 & Value)); //tag + last 6 bits
yield break;
}
//up to 21 bits
if (Value <= 0x1FFFFF)
{
yield return (byte) (0b11110000 | (0b00000111 & (Value >> 18))); //tag + upper 3 bits
yield return (byte) (0b10000000 | (0b00111111 & (Value >> 12))); //tag + next 6 bits
yield return (byte) (0b10000000 | (0b00111111 & (Value >> 6))); //tag + next 6 bits
yield return (byte) (0b10000000 | (0b00111111 & Value)); //tag + last 6 bits
yield break;
}
throw new UnsupportedCodepointException();
}
public int CompareTo(Codepoint other)
{
return Value.CompareTo(other.Value);
}
public int CompareTo(UInt32 other)
{
return Value.CompareTo(other);
}
public bool Equals(Codepoint other)
{
return Value == other.Value;
}
public override bool Equals(object obj)
{
if (obj is Codepoint)
{
return Value == ((Codepoint)obj).Value;
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static bool operator== (Codepoint a, Codepoint b)
{
return a.Value == b.Value;
}
public static bool operator!= (Codepoint a, Codepoint b)
{
return a.Value != b.Value;
}
public static bool operator <(Codepoint a, Codepoint b)
{
return a.Value < b.Value;
}
public static bool operator >(Codepoint a, Codepoint b)
{
return a.Value > b.Value;
}
public static bool operator >=(Codepoint a, Codepoint b)
{
return a.Value >= b.Value;
}
public static bool operator <=(Codepoint a, Codepoint b)
{
return a.Value <= b.Value;
}
public static implicit operator UInt32(Codepoint codepoint)
{
return codepoint.Value;
}
public static implicit operator Codepoint(UInt32 value)
{
return new Codepoint(value);
}
public override string ToString()
{
return $"U+{Value.ToString("X")}";
}
public string AsString()
{
return Encoding.UTF8.GetString(AsUtf8().ToArray());
}
public bool IsIn(Range range)
{
return range.Contains(this);
}
public bool IsIn(MultiRange multirange)
{
return multirange.Contains(this);
}
public bool Equals(string other)
{
return AsString() == other;
}
public int CompareTo(string other)
{
return AsString().CompareTo(other);
}
public bool Equals(char other)
{
var s = AsString();
return s.Count() == 1 && s[0] == other;
}
public static bool operator ==(Codepoint a, string b)
{
return a.Equals(b);
}
public static bool operator !=(Codepoint a, string b)
{
return !a.Equals(b);
}
public static bool operator ==(Codepoint a, char b)
{
return a.Equals(b);
}
public static bool operator !=(Codepoint a, char b)
{
return !a.Equals(b);
}
}
}