forked from IronLanguages/ironpython3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunicodedata.cs
More file actions
440 lines (366 loc) · 15.4 KB
/
unicodedata.cs
File metadata and controls
440 lines (366 loc) · 15.4 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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
//
// Copyright (c) Jeff Hardy 2011.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
[assembly: PythonModule("unicodedata", typeof(IronPython.Modules.unicodedata))]
namespace IronPython.Modules {
public static class unicodedata {
private static UCD ucd_5_2_0 = null;
// This is supposed to by the Unicode 3.2 database, but since .NET doesn't provide it
// just use the 5.2 one instead.
public static UCD ucd_3_2_0 {
get {
if (_ucd_3_2_0 == null) {
Interlocked.CompareExchange(ref _ucd_3_2_0, new UCD("3.2.0"), null);
}
return _ucd_3_2_0;
}
}
private static UCD _ucd_3_2_0 = null;
public static string unidata_version { get { return ucd_5_2_0.unidata_version; } }
[SpecialName]
public static void PerformModuleReload(PythonContext/*!*/ context, IDictionary/*!*/ dict) {
if (ucd_5_2_0 == null) {
// This is a lie. The version of Unicode depends on the .NET version as well as the OS. The
// version of the database stored internally is 5.2, so just say that.
Interlocked.CompareExchange(ref ucd_5_2_0, new UCD("5.2.0"), null);
}
}
public static string lookup(string name) {
return ucd_5_2_0.lookup(name);
}
public static string name(char unichr, string @default = null) {
return ucd_5_2_0.name(unichr, @default);
}
public static int @decimal(char unichr, int @default) {
return ucd_5_2_0.@decimal(unichr, @default);
}
public static int @decimal(char unichr) {
return ucd_5_2_0.@decimal(unichr);
}
public static object @decimal(char unichr, object @default) {
return ucd_5_2_0.@decimal(unichr, @default);
}
public static int digit(char unichr, int @default) {
return ucd_5_2_0.digit(unichr, @default);
}
public static object digit(char unichr, object @default) {
return ucd_5_2_0.digit(unichr, @default);
}
public static int digit(char unichr) {
return ucd_5_2_0.digit(unichr);
}
public static double numeric(char unichr, double @default) {
return ucd_5_2_0.numeric(unichr, @default);
}
public static double numeric(char unichr) {
return ucd_5_2_0.numeric(unichr);
}
public static object numeric(char unichr, object @default) {
return ucd_5_2_0.numeric(unichr, @default);
}
public static string category(char unichr) {
return ucd_5_2_0.category(unichr);
}
public static string bidirectional(char unichr) {
return ucd_5_2_0.bidirectional(unichr);
}
public static int combining(char unichr) {
return ucd_5_2_0.combining(unichr);
}
public static string east_asian_width(char unichr) {
return ucd_5_2_0.east_asian_width(unichr);
}
public static int mirrored(char unichr) {
return ucd_5_2_0.mirrored(unichr);
}
public static string decomposition(char unichr) {
return ucd_5_2_0.decomposition(unichr);
}
public static string normalize(string form, string unistr) {
return ucd_5_2_0.normalize(form, unistr);
}
[PythonType("unicodedata.UCD")]
public class UCD {
private const string UnicodedataResourceName = "IronPython.Modules.unicodedata.IPyUnicodeData.txt.gz";
private const string OtherNotAssigned = "Cn";
private Dictionary<int, CharInfo> database;
private List<RangeInfo> ranges;
private Dictionary<string, int> nameLookup;
public UCD(string version) {
unidata_version = version;
EnsureLoaded();
}
public string unidata_version { get; private set; }
public string lookup(string name) {
return char.ConvertFromUtf32(nameLookup[name]);
}
public string name(char unichr, string @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
return info.Name;
}
return @default;
}
public string name(char unichr) {
if (TryGetInfo(unichr, out CharInfo info)) {
return info.Name;
}
throw PythonOps.ValueError("no such name");
}
public int @decimal(char unichr, int @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Decimal;
if (d.HasValue) {
return d.Value;
}
}
return @default;
}
public int @decimal(char unichr) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Decimal;
if (d.HasValue) {
return d.Value;
}
}
throw PythonOps.ValueError("not a decimal");
}
public object @decimal(char unichr, object @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Decimal;
if (d.HasValue) {
return d.Value;
}
}
return @default;
}
public int digit(char unichr, int @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Digit;
if (d.HasValue) {
return d.Value;
}
}
return @default;
}
public object digit(char unichr, object @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Digit;
if (d.HasValue) {
return d.Value;
}
}
return @default;
}
public int digit(char unichr) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Digit;
if (d.HasValue) {
return d.Value;
}
}
throw PythonOps.ValueError("not a digit");
}
public double numeric(char unichr, double @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Numeric;
if (d.HasValue) {
return d.Value;
}
}
return @default;
}
public double numeric(char unichr) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Numeric;
if (d.HasValue) {
return d.Value;
}
}
throw PythonOps.ValueError("not a numeric character");
}
public object numeric(char unichr, object @default) {
if (TryGetInfo(unichr, out CharInfo info)) {
var d = info.Numeric_Value_Numeric;
if (d.HasValue) {
return d.Value;
}
}
return @default;
}
public string category(char unichr) {
if (TryGetInfo(unichr, out CharInfo info))
return info.General_Category;
return OtherNotAssigned;
}
public string bidirectional(char unichr) {
if (TryGetInfo(unichr, out CharInfo info))
return info.Bidi_Class;
return string.Empty;
}
public int combining(char unichr) {
if (TryGetInfo(unichr, out CharInfo info))
return info.Canonical_Combining_Class;
return 0;
}
public string east_asian_width(char unichr) {
if (TryGetInfo(unichr, out CharInfo info))
return info.East_Asian_Width;
return string.Empty;
}
public int mirrored(char unichr) {
if (TryGetInfo(unichr, out CharInfo info))
return info.Bidi_Mirrored;
return 0;
}
public string decomposition(char unichr) {
if (TryGetInfo(unichr, out CharInfo info))
return info.Decomposition_Type;
return string.Empty;
}
public string normalize(string form, string unistr) {
NormalizationForm nf;
switch (form) {
case "NFC":
nf = NormalizationForm.FormC;
break;
case "NFD":
nf = NormalizationForm.FormD;
break;
case "NFKC":
nf = NormalizationForm.FormKC;
break;
case "NFKD":
nf = NormalizationForm.FormKD;
break;
default:
throw new ArgumentException("Invalid normalization form " + form, nameof(form));
}
return unistr.Normalize(nf);
}
private void BuildDatabase(StreamReader data) {
var sep = new char[] { ';' };
database = new Dictionary<int, CharInfo>();
ranges = new List<RangeInfo>();
foreach (string raw_line in data.ReadLines()) {
int n = raw_line.IndexOf('#');
string line = n == -1 ? raw_line : raw_line.Substring(raw_line.Length - n).Trim();
if (string.IsNullOrEmpty(line))
continue;
var info = line.Split(sep, 2);
var m = Regex.Match(info[0], @"([0-9a-fA-F]{4})\.\.([0-9a-fA-F]{4})");
if (m.Success) {
// this is a character range
int first = Convert.ToInt32(m.Groups[1].Value, 16);
int last = Convert.ToInt32(m.Groups[2].Value, 16);
ranges.Add(new RangeInfo(first, last, info[1].Split(sep)));
} else {
// this is a single character
database[Convert.ToInt32(info[0], 16)] = new CharInfo(info[1].Split(sep));
}
}
}
private void BuildNameLookup() {
nameLookup = database.Where(c => !c.Value.Name.StartsWith("<")).ToDictionary(c => c.Value.Name, c => c.Key, StringComparer.OrdinalIgnoreCase);
}
private bool TryGetInfo(char unichr, out CharInfo charInfo) {
if (database.TryGetValue(unichr, out charInfo)) return true;
foreach (var range in ranges) {
if (range.First <= unichr && unichr <= range.Last) {
charInfo = range;
return true;
}
}
return false;
}
private void EnsureLoaded() {
if (database == null || nameLookup == null) {
var rsrc = typeof(unicodedata).Assembly.GetManifestResourceStream(UnicodedataResourceName);
var gzip = new GZipStream(rsrc, CompressionMode.Decompress);
var data = new StreamReader(gzip, Encoding.UTF8);
BuildDatabase(data);
BuildNameLookup();
}
}
}
}
internal static class StreamReaderExtensions {
public static IEnumerable<string> ReadLines(this StreamReader reader) {
string line;
while ((line = reader.ReadLine()) != null)
yield return line;
}
}
internal class CharInfo {
internal CharInfo(string[] info) {
Name = info[PropertyIndex.Name].ToUpperInvariant();
General_Category = info[PropertyIndex.General_Category];
Canonical_Combining_Class = int.Parse(info[PropertyIndex.Canonical_Combining_Class]);
Bidi_Class = info[PropertyIndex.Bidi_Class];
Decomposition_Type = info[PropertyIndex.Decomposition_Type];
string nvdes = info[PropertyIndex.Numeric_Value_Decimal];
Numeric_Value_Decimal = nvdes != "" ? (int?)int.Parse(nvdes) : null;
string nvdis = info[PropertyIndex.Numeric_Value_Digit];
Numeric_Value_Digit = nvdis != "" ? (int?)int.Parse(nvdis) : null;
string nvns = info[PropertyIndex.Numeric_Value_Numeric];
if (nvns != "") {
string[] nvna = nvns.Split(new char[] { '/' });
double num = double.Parse(nvna[0]);
if (nvna.Length > 1) {
double den = double.Parse(nvna[1]);
num /= den;
}
Numeric_Value_Numeric = num;
} else {
Numeric_Value_Numeric = null;
}
Bidi_Mirrored = info[PropertyIndex.Bidi_Mirrored] == "Y" ? 1 : 0;
East_Asian_Width = info[PropertyIndex.East_Asian_Width];
}
internal readonly string Name;
internal readonly string General_Category;
internal readonly int Canonical_Combining_Class;
internal readonly string Bidi_Class;
internal readonly string Decomposition_Type;
internal readonly int? Numeric_Value_Decimal;
internal readonly int? Numeric_Value_Digit;
internal readonly double? Numeric_Value_Numeric;
internal readonly int Bidi_Mirrored;
internal readonly string East_Asian_Width;
private static class PropertyIndex {
internal const int Name = 0;
internal const int General_Category = 1;
internal const int Canonical_Combining_Class = 2;
internal const int Bidi_Class = 3;
internal const int Decomposition_Type = 4;
internal const int Numeric_Value_Decimal = 5;
internal const int Numeric_Value_Digit = 6;
internal const int Numeric_Value_Numeric = 7;
internal const int Bidi_Mirrored = 8;
internal const int East_Asian_Width = 9;
}
}
internal class RangeInfo : CharInfo {
internal RangeInfo(int first, int last, string[] info)
: base(info) {
this.First = first;
this.Last = last;
}
internal readonly int First;
internal readonly int Last;
}
}