File: Normalization.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (574 lines) | stat: -rw-r--r-- 14,868 bytes parent folder | download | duplicates (7)
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
using System;
using System.Globalization;
using System.Text;
using System.Runtime.CompilerServices;

using NUtil = Mono.Globalization.Unicode.NormalizationTableUtil;

namespace System.Text
{
	internal enum NormalizationCheck {
		Yes,
		No,
		Maybe
	}

	internal unsafe class Normalization
	{
		public const int NoNfd = 1;
		public const int NoNfkd = 2;
		public const int NoNfc = 4;
		public const int MaybeNfc = 8;
		public const int NoNfkc = 16;
		public const int MaybeNfkc = 32;
		public const int FullCompositionExclusion = 64;
		public const int IsUnsafe = 128;
//		public const int ExpandOnNfd = 256;
//		public const int ExpandOnNfc = 512;
//		public const int ExpandOnNfkd = 1024;
//		public const int ExpandOnNfkc = 2048;

		static uint PropValue (int cp)
		{
			return props [NUtil.PropIdx (cp)];
		}

		static int CharMapIdx (int cp)
		{
			return charMapIndex [NUtil.MapIdx (cp)];
		}

		static byte GetCombiningClass (int c)
		{
			return combiningClass [NUtil.Combining.ToIndex (c)];
		}

		static int GetPrimaryCompositeFromMapIndex (int src)
		{
			return mapIdxToComposite [NUtil.Composite.ToIndex (src)];
		}

		static int GetPrimaryCompositeHelperIndex (int cp)
		{
			return helperIndex [NUtil.Helper.ToIndex (cp)];
		}

		private static string Compose (string source, int checkType)
		{
			StringBuilder sb = null;
			// Decompose to NFD or NKFD depending on our target
			Decompose (source, ref sb, checkType == 2 ? 3 : 1);
			if (sb == null)
				sb = Combine (source, 0, checkType);
			else
				Combine (sb, 0, checkType);

			return sb != null ? sb.ToString () : source;
		}

		private static StringBuilder Combine (string source, int start, int checkType)
		{
			for (int i = 0; i < source.Length; i++) {
				if (QuickCheck (source [i], checkType) == NormalizationCheck.Yes)
					continue;
				StringBuilder sb = new StringBuilder (source.Length + source.Length / 10);
				sb.Append (source);
				Combine (sb, i, checkType);
				return sb;
			}
			return null;
		}

/*
		private static bool CanBePrimaryComposite (int i)
		{
			if (i >= 0x3400 && i <= 0x9FBB)
				return GetPrimaryCompositeHelperIndex (i) != 0;
			return (PropValue (i) & IsUnsafe) != 0;
		}
*/
		private static void Combine (StringBuilder sb, int i, int checkType)
		{
			// Back off one character as we may be looking at a V or T jamo.
			CombineHangul (sb, null, i > 0 ? i - 1 : i);

			while (i < sb.Length) {
				if (QuickCheck (sb [i], checkType) == NormalizationCheck.Yes) {
					i++;
					continue;
				}

				i = TryComposeWithPreviousStarter (sb, null, i);
			}
		}

		private static int CombineHangul (StringBuilder sb, string s, int current)
		{
			int length = sb != null ? sb.Length : s.Length;
			int last = Fetch (sb, s, current);

			for (int i = current + 1; i < length; ++i) {
				int ch = Fetch (sb, s, i);

				// 1. check to see if two current characters are L and V

				int LIndex = last - HangulLBase;
				if (0 <= LIndex && LIndex < HangulLCount) {
					int VIndex = ch - HangulVBase;
					if (0 <= VIndex && VIndex < HangulVCount) {
						if (sb == null)
							return -1;

						// make syllable of form LV

						last = HangulSBase + (LIndex * HangulVCount + VIndex) * HangulTCount;

						sb [i - 1] = (char) last; // reset last
						sb.Remove (i, 1);
						i--; length--;
						continue; // discard ch
					}
				}


				// 2. check to see if two current characters are LV and T

				int SIndex = last - HangulSBase;
				if (0 <= SIndex && SIndex < HangulSCount && (SIndex % HangulTCount) == 0) {
					int TIndex = ch - HangulTBase;
					if (0 < TIndex && TIndex < HangulTCount) {
						if (sb == null)
							return -1;

						// make syllable of form LVT

						last += TIndex;

						sb [i - 1] = (char) last; // reset last
						sb.Remove (i, 1);
						i--; length--;
						continue; // discard ch
					}
				}
				// if neither case was true, just add the character
				last = ch;
			}

			return length;
		}

		static int Fetch (StringBuilder sb, string s, int i)
		{
			return (int) (sb != null ? sb [i] : s [i]);
		}

		// Cf. figure 7, section 1.3 of http://unicode.org/reports/tr15/.
		static int TryComposeWithPreviousStarter (StringBuilder sb, string s, int current)
		{
			// Backtrack to previous starter.
			int i = current - 1;
			if (GetCombiningClass (Fetch (sb, s, current)) == 0) {
				if (i < 0 || GetCombiningClass (Fetch (sb, s, i)) != 0)
					return current + 1;
			} else {
				while (i >= 0 && GetCombiningClass (Fetch (sb, s, i)) != 0)
					i--;
				if (i < 0)
					return current + 1;
			}

			int starter = Fetch (sb, s, i);

			// The various decompositions involving starter follow this index.
			int comp_idx = GetPrimaryCompositeHelperIndex (starter);
			if (comp_idx == 0)
				return current + 1;

			int length = (sb != null ? sb.Length : s.Length);
			int prevCombiningClass = -1;
			for (int j = i + 1; j < length; j++) {
				int candidate = Fetch (sb, s, j);

				int combiningClass = GetCombiningClass (candidate);
				if (combiningClass == prevCombiningClass)
					// We skipped over a guy with the same class, without
					// combining.  Skip this one, too.
					continue;

				int composed = TryCompose (comp_idx, starter, candidate);
				if (composed != 0) {
					if (sb == null)
						// Not normalized, and we are only checking.
						return -1;

					// Full Unicode warning: This will break when the underlying
					// tables are extended.
					sb [i] = (char) composed;
					sb.Remove (j, 1);

					return current;
				}

				// Gray box.  We're done.
				if (combiningClass == 0)
					return j + 1;

				prevCombiningClass = combiningClass;
			}

			return length;
		}

		static int TryCompose (int i, int starter, int candidate)
		{
			while (mappedChars [i] == starter) {
				if (mappedChars [i + 1] == candidate &&
				    mappedChars [i + 2] == 0) {
					int composed = GetPrimaryCompositeFromMapIndex (i);

					if ((PropValue (composed) & FullCompositionExclusion) == 0)
						return composed;
				}

				// Skip this entry.
				while (mappedChars [i] != 0)
					i++;
				i++;
			}

			return 0;
		}

		static string Decompose (string source, int checkType)
		{
			StringBuilder sb = null;
			Decompose (source, ref sb, checkType);
			return sb != null ? sb.ToString () : source;
		}

		static void Decompose (string source,
			ref StringBuilder sb, int checkType)
		{
			int [] buf = null;
			int start = 0;
			for (int i = 0; i < source.Length; i++)
				if (QuickCheck (source [i], checkType) == NormalizationCheck.No)
					DecomposeChar (ref sb, ref buf, source,
						i, checkType, ref start);
			if (sb != null)
				sb.Append (source, start, source.Length - start);
			ReorderCanonical (source, ref sb, 1);
		}

		static void ReorderCanonical (string src, ref StringBuilder sb, int start)
		{
			if (sb == null) {
				// check only with src.
				for (int i = 1; i < src.Length; i++) {
					int level = GetCombiningClass (src [i]);
					if (level == 0)
						continue;
					if (GetCombiningClass (src [i - 1]) > level) {
						sb = new StringBuilder (src.Length);
						sb.Append (src, 0, src.Length);
						ReorderCanonical (src, ref sb, i);
						return;
					}
				}
				return;
			}
			// check only with sb
			for (int i = start; i < sb.Length; ) {
				int level = GetCombiningClass (sb [i]);
				if (level == 0 || GetCombiningClass (sb [i - 1]) <= level) {
					i++;
					continue;
				}

				char c = sb [i - 1];
				sb [i - 1] = sb [i];
				sb [i] = c;
				// Apply recursively.
				if (i > 1)
					i--;
			}
		}

		static void DecomposeChar (ref StringBuilder sb,
			ref int [] buf, string s, int i, int checkType, ref int start)
		{
			if (sb == null)
				sb = new StringBuilder (s.Length + 100);
			sb.Append (s, start, i - start);
			if (buf == null)
				buf = new int [19];
			int n = GetCanonical (s [i], buf, 0, checkType);
			for (int x = 0; x < n; x++) {
				if (buf [x] < char.MaxValue)
					sb.Append ((char) buf [x]);
				else { // surrogate
					sb.Append ((char) (buf [x] >> 10 + 0xD800));
					sb.Append ((char) ((buf [x] & 0x0FFF) + 0xDC00));
				}
			}
			start = i + 1;
		}

		public static NormalizationCheck QuickCheck (char c, int type)
		{
			uint v;
			switch (type) {
			default: // NFC
				v = PropValue ((int) c);
				return (v & NoNfc) == 0 ?
					(v & MaybeNfc) == 0 ?
					NormalizationCheck.Yes :
					NormalizationCheck.Maybe :
					NormalizationCheck.No;
			case 1: // NFD
				if ('\uAC00' <= c && c <= '\uD7A3')
					return NormalizationCheck.No;
				return (PropValue ((int) c) & NoNfd) != 0 ?
					NormalizationCheck.No : NormalizationCheck.Yes;
			case 2: // NFKC
				v = PropValue ((int) c);
				return (v & NoNfkc) != 0 ? NormalizationCheck.No :
					(v & MaybeNfkc) != 0 ?
					NormalizationCheck.Maybe :
					NormalizationCheck.Yes;
			case 3: // NFKD
				if ('\uAC00' <= c && c <= '\uD7A3')
					return NormalizationCheck.No;
				return (PropValue ((int) c) & NoNfkd) != 0 ?
					NormalizationCheck.No : NormalizationCheck.Yes;
			}
		}

		/* for now we don't use FC_NFKC closure
		public static bool IsMultiForm (char c)
		{
			return (PropValue ((int) c) & 0xF0000000) != 0;
		}

		public static char SingleForm (char c)
		{
			uint v = PropValue ((int) c);
			int idx = (int) ((v & 0x7FFF0000) >> 16);
			return (char) singleNorm [idx];
		}

		public static void MultiForm (char c, char [] buf, int index)
		{
			// FIXME: handle surrogate
			uint v = PropValue ((int) c);
			int midx = (int) ((v & 0x7FFF0000) >> 16);
			buf [index] = (char) multiNorm [midx];
			buf [index + 1] = (char) multiNorm [midx + 1];
			buf [index + 2] = (char) multiNorm [midx + 2];
			buf [index + 3] = (char) multiNorm [midx + 3];
			if (buf [index + 3] != 0)
				buf [index + 4] = (char) 0; // zero termination
		}
		*/

		const int HangulSBase = 0xAC00, HangulLBase = 0x1100,
				  HangulVBase = 0x1161, HangulTBase = 0x11A7,
				  HangulLCount = 19, HangulVCount = 21, HangulTCount = 28,
				  HangulNCount = HangulVCount * HangulTCount,   // 588
				  HangulSCount = HangulLCount * HangulNCount;   // 11172

		private static int GetCanonicalHangul (int s, int [] buf, int bufIdx)
		{
			int idx = s - HangulSBase;
			if (idx < 0 || idx >= HangulSCount) {
				return bufIdx;
			}

			int L = HangulLBase + idx / HangulNCount;
			int V = HangulVBase + (idx % HangulNCount) / HangulTCount;
			int T = HangulTBase + idx % HangulTCount;

			buf [bufIdx++] = L;
			buf [bufIdx++] = V;
			if (T != HangulTBase) {
				buf [bufIdx++] = T;
			}
			buf [bufIdx] = (char) 0;
			return bufIdx;
		}

		static int GetCanonical (int c, int [] buf, int bufIdx, int checkType)
		{
			int newBufIdx = GetCanonicalHangul (c, buf, bufIdx);
			if (newBufIdx > bufIdx)
				return newBufIdx;
 
			int i = CharMapIdx (c);
			if (i == 0 || mappedChars [i] == c)
				buf [bufIdx++] = c;
			else {
				// Character c maps to one or more decomposed chars.
				for (; mappedChars [i] != 0; i++) {
					int nth = mappedChars [i];

					// http://www.unicode.org/reports/tr15/tr15-31.html, 1.3:
					// Full decomposition involves recursive application of the
					// Decomposition_Mapping values.  Note that QuickCheck does
					// not currently support astral plane codepoints.
					if (nth <= 0xffff && QuickCheck ((char)nth, checkType) == NormalizationCheck.Yes)
						buf [bufIdx++] = nth;
					else
						bufIdx = GetCanonical (nth, buf, bufIdx, checkType);
				}
			}

			return bufIdx;
		}

		public static bool IsNormalized (string source, NormalizationForm normalizationForm)
		{
			switch (normalizationForm) {
			default:
				return IsNormalized (source, 0);
			case NormalizationForm.FormD:
				return IsNormalized (source, 1);
			case NormalizationForm.FormKC:
				return IsNormalized (source, 2);
			case NormalizationForm.FormKD:
				return IsNormalized (source, 3);
			}
		}

		public static bool IsNormalized (string source, int type)
		{
			int prevCC = -1;
			for (int i = 0; i < source.Length; ) {
				int cc = GetCombiningClass (source [i]);
				if (cc != 0 && cc < prevCC)
					return false;
				prevCC = cc;

				switch (QuickCheck (source [i], type)) {
				case NormalizationCheck.Yes:
					i++;
					break;
				case NormalizationCheck.No:
					return false;
				case NormalizationCheck.Maybe:
					// for those forms with composition, it cannot be checked here
					switch (type) {
					case 0: // NFC
					case 2: // NFKC
						return source == Normalize (source, type);
					}
					// go on...

					i = CombineHangul (null, source, i > 0 ? i - 1 : i);
					if (i < 0)
						return false;

					i = TryComposeWithPreviousStarter (null, source, i);
					if (i < 0)
						return false;
					break;
				}
			}
			return true;
		}

		public static string Normalize (string source, NormalizationForm normalizationForm)
		{
			switch (normalizationForm) {
			default:
				return Normalization.Normalize (source, 0);
			case NormalizationForm.FormD:
				return Normalization.Normalize (source, 1);
			case NormalizationForm.FormKC:
				return Normalization.Normalize (source, 2);
			case NormalizationForm.FormKD:
				return Normalization.Normalize (source, 3);
			}
		}

		public static string Normalize (string source, int type)
		{
			switch (type) {
			default:
			case 2:
				return Compose (source, type);
			case 1:
			case 3:
				return Decompose (source, type);
			}
		}

		static byte* props;
		static int* mappedChars;
		static short* charMapIndex;
		static short* helperIndex;
		static ushort* mapIdxToComposite;
		static byte* combiningClass;

#if GENERATE_TABLE

		public static readonly bool IsReady = true; // always

		static Normalization ()
		{
			fixed (byte* tmp = propsArr) {
				props = tmp;
			}
			fixed (int* tmp = mappedCharsArr) {
				mappedChars = tmp;
			}
			fixed (short* tmp = charMapIndexArr) {
				charMapIndex = tmp;
			}
			fixed (short* tmp = helperIndexArr) {
				helperIndex = tmp;
			}
			fixed (ushort* tmp = mapIdxToCompositeArr) {
				mapIdxToComposite = tmp;
			}
			fixed (byte* tmp = combiningClassArr) {
				combiningClass = tmp;
			}
		}
#else

		static object forLock = new object ();
		public static readonly bool isReady;

		public static bool IsReady {
			get { return isReady; }
		}

		[MethodImpl (MethodImplOptions.InternalCall)]
		static extern void load_normalization_resource (
			out IntPtr props, out IntPtr mappedChars,
			out IntPtr charMapIndex, out IntPtr helperIndex,
			out IntPtr mapIdxToComposite, out IntPtr combiningClass);

		static Normalization ()
		{
			IntPtr p1, p2, p3, p4, p5, p6;
			lock (forLock) {
				load_normalization_resource (out p1, out p2, out p3, out p4, out p5, out p6);
				props = (byte*) p1;
				mappedChars = (int*) p2;
				charMapIndex = (short*) p3;
				helperIndex = (short*) p4;
				mapIdxToComposite = (ushort*) p5;
				combiningClass = (byte*) p6;
			}

			isReady = true;
		}
	}
}
#endif

		//
		// autogenerated code or icall to fill array runs here
		//