ObjFW  Diff

Differences From Artifact [74632c14e6]:

To Artifact [33faa56a1e]:


44
45
46
47
48
49
50
51

52
53
54
55
56
57
58
44
45
46
47
48
49
50

51
52
53
54
55
56
57
58







-
+







@implementation TableGenerator
- (instancetype)init
{
	self = [super init];

	@try {
		_HTTPClient = [[OFHTTPClient alloc] init];
		[_HTTPClient setDelegate: self];
		_HTTPClient.delegate = self;

		_uppercaseTableSize           = SIZE_MAX;
		_lowercaseTableSize           = SIZE_MAX;
		_titlecaseTableSize           = SIZE_MAX;
		_casefoldingTableSize         = SIZE_MAX;
		_decompositionTableSize       = SIZE_MAX;
		_decompositionCompatTableSize = SIZE_MAX;
106
107
108
109
110
111
112
113

114
115
116
117
118
119

120
121
122
123
124
125
126
106
107
108
109
110
111
112

113
114
115
116
117
118

119
120
121
122
123
124
125
126







-
+





-
+







	[of_stdout writeString: @"Parsing UnicodeData.txt…"];

	while ((line = [response readLine]) != nil) {
		void *pool2;
		OFArray OF_GENERIC(OFString *) *components;
		of_unichar_t codePoint;

		if ([line length] == 0)
		if (line.length == 0)
			continue;

		pool2 = objc_autoreleasePoolPush();

		components = [line componentsSeparatedByString: @";"];
		if ([components count] != 15) {
		if (components.count != 15) {
			of_log(@"Invalid line: %@\n", line);
			[OFApplication terminateWithStatus: 1];
		}

		codePoint = (of_unichar_t)
		    [[components objectAtIndex: 0] hexadecimalValue];

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
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







-
+

-
+







-
+








		if ([[components objectAtIndex: 5] length] > 0) {
			OFArray *decomposed = [[components objectAtIndex: 5]
			    componentsSeparatedByString: @" "];
			bool compat = false;
			OFMutableString *string;

			if ([[decomposed firstObject] hasPrefix: @"<"]) {
			if ([decomposed.firstObject hasPrefix: @"<"]) {
				decomposed = [decomposed objectsInRange:
				    of_range(1, [decomposed count] - 1)];
				    of_range(1, decomposed.count - 1)];
				compat = true;
			}

			string = [OFMutableString string];

			for (OFString *character in decomposed) {
				of_unichar_t unichar =
				    (of_unichar_t)[character hexadecimalValue];
				    (of_unichar_t)character.hexadecimalValue;

				[string appendCharacters: &unichar
						  length: 1];
			}

			[string makeImmutable];

185
186
187
188
189
190
191
192

193
194
195
196
197
198

199
200
201
202
203
204
205
185
186
187
188
189
190
191

192
193
194
195
196
197

198
199
200
201
202
203
204
205







-
+





-
+







	[of_stdout writeString: @"Parsing CaseFolding.txt…"];

	while ((line = [response readLine]) != nil) {
		void *pool2;
		OFArray OF_GENERIC(OFString *) *components;
		of_unichar_t codePoint;

		if ([line length] == 0 || [line hasPrefix: @"#"])
		if (line.length == 0 || [line hasPrefix: @"#"])
			continue;

		pool2 = objc_autoreleasePoolPush();

		components = [line componentsSeparatedByString: @"; "];
		if ([components count] != 4) {
		if (components.count != 4) {
			of_log(@"Invalid line: %s\n", line);
			[OFApplication terminateWithStatus: 1];
		}

		if (![[components objectAtIndex: 1] isEqual: @"S"] &&
		    ![[components objectAtIndex: 1] isEqual: @"C"])
			continue;
235
236
237
238
239
240
241
242
243


244
245
246
247
248
249
250
235
236
237
238
239
240
241


242
243
244
245
246
247
248
249
250







-
-
+
+







			OFMutableString *replacement;
			bool changed = false;

			if (table[i] == nil)
				continue;

			pool = objc_autoreleasePoolPush();
			characters = [table[i] characters];
			length = [table[i] length];
			characters = table[i].characters;
			length = table[i].length;
			replacement = [OFMutableString string];

			for (size_t j = 0; j < length; j++) {
				if (characters[j] > 0x10FFFF)
					@throw [OFOutOfRangeException
					    exception];

274
275
276
277
278
279
280
281

282
283
284

285
286
287
288
289
290
291
274
275
276
277
278
279
280

281
282
283

284
285
286
287
288
289
290
291







-
+


-
+







- (void)writeFiles
{
	OFURL *URL;

	[of_stdout writeString: @"Writing files…"];

	URL = [OFURL fileURLWithPath: @"../src/unicode.m"];
	[self writeTablesToFile: [URL fileSystemRepresentation]];
	[self writeTablesToFile: URL.fileSystemRepresentation];

	URL = [OFURL fileURLWithPath: @"../src/unicode.h"];
	[self writeHeaderToFile: [URL fileSystemRepresentation]];
	[self writeHeaderToFile: URL.fileSystemRepresentation];

	[of_stdout writeLine: @" done"];

	[OFApplication terminate];
}

- (void)writeTablesToFile: (OFString *)path
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
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







-
-
-
+
+
+


-
+








-
+







				break;
			}
		}

		if (!isEmpty) {
			void *pool2 = objc_autoreleasePoolPush();

			[file writeString: [OFString stringWithFormat:
			    @"static const of_unichar_t "
			    @"uppercasePage%u[0x100] = {\n", i >> 8]];
			[file writeFormat: @"static const of_unichar_t "
					   @"uppercasePage%u[0x100] = {\n",
					   i >> 8];

			for (of_unichar_t j = i; j < i + 0x100; j += 8)
				[file writeString: [OFString stringWithFormat:
				[file writeFormat:
				    @"\t%u, %u, %u, %u, %u, %u, %u, %u,\n",
				    _uppercaseTable[j],
				    _uppercaseTable[j + 1],
				    _uppercaseTable[j + 2],
				    _uppercaseTable[j + 3],
				    _uppercaseTable[j + 4],
				    _uppercaseTable[j + 5],
				    _uppercaseTable[j + 6],
				    _uppercaseTable[j + 7]]];
				    _uppercaseTable[j + 7]];

			[file writeString: @"};\n\n"];

			objc_autoreleasePoolPop(pool2);
		}
	}

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
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







-
-
-
+
+
+


-
+








-
+







				break;
			}
		}

		if (!isEmpty) {
			void *pool2 = objc_autoreleasePoolPush();

			[file writeString: [OFString stringWithFormat:
			    @"static const of_unichar_t "
			    @"lowercasePage%u[0x100] = {\n", i >> 8]];
			[file writeFormat: @"static const of_unichar_t "
					   @"lowercasePage%u[0x100] = {\n",
					   i >> 8];

			for (of_unichar_t j = i; j < i + 0x100; j += 8)
				[file writeString: [OFString stringWithFormat:
				[file writeFormat:
				    @"\t%u, %u, %u, %u, %u, %u, %u, %u,\n",
				    _lowercaseTable[j],
				    _lowercaseTable[j + 1],
				    _lowercaseTable[j + 2],
				    _lowercaseTable[j + 3],
				    _lowercaseTable[j + 4],
				    _lowercaseTable[j + 5],
				    _lowercaseTable[j + 6],
				    _lowercaseTable[j + 7]]];
				    _lowercaseTable[j + 7]];

			[file writeString: @"};\n\n"];

			objc_autoreleasePoolPop(pool2);
		}
	}

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
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







-
-
-
+
+
+


-
+








-
+







				break;
			}
		}

		if (!isEmpty) {
			void *pool2 = objc_autoreleasePoolPush();

			[file writeString: [OFString stringWithFormat:
			    @"static const of_unichar_t "
			    @"titlecasePage%u[0x100] = {\n", i >> 8]];
			[file writeFormat: @"static const of_unichar_t "
					   @"titlecasePage%u[0x100] = {\n",
					   i >> 8];

			for (of_unichar_t j = i; j < i + 0x100; j += 8)
				[file writeString: [OFString stringWithFormat:
				[file writeFormat:
				    @"\t%u, %u, %u, %u, %u, %u, %u, %u,\n",
				    _titlecaseTable[j],
				    _titlecaseTable[j + 1],
				    _titlecaseTable[j + 2],
				    _titlecaseTable[j + 3],
				    _titlecaseTable[j + 4],
				    _titlecaseTable[j + 5],
				    _titlecaseTable[j + 6],
				    _titlecaseTable[j + 7]]];
				    _titlecaseTable[j + 7]];

			[file writeString: @"};\n\n"];

			objc_autoreleasePoolPop(pool2);
		}
	}

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
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







-
-
-
+
+
+


-
+








-
+







				break;
			}
		}

		if (!isEmpty) {
			void *pool2 = objc_autoreleasePoolPush();

			[file writeString: [OFString stringWithFormat:
			    @"static const of_unichar_t "
			    @"casefoldingPage%u[0x100] = {\n", i >> 8]];
			[file writeFormat: @"static const of_unichar_t "
					   @"casefoldingPage%u[0x100] = {\n",
					   i >> 8];

			for (of_unichar_t j = i; j < i + 0x100; j += 8)
				[file writeString: [OFString stringWithFormat:
				[file writeFormat:
				    @"\t%u, %u, %u, %u, %u, %u, %u, %u,\n",
				    _casefoldingTable[j],
				    _casefoldingTable[j + 1],
				    _casefoldingTable[j + 2],
				    _casefoldingTable[j + 3],
				    _casefoldingTable[j + 4],
				    _casefoldingTable[j + 5],
				    _casefoldingTable[j + 6],
				    _casefoldingTable[j + 7]]];
				    _casefoldingTable[j + 7]];

			[file writeString: @"};\n\n"];

			objc_autoreleasePoolPop(pool2);
		}
	}

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
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







-
-
-
+
+
+









-
-
-
+
+
+







				break;
			}
		}

		if (!isEmpty) {
			void *pool2 = objc_autoreleasePoolPush();

			[file writeString: [OFString stringWithFormat:
			    @"static const char *const "
			    @"decompositionPage%u[0x100] = {\n", i >> 8]];
			[file writeFormat: @"static const char *const "
					   @"decompositionPage%u[0x100] = {\n",
					   i >> 8];

			for (of_unichar_t j = i; j < i + 0x100; j++) {
				if ((j - i) % 2 == 0)
					[file writeString: @"\t"];
				else
					[file writeString: @" "];

				if (_decompositionTable[j] != nil) {
					const char *UTF8String =
					    [_decompositionTable[j] UTF8String];
					size_t length = [_decompositionTable[j]
					    UTF8StringLength];
					    _decompositionTable[j].UTF8String;
					size_t length = _decompositionTable[j]
					    .UTF8StringLength;

					[file writeString: @"\""];

					for (size_t k = 0; k < length; k++)
						[file writeFormat:
						    @"\\x%02X",
						    (uint8_t)UTF8String[k]];
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
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







-
-
-
+
+
+









-
-
+
+

-
-
+
+







				break;
			}
		}

		if (!isEmpty) {
			void *pool2 = objc_autoreleasePoolPush();

			[file writeString: [OFString stringWithFormat:
			    @"static const char *const "
			    @"decompCompatPage%u[0x100] = {\n", i >> 8]];
			[file writeFormat: @"static const char *const "
					   @"decompCompatPage%u[0x100] = {\n",
					   i >> 8];

			for (of_unichar_t j = i; j < i + 0x100; j++) {
				if ((j - i) % 2 == 0)
					[file writeString: @"\t"];
				else
					[file writeString: @" "];

				if (_decompositionCompatTable[j] != nil) {
					const char *UTF8String =
					    [_decompositionCompatTable[j]
					    UTF8String];
					    _decompositionCompatTable[j]
					    .UTF8String;
					size_t length =
					    [_decompositionCompatTable[j]
					    UTF8StringLength];
					    _decompositionCompatTable[j]
					    .UTF8StringLength;

					[file writeString: @"\""];

					for (size_t k = 0; k < length; k++)
						[file writeFormat:
						    @"\\x%02X",
						    (uint8_t)UTF8String[k]];
587
588
589
590
591
592
593
594
595
596



597
598
599
600
601

602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618



619
620
621
622
623

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640



641
642
643
644
645

646
647
648

649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665



666
667
668
669
670

671
672
673

674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690



691
692
693
694
695

696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712




713
714
715
716
717

718
719
720

721
722
723
724
725
726
727
587
588
589
590
591
592
593



594
595
596
597
598
599


600
601
602
603
604
605
606
607
608
609
610
611
612
613
614



615
616
617
618
619
620


621
622
623
624
625
626
627
628
629
630
631
632
633
634
635



636
637
638
639
640
641


642
643


644
645
646
647
648
649
650
651
652
653
654
655
656
657
658



659
660
661
662
663
664


665
666


667
668
669
670
671
672
673
674
675
676
677
678
679
680
681



682
683
684
685
686
687


688
689
690
691
692
693
694
695
696
697
698
699
700
701
702



703
704
705
706
707
708
709


710
711


712
713
714
715
716
717
718
719







-
-
-
+
+
+



-
-
+














-
-
-
+
+
+



-
-
+














-
-
-
+
+
+



-
-
+

-
-
+














-
-
-
+
+
+



-
-
+

-
-
+














-
-
-
+
+
+



-
-
+














-
-
-
+
+
+
+



-
-
+

-
-
+







	_lowercaseTableSize++;
	_titlecaseTableSize++;
	_casefoldingTableSize++;
	_decompositionTableSize++;
	_decompositionCompatTableSize++;

	/* Write of_unicode_uppercase_table */
	[file writeString: [OFString stringWithFormat:
	    @"const of_unichar_t *const of_unicode_uppercase_table[0x%X] = "
	    @"{\n\t", _uppercaseTableSize]];
	[file writeFormat: @"const of_unichar_t *const "
			   @"of_unicode_uppercase_table[0x%X] = {\n\t",
			   _uppercaseTableSize];

	for (of_unichar_t i = 0; i < _uppercaseTableSize; i++) {
		if (_uppercaseTableUsed[i])
			[file writeString: [OFString stringWithFormat:
			    @"uppercasePage%u", i]];
			[file writeFormat: @"uppercasePage%u", i];
		else
			[file writeString: @"emptyPage"];

		if (i + 1 < _uppercaseTableSize) {
			if ((i + 1) % 4 == 0)
				[file writeString: @",\n\t"];
			else
				[file writeString: @", "];
		}
	}

	[file writeString: @"\n};\n\n"];

	/* Write of_unicode_lowercase_table */
	[file writeString: [OFString stringWithFormat:
	    @"const of_unichar_t *const of_unicode_lowercase_table[0x%X] = "
	    @"{\n\t", _lowercaseTableSize]];
	[file writeFormat: @"const of_unichar_t *const "
			   @"of_unicode_lowercase_table[0x%X] = {\n\t",
			   _lowercaseTableSize];

	for (of_unichar_t i = 0; i < _lowercaseTableSize; i++) {
		if (_lowercaseTableUsed[i])
			[file writeString: [OFString stringWithFormat:
			    @"lowercasePage%u", i]];
			[file writeFormat: @"lowercasePage%u", i];
		else
			[file writeString: @"emptyPage"];

		if (i + 1 < _lowercaseTableSize) {
			if ((i + 1) % 4 == 0)
				[file writeString: @",\n\t"];
			else
				[file writeString: @", "];
		}
	}

	[file writeString: @"\n};\n\n"];

	/* Write of_unicode_titlecase_table */
	[file writeString: [OFString stringWithFormat:
	    @"const of_unichar_t *const of_unicode_titlecase_table[0x%X] = {"
	    @"\n\t", _titlecaseTableSize]];
	[file writeFormat: @"const of_unichar_t *const "
			   @"of_unicode_titlecase_table[0x%X] = {\n\t",
			   _titlecaseTableSize];

	for (of_unichar_t i = 0; i < _titlecaseTableSize; i++) {
		if (_titlecaseTableUsed[i] == 1)
			[file writeString: [OFString stringWithFormat:
			    @"titlecasePage%u", i]];
			[file writeFormat: @"titlecasePage%u", i];
		else if (_titlecaseTableUsed[i] == 2)
			[file writeString: [OFString stringWithFormat:
			    @"uppercasePage%u", i]];
			[file writeFormat: @"uppercasePage%u", i];
		else
			[file writeString: @"emptyPage"];

		if (i + 1 < _titlecaseTableSize) {
			if ((i + 1) % 4 == 0)
				[file writeString: @",\n\t"];
			else
				[file writeString: @", "];
		}
	}

	[file writeString: @"\n};\n\n"];

	/* Write of_unicode_casefolding_table */
	[file writeString: [OFString stringWithFormat:
	    @"const of_unichar_t *const of_unicode_casefolding_table[0x%X] = "
	    @"{\n\t", _casefoldingTableSize]];
	[file writeFormat: @"const of_unichar_t *const "
			   @"of_unicode_casefolding_table[0x%X] = {\n\t",
			   _casefoldingTableSize];

	for (of_unichar_t i = 0; i < _casefoldingTableSize; i++) {
		if (_casefoldingTableUsed[i] == 1)
			[file writeString: [OFString stringWithFormat:
			    @"casefoldingPage%u", i]];
			[file writeFormat: @"casefoldingPage%u", i];
		else if (_casefoldingTableUsed[i] == 2)
			[file writeString: [OFString stringWithFormat:
			    @"lowercasePage%u", i]];
			[file writeFormat: @"lowercasePage%u", i];
		else
			[file writeString: @"emptyPage"];

		if (i + 1 < _casefoldingTableSize) {
			if ((i + 1) % 3 == 0)
				[file writeString: @",\n\t"];
			else
				[file writeString: @", "];
		}
	}

	[file writeString: @"\n};\n\n"];

	/* Write of_unicode_decomposition_table */
	[file writeString: [OFString stringWithFormat:
	    @"const char *const *of_unicode_decomposition_table[0x%X] = "
	    @"{\n\t", _decompositionTableSize]];
	[file writeFormat: @"const char *const "
			   @"*of_unicode_decomposition_table[0x%X] = {\n\t",
			   _decompositionTableSize];

	for (of_unichar_t i = 0; i < _decompositionTableSize; i++) {
		if (_decompositionTableUsed[i])
			[file writeString: [OFString stringWithFormat:
			    @"decompositionPage%u", i]];
			[file writeFormat: @"decompositionPage%u", i];
		else
			[file writeString: @"emptyDecompositionPage"];

		if (i + 1 < _decompositionTableSize) {
			if ((i + 1) % 3 == 0)
				[file writeString: @",\n\t"];
			else
				[file writeString: @", "];
		}
	}

	[file writeString: @"\n};\n\n"];

	/* Write of_unicode_decomposition_compat_table */
	[file writeString: [OFString stringWithFormat:
	    @"const char *const *of_unicode_decomposition_compat_table[0x%X] = "
	    @"{\n\t", _decompositionCompatTableSize]];
	[file writeFormat: @"const char *const "
			   @"*of_unicode_decomposition_compat_table[0x%X] = {"
			   @"\n\t",
			   _decompositionCompatTableSize];

	for (of_unichar_t i = 0; i < _decompositionCompatTableSize; i++) {
		if (_decompositionCompatTableUsed[i] == 1)
			[file writeString: [OFString stringWithFormat:
			    @"decompCompatPage%u", i]];
			[file writeFormat: @"decompCompatPage%u", i];
		else if (_decompositionCompatTableUsed[i] == 2)
			[file writeString: [OFString stringWithFormat:
			    @"decompositionPage%u", i]];
			[file writeFormat: @"decompositionPage%u", i];
		else
			[file writeString: @"emptyDecompositionPage"];

		if (i + 1 < _decompositionCompatTableSize) {
			if ((i + 1) % 3 == 0)
				[file writeString: @",\n\t"];
			else
739
740
741
742
743
744
745
746

747
748
749
750
751
752
753
754
755

756
757
758
759
760
761
762
731
732
733
734
735
736
737

738
739
740
741
742
743
744
745
746

747
748
749
750
751
752
753
754







-
+








-
+







	void *pool = objc_autoreleasePoolPush();
	OFFile *file = [OFFile fileWithPath: path
				       mode: @"w"];

	[file writeString: COPYRIGHT
	    @"#import \"OFString.h\"\n\n"];

	[file writeString: [OFString stringWithFormat:
	[file writeFormat:
	    @"#define OF_UNICODE_UPPERCASE_TABLE_SIZE 0x%X\n"
	    @"#define OF_UNICODE_LOWERCASE_TABLE_SIZE 0x%X\n"
	    @"#define OF_UNICODE_TITLECASE_TABLE_SIZE 0x%X\n"
	    @"#define OF_UNICODE_CASEFOLDING_TABLE_SIZE 0x%X\n"
	    @"#define OF_UNICODE_DECOMPOSITION_TABLE_SIZE 0x%X\n"
	    @"#define OF_UNICODE_DECOMPOSITION_COMPAT_TABLE_SIZE 0x%X\n\n",
	    _uppercaseTableSize, _lowercaseTableSize, _titlecaseTableSize,
	    _casefoldingTableSize, _decompositionTableSize,
	    _decompositionCompatTableSize]];
	    _decompositionCompatTableSize];

	[file writeString:
	    @"#ifdef __cplusplus\n"
	    @"extern \"C\" {\n"
	    @"#endif\n"
	    @"extern const of_unichar_t *const _Nonnull\n"
	    @"    of_unicode_uppercase_table["