Repository navigation
Expand file tree
/
Copy pathcomponents.zig
More file actions
2763 lines (2518 loc) · 100 KB
/
Copy pathcomponents.zig
File metadata and controls
2763 lines (2518 loc) · 100 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
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
575
576
577
578
579
580
581
582
583
584
585
586
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
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
const std = @import("std");
const config = @import("config.zig");
const types = @import("types.zig");
const ucd_inputs = @import("ucd_inputs.zig");
const inlineAssert = config.quirks.inlineAssert;
const setBuiltField = config.setBuiltField;
const setField = config.setField;
const setAllocField = config.setAllocField;
const initField = config.initField;
const initAllocField = config.initAllocField;
pub const build_components: []const config.Component = &.{
.{
.Impl = UnicodeData,
.fields = &.{
"name",
"general_category",
"canonical_combining_class",
"decomposition_type",
"decomposition_mapping",
"numeric_type",
"numeric_value_decimal",
"numeric_value_digit",
"numeric_value_numeric",
"is_bidi_mirrored",
"unicode_1_name",
"simple_uppercase_mapping",
"simple_lowercase_mapping",
"simple_titlecase_mapping",
},
},
.{
.Impl = CaseFolding,
.fields = &.{
"case_folding_turkish_only",
"case_folding_common_only",
"case_folding_simple_only",
"case_folding_full_only",
},
},
.{
.Impl = SpecialCasing,
.fields = &.{
"has_special_casing",
"special_lowercase_mapping",
"special_titlecase_mapping",
"special_uppercase_mapping",
"special_casing_condition",
"special_lowercase_mapping_conditional",
"special_titlecase_mapping_conditional",
"special_uppercase_mapping_conditional",
},
},
.{
.Impl = DerivedCoreProperties,
.fields = &.{
"is_math",
"is_alphabetic",
"is_lowercase",
"is_uppercase",
"is_cased",
"is_case_ignorable",
"changes_when_lowercased",
"changes_when_uppercased",
"changes_when_titlecased",
"changes_when_casefolded",
"changes_when_casemapped",
"is_id_start",
"is_id_continue",
"is_xid_start",
"is_xid_continue",
"is_default_ignorable",
"is_grapheme_extend",
"is_grapheme_base",
"is_grapheme_link",
"indic_conjunct_break",
},
},
.{ .Impl = DerivedBidiClass, .fields = &.{"bidi_class"} },
.{ .Impl = EastAsianWidth, .fields = &.{"east_asian_width"} },
.{ .Impl = GraphemeBreak, .fields = &.{"original_grapheme_break"} },
.{
.Impl = EmojiData,
.fields = &.{
"is_emoji",
"is_emoji_presentation",
"is_emoji_modifier",
"is_emoji_modifier_base",
"is_emoji_component",
"is_extended_pictographic",
},
},
.{ .Impl = EmojiVs, .fields = &.{"is_emoji_vs_base"} },
.{ .Impl = BidiPairedBracket, .fields = &.{"bidi_paired_bracket"} },
.{ .Impl = BidiMirroring, .fields = &.{"bidi_mirroring"} },
.{ .Impl = Blocks, .fields = &.{"block"} },
.{ .Impl = Scripts, .fields = &.{"script"} },
.{ .Impl = JoiningType, .fields = &.{"joining_type"} },
.{ .Impl = JoiningGroup, .fields = &.{"joining_group"} },
.{ .Impl = CompositionExclusions, .fields = &.{"is_composition_exclusion"} },
.{ .Impl = IndicPositionalCategory, .fields = &.{"indic_positional_category"} },
.{ .Impl = IndicSyllabicCategory, .fields = &.{"indic_syllabic_category"} },
.{
.Impl = CaseFoldingSimple,
.inputs = &.{
"case_folding_simple_only",
"case_folding_common_only",
"case_folding_turkish_only",
},
.fields = &.{"case_folding_simple"},
},
.{
.Impl = CaseFoldingFull,
.inputs = &.{ "case_folding_full_only", "case_folding_common_only" },
.fields = &.{"case_folding_full"},
},
.{
.Impl = LowercaseMapping,
.inputs = &.{
"has_special_casing",
"special_casing_condition",
"special_lowercase_mapping",
"simple_lowercase_mapping",
},
.fields = &.{"lowercase_mapping"},
},
.{
.Impl = TitlecaseMapping,
.inputs = &.{
"has_special_casing",
"special_casing_condition",
"special_titlecase_mapping",
"simple_titlecase_mapping",
},
.fields = &.{"titlecase_mapping"},
},
.{
.Impl = UppercaseMapping,
.inputs = &.{
"has_special_casing",
"special_casing_condition",
"special_uppercase_mapping",
"simple_uppercase_mapping",
},
.fields = &.{"uppercase_mapping"},
},
.{
.Impl = GraphemeBreakDerived,
.inputs = &.{
"original_grapheme_break",
"indic_conjunct_break",
"is_emoji_modifier",
"is_emoji_modifier_base",
"is_extended_pictographic",
"is_emoji_component",
},
.fields = &.{"grapheme_break"},
},
.{
.Impl = GraphemeBreakNoControlComponent,
.inputs = &.{"grapheme_break"},
.fields = &.{"grapheme_break_no_control"},
},
.{
.Impl = Wcwidth,
.inputs = &.{
"east_asian_width",
"general_category",
"grapheme_break",
"is_default_ignorable",
"is_emoji_modifier",
},
.fields = &.{
"wcwidth_standalone",
"wcwidth_zero_in_grapheme",
},
},
};
pub const get_components: []const config.Component = &.{};
pub fn parseCp(str: []const u8) !u21 {
return std.fmt.parseInt(u21, str, 16);
}
test "parseCp" {
try std.testing.expectEqual(0x0000, try parseCp("0000"));
try std.testing.expectEqual(0x1F600, try parseCp("1F600"));
}
pub fn parseRange(str: []const u8) !struct { start: usize, end: usize } {
if (std.mem.indexOf(u8, str, "..")) |dot_idx| {
const start = try parseCp(str[0..dot_idx]);
const end = try parseCp(str[dot_idx + 2 ..]);
return .{ .start = start, .end = end + 1 };
} else {
const cp = try parseCp(str);
return .{ .start = cp, .end = cp + 1 };
}
}
test "parseRange" {
const range = try parseRange("0030..0039");
try std.testing.expectEqual(0x0030, range.start);
try std.testing.expectEqual(0x003A, range.end);
const single = try parseRange("1F600");
try std.testing.expectEqual(0x1F600, single.start);
try std.testing.expectEqual(0x1F601, single.end);
}
fn readFile(allocator: std.mem.Allocator, io: std.Io, file_path: []const u8) ![]u8 {
try ensureUcdInputDeclared(allocator, io, file_path);
return readFileRaw(allocator, io, file_path);
}
fn readFileRaw(allocator: std.mem.Allocator, io: std.Io, file_path: []const u8) ![]u8 {
const file = try std.Io.Dir.cwd().openFile(io, file_path, .{});
defer file.close(io);
var buf: [2048]u8 = undefined;
var file_reader = file.reader(io, &buf);
return try file_reader.interface.allocRemaining(allocator, .unlimited);
}
// Every UCD file the generator reads must be marked `(used)` in
// `ucd/.gitignore`, which is how `build.zig` knows to regenerate the tables
// when it changes. See `src/ucd_inputs.zig`.
fn ensureUcdInputDeclared(allocator: std.mem.Allocator, io: std.Io, file_path: []const u8) !void {
const ucd_dir = "ucd/";
if (!std.mem.startsWith(u8, file_path, ucd_dir)) return;
const gitignore = try readFileRaw(allocator, io, ucd_dir ++ ".gitignore");
defer allocator.free(gitignore);
if (!ucd_inputs.isUsed(gitignore, file_path[ucd_dir.len..])) {
std.log.err(
"{s} is read by the generator but isn't marked `(used)` in ucd/.gitignore",
.{file_path},
);
return error.UndeclaredUcdInput;
}
}
pub fn trim(line: []const u8) []const u8 {
if (std.mem.indexOf(u8, line, "#")) |idx| {
return std.mem.trim(u8, line[0..idx], " \t\r");
}
return std.mem.trim(u8, line, " \t\r");
}
const UnicodeData = struct {
pub fn build(
comptime InputRow: type,
comptime Row: type,
allocator: std.mem.Allocator,
io: std.Io,
inputs: config.MultiSlice(InputRow),
rows: *config.MultiSlice(Row),
backing: anytype,
tracking: anytype,
) !void {
_ = inputs;
_ = backing;
var default_row: Row = comptime blk: {
var row: Row = undefined;
setBuiltField(&row, "name", .empty);
setBuiltField(&row, "general_category", .other_not_assigned);
setBuiltField(&row, "canonical_combining_class", 0);
setBuiltField(&row, "decomposition_type", .default);
setBuiltField(&row, "decomposition_mapping", .same);
setBuiltField(&row, "numeric_type", .none);
setBuiltField(&row, "numeric_value_numeric", .empty);
setBuiltField(&row, "is_bidi_mirrored", false);
setBuiltField(&row, "unicode_1_name", .empty);
setBuiltField(&row, "simple_uppercase_mapping", .same);
setBuiltField(&row, "simple_lowercase_mapping", .same);
setBuiltField(&row, "simple_titlecase_mapping", .same);
break :blk row;
};
setField(&default_row, "numeric_value_decimal", 0, null, tracking);
setField(&default_row, "numeric_value_digit", 0, null, tracking);
const file_path = "ucd/UnicodeData.txt";
// TODO: look for defaults in the Derived Extracted properties files:
// https://www.unicode.org/reports/tr44/#Derived_Extracted
//
// > For nondefault values of properties, if there is any inadvertent
// mismatch between the primary data files specifying those properties and
// these lists of extracted properties, the primary data files are taken as
// definitive. However, for default values of properties, the extracted
// data files are definitive.
const content = try readFile(allocator, io, file_path);
defer allocator.free(content);
var lines = std.mem.splitScalar(u8, content, '\n');
var range_row: ?Row = null;
while (lines.next()) |line| {
const trimmed = trim(line);
if (trimmed.len == 0) continue;
var parts = std.mem.splitScalar(u8, trimmed, ';');
const cp_str = parts.next().?;
const cp = try parseCp(cp_str);
// Fill ranges or gaps
while (rows.len < cp) {
rows.append(range_row orelse default_row);
}
if (range_row != null) {
// We're in a range, so the next entry marks the last, with the same
// information.
inlineAssert(std.mem.endsWith(u8, parts.next().?, "Last>"));
rows.append(range_row.?);
range_row = null;
continue;
}
const name_str = parts.next().?; // Field 1
const general_category_str = parts.next().?; // Field 2
const canonical_combining_class = try std.fmt.parseInt(u8, parts.next().?, 10); // Field 3
_ = parts.next().?; // Field 4: Bidi_Class (handled by DerivedBidiClass component)
const decomposition_str = parts.next().?; // Field 5: Combined type and mapping
const numeric_decimal_str = parts.next().?; // Field 6
const numeric_digit_str = parts.next().?; // Field 7
const numeric_value_numeric = parts.next().?; // Field 8
const is_bidi_mirrored = std.mem.eql(u8, parts.next().?, "Y"); // Field 9
const unicode_1_name = parts.next().?; // Field 10
_ = parts.next().?; // Field 11: Obsolete ISO_Comment
const simple_uppercase_mapping_str = parts.next().?; // Field 12
const simple_lowercase_mapping_str = parts.next().?; // Field 13
const simple_titlecase_mapping_str = parts.next().?; // Field 14
const name = if (std.mem.endsWith(u8, name_str, "First>")) name_str["<".len..(name_str.len - ", First>".len)] else name_str;
const general_category = general_category_map.get(general_category_str) orelse blk: {
std.log.err("Unknown general category: {s}", .{general_category_str});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .other_not_assigned;
}
};
const simple_uppercase_mapping = if (simple_uppercase_mapping_str.len == 0)
cp
else
try parseCp(simple_uppercase_mapping_str);
const simple_lowercase_mapping = if (simple_lowercase_mapping_str.len == 0)
cp
else
try parseCp(simple_lowercase_mapping_str);
const simple_titlecase_mapping = if (simple_titlecase_mapping_str.len == 0)
simple_uppercase_mapping
else
try parseCp(simple_titlecase_mapping_str);
// Parse decomposition type and mapping from single field
var decomposition_type: types.DecompositionType = undefined;
var decomposition_mapping: [40]u21 = undefined; // Max is currently 18
var decomposition_mapping_len: usize = undefined;
if (decomposition_str.len > 0) {
decomposition_mapping_len = 0;
// Non-empty field means canonical unless explicit type is given
decomposition_type = types.DecompositionType.canonical;
var mapping_str = decomposition_str;
if (std.mem.startsWith(u8, decomposition_str, "<")) {
// Compatibility decomposition with type in angle brackets
const end_bracket = std.mem.indexOf(u8, decomposition_str, ">") orelse {
std.log.err("Invalid decomposition format: {s}", .{decomposition_str});
unreachable;
};
const type_str = decomposition_str[1..end_bracket];
decomposition_type = std.meta.stringToEnum(types.DecompositionType, type_str) orelse blk: {
std.log.err("Unknown decomposition type: {s}", .{type_str});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .canonical;
}
};
mapping_str = std.mem.trim(u8, decomposition_str[end_bracket + 1 ..], " \t\r");
}
// Parse code points from mapping string
if (mapping_str.len > 0) {
var mapping_parts = std.mem.splitScalar(u8, mapping_str, ' ');
while (mapping_parts.next()) |part| {
if (part.len == 0) continue;
decomposition_mapping[decomposition_mapping_len] = try parseCp(part);
decomposition_mapping_len += 1;
}
}
} else {
// Default: character decomposes to itself (field 5 empty)
decomposition_type = .default;
decomposition_mapping_len = 1;
decomposition_mapping[0] = cp;
}
// Determine numeric type and parse values based on which field has a value
var numeric_type = types.NumericType.none;
var numeric_value_decimal: ?u4 = null;
var numeric_value_digit: ?u4 = null;
if (numeric_decimal_str.len > 0) {
numeric_type = types.NumericType.decimal;
numeric_value_decimal = std.fmt.parseInt(u4, numeric_decimal_str, 10) catch |err| {
std.log.err("Invalid decimal numeric value '{s}' at code point {X}: {}", .{ numeric_decimal_str, cp, err });
unreachable;
};
} else if (numeric_digit_str.len > 0) {
numeric_type = types.NumericType.digit;
numeric_value_digit = std.fmt.parseInt(u4, numeric_digit_str, 10) catch |err| {
std.log.err("Invalid digit numeric value '{s}' at code point {X}: {}", .{ numeric_digit_str, cp, err });
unreachable;
};
} else if (numeric_value_numeric.len > 0) {
numeric_type = types.NumericType.numeric;
}
var row: Row = undefined;
try setAllocField(
allocator,
&row,
"name",
cp,
name,
tracking,
);
try setAllocField(allocator, &row, "name", cp, name, tracking);
setBuiltField(&row, "general_category", general_category);
setBuiltField(&row, "canonical_combining_class", canonical_combining_class);
setBuiltField(&row, "decomposition_type", decomposition_type);
try setAllocField(
allocator,
&row,
"decomposition_mapping",
cp,
decomposition_mapping[0..decomposition_mapping_len],
tracking,
);
setBuiltField(&row, "numeric_type", numeric_type);
setField(&row, "numeric_value_decimal", cp, numeric_value_decimal, tracking);
setField(&row, "numeric_value_digit", cp, numeric_value_digit, tracking);
try setAllocField(
allocator,
&row,
"numeric_value_numeric",
cp,
numeric_value_numeric,
tracking,
);
setBuiltField(&row, "is_bidi_mirrored", is_bidi_mirrored);
try setAllocField(
allocator,
&row,
"unicode_1_name",
cp,
unicode_1_name,
tracking,
);
setField(&row, "simple_uppercase_mapping", cp, simple_uppercase_mapping, tracking);
setField(&row, "simple_lowercase_mapping", cp, simple_lowercase_mapping, tracking);
setField(&row, "simple_titlecase_mapping", cp, simple_titlecase_mapping, tracking);
// Handle range entries with "First>" and "Last>"
if (std.mem.endsWith(u8, name_str, "First>")) {
range_row = row;
}
rows.append(row);
}
// Fill any remaining gaps at the end with default values
for (rows.len..config.num_code_points) |_| {
rows.append(default_row);
}
}
};
const general_category_map = std.StaticStringMap(types.GeneralCategory).initComptime(.{
.{ "Lu", .letter_uppercase },
.{ "Ll", .letter_lowercase },
.{ "Lt", .letter_titlecase },
.{ "Lm", .letter_modifier },
.{ "Lo", .letter_other },
.{ "Mn", .mark_nonspacing },
.{ "Mc", .mark_spacing_combining },
.{ "Me", .mark_enclosing },
.{ "Nd", .number_decimal_digit },
.{ "Nl", .number_letter },
.{ "No", .number_other },
.{ "Pc", .punctuation_connector },
.{ "Pd", .punctuation_dash },
.{ "Ps", .punctuation_open },
.{ "Pe", .punctuation_close },
.{ "Pi", .punctuation_initial_quote },
.{ "Pf", .punctuation_final_quote },
.{ "Po", .punctuation_other },
.{ "Sm", .symbol_math },
.{ "Sc", .symbol_currency },
.{ "Sk", .symbol_modifier },
.{ "So", .symbol_other },
.{ "Zs", .separator_space },
.{ "Zl", .separator_line },
.{ "Zp", .separator_paragraph },
.{ "Cc", .other_control },
.{ "Cf", .other_format },
.{ "Cs", .other_surrogate },
.{ "Co", .other_private_use },
.{ "Cn", .other_not_assigned },
});
const bidi_class_map = std.StaticStringMap(types.BidiClass).initComptime(.{
.{ "L", .left_to_right },
.{ "LRE", .left_to_right_embedding },
.{ "LRO", .left_to_right_override },
.{ "R", .right_to_left },
.{ "AL", .right_to_left_arabic },
.{ "RLE", .right_to_left_embedding },
.{ "RLO", .right_to_left_override },
.{ "PDF", .pop_directional_format },
.{ "EN", .european_number },
.{ "ES", .european_number_separator },
.{ "ET", .european_number_terminator },
.{ "AN", .arabic_number },
.{ "CS", .common_number_separator },
.{ "NSM", .nonspacing_mark },
.{ "BN", .boundary_neutral },
.{ "B", .paragraph_separator },
.{ "S", .segment_separator },
.{ "WS", .whitespace },
.{ "ON", .other_neutrals },
.{ "LRI", .left_to_right_isolate },
.{ "RLI", .right_to_left_isolate },
.{ "FSI", .first_strong_isolate },
.{ "PDI", .pop_directional_isolate },
});
const CaseFolding = struct {
pub fn build(
comptime InputRow: type,
comptime Row: type,
allocator: std.mem.Allocator,
io: std.Io,
inputs: config.MultiSlice(InputRow),
rows: *config.MultiSlice(Row),
backing: anytype,
tracking: anytype,
) !void {
_ = inputs;
_ = backing;
const default_row: Row = comptime blk: {
var row: Row = undefined;
setBuiltField(&row, "case_folding_turkish_only", .null);
setBuiltField(&row, "case_folding_common_only", .null);
setBuiltField(&row, "case_folding_simple_only", .null);
setBuiltField(&row, "case_folding_full_only", .empty);
break :blk row;
};
rows.len = config.num_code_points;
rows.memset(default_row);
const file_path = "ucd/CaseFolding.txt";
const content = try readFile(allocator, io, file_path);
defer allocator.free(content);
var lines = std.mem.splitScalar(u8, content, '\n');
while (lines.next()) |line| {
const trimmed = trim(line);
if (trimmed.len == 0) continue;
var parts = std.mem.splitScalar(u8, trimmed, ';');
const cp_str = std.mem.trim(u8, parts.next().?, " \t\r");
const cp = try parseCp(cp_str);
const status_str = std.mem.trim(u8, parts.next().?, " \t\r");
const status = if (status_str.len > 0) status_str[0] else 0;
const mapping_str = std.mem.trim(u8, parts.next() orelse "", " \t\r");
var mapping_parts = std.mem.splitScalar(u8, mapping_str, ' ');
var mapping: [9]u21 = undefined;
var mapping_len: u2 = 0;
while (mapping_parts.next()) |part| {
if (part.len == 0) continue;
const mapped_cp = try parseCp(part);
mapping[mapping_len] = mapped_cp;
mapping_len += 1;
}
var row = rows.get(cp);
switch (status) {
'S' => {
inlineAssert(mapping_len == 1);
setField(&row, "case_folding_simple_only", cp, mapping[0], tracking);
},
'C' => {
inlineAssert(mapping_len == 1);
setField(&row, "case_folding_common_only", cp, mapping[0], tracking);
},
'T' => {
inlineAssert(mapping_len == 1);
setField(&row, "case_folding_turkish_only", cp, mapping[0], tracking);
},
'F' => {
inlineAssert(mapping_len > 1);
try setAllocField(
allocator,
&row,
"case_folding_full_only",
cp,
mapping[0..mapping_len],
tracking,
);
},
else => unreachable,
}
rows.set(cp, row);
}
}
};
const SpecialCasing = struct {
pub fn build(
comptime InputRow: type,
comptime Row: type,
allocator: std.mem.Allocator,
io: std.Io,
inputs: config.MultiSlice(InputRow),
rows: *config.MultiSlice(Row),
backing: anytype,
tracking: anytype,
) !void {
_ = inputs;
_ = backing;
const default_row: Row = comptime blk: {
var row: Row = undefined;
setBuiltField(&row, "has_special_casing", false);
setBuiltField(&row, "special_lowercase_mapping", .empty);
setBuiltField(&row, "special_titlecase_mapping", .empty);
setBuiltField(&row, "special_uppercase_mapping", .empty);
setBuiltField(&row, "special_casing_condition", .empty);
setBuiltField(&row, "special_lowercase_mapping_conditional", .empty);
setBuiltField(&row, "special_titlecase_mapping_conditional", .empty);
setBuiltField(&row, "special_uppercase_mapping_conditional", .empty);
break :blk row;
};
rows.len = config.num_code_points;
rows.memset(default_row);
const file_path = "ucd/SpecialCasing.txt";
const content = try readFile(allocator, io, file_path);
defer allocator.free(content);
var lines = std.mem.splitScalar(u8, content, '\n');
while (lines.next()) |line| {
const trimmed = trim(line);
if (trimmed.len == 0) continue;
var parts = std.mem.splitScalar(u8, trimmed, ';');
const cp_str = std.mem.trim(u8, parts.next().?, " \t\r");
const cp = try parseCp(cp_str);
const lower_str = std.mem.trim(u8, parts.next().?, " \t\r");
const title_str = std.mem.trim(u8, parts.next().?, " \t\r");
const upper_str = std.mem.trim(u8, parts.next().?, " \t\r");
var is_conditional = false;
var conditions: [6]types.SpecialCasingCondition = undefined;
var conditions_len: u8 = 0;
if (parts.next()) |condition_str| {
const trimmed_conditions = std.mem.trim(u8, condition_str, " \t\r");
if (trimmed_conditions.len > 0) {
is_conditional = true;
var condition_parts = std.mem.splitScalar(u8, trimmed_conditions, ' ');
while (condition_parts.next()) |condition_part| {
const trimmed_condition = std.mem.trim(u8, condition_part, " \t\r");
if (trimmed_condition.len == 0) continue;
const condition = special_casing_condition_map.get(trimmed_condition) orelse blk: {
std.log.err("Unknown special casing condition '{s}'", .{trimmed_condition});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .final_sigma;
}
};
conditions[conditions_len] = condition;
conditions_len += 1;
}
}
}
var row = rows.get(cp);
var lower_mapping: [9]u21 = undefined;
var lower_mapping_len: u8 = 0;
var lower_parts = std.mem.splitScalar(u8, lower_str, ' ');
while (lower_parts.next()) |part| {
if (part.len == 0) continue;
lower_mapping[lower_mapping_len] = try parseCp(part);
lower_mapping_len += 1;
}
var title_mapping: [9]u21 = undefined;
var title_mapping_len: u8 = 0;
var title_parts = std.mem.splitScalar(u8, title_str, ' ');
while (title_parts.next()) |part| {
if (part.len == 0) continue;
title_mapping[title_mapping_len] = try parseCp(part);
title_mapping_len += 1;
}
var upper_mapping: [9]u21 = undefined;
var upper_mapping_len: u8 = 0;
var upper_parts = std.mem.splitScalar(u8, upper_str, ' ');
while (upper_parts.next()) |part| {
if (part.len == 0) continue;
upper_mapping[upper_mapping_len] = try parseCp(part);
upper_mapping_len += 1;
}
setBuiltField(&row, "has_special_casing", true);
if (is_conditional) {
try setAllocField(allocator, &row, "special_lowercase_mapping_conditional", cp, lower_mapping[0..lower_mapping_len], tracking);
try setAllocField(allocator, &row, "special_titlecase_mapping_conditional", cp, title_mapping[0..title_mapping_len], tracking);
try setAllocField(allocator, &row, "special_uppercase_mapping_conditional", cp, upper_mapping[0..upper_mapping_len], tracking);
try setAllocField(allocator, &row, "special_casing_condition", cp, conditions[0..conditions_len], tracking);
} else {
try setAllocField(allocator, &row, "special_lowercase_mapping", cp, lower_mapping[0..lower_mapping_len], tracking);
try setAllocField(allocator, &row, "special_titlecase_mapping", cp, title_mapping[0..title_mapping_len], tracking);
try setAllocField(allocator, &row, "special_uppercase_mapping", cp, upper_mapping[0..upper_mapping_len], tracking);
}
rows.set(cp, row);
}
}
};
const special_casing_condition_map = std.StaticStringMap(types.SpecialCasingCondition).initComptime(.{
.{ "Final_Sigma", .final_sigma },
.{ "After_Soft_Dotted", .after_soft_dotted },
.{ "More_Above", .more_above },
.{ "After_I", .after_i },
.{ "Not_Before_Dot", .not_before_dot },
.{ "lt", .lt },
.{ "tr", .tr },
.{ "az", .az },
});
const DerivedCoreProperties = struct {
pub fn build(
comptime InputRow: type,
comptime Row: type,
allocator: std.mem.Allocator,
io: std.Io,
inputs: config.MultiSlice(InputRow),
rows: *config.MultiSlice(Row),
backing: anytype,
tracking: anytype,
) !void {
_ = inputs;
_ = backing;
_ = tracking;
const default_row: Row = comptime blk: {
var row: Row = undefined;
setBuiltField(&row, "is_math", false);
setBuiltField(&row, "is_alphabetic", false);
setBuiltField(&row, "is_lowercase", false);
setBuiltField(&row, "is_uppercase", false);
setBuiltField(&row, "is_cased", false);
setBuiltField(&row, "is_case_ignorable", false);
setBuiltField(&row, "changes_when_lowercased", false);
setBuiltField(&row, "changes_when_uppercased", false);
setBuiltField(&row, "changes_when_titlecased", false);
setBuiltField(&row, "changes_when_casefolded", false);
setBuiltField(&row, "changes_when_casemapped", false);
setBuiltField(&row, "is_id_start", false);
setBuiltField(&row, "is_id_continue", false);
setBuiltField(&row, "is_xid_start", false);
setBuiltField(&row, "is_xid_continue", false);
setBuiltField(&row, "is_default_ignorable", false);
setBuiltField(&row, "is_grapheme_extend", false);
setBuiltField(&row, "is_grapheme_base", false);
setBuiltField(&row, "is_grapheme_link", false);
setBuiltField(&row, "indic_conjunct_break", .none);
break :blk row;
};
rows.len = config.num_code_points;
rows.memset(default_row);
const file_path = "ucd/DerivedCoreProperties.txt";
const content = try readFile(allocator, io, file_path);
defer allocator.free(content);
var lines = std.mem.splitScalar(u8, content, '\n');
while (lines.next()) |line| {
const trimmed = trim(line);
if (trimmed.len == 0) continue;
var parts = std.mem.splitScalar(u8, trimmed, ';');
const cp_str = std.mem.trim(u8, parts.next().?, " \t\r");
const property_str = std.mem.trim(u8, parts.next().?, " \t\r");
const value_str = if (parts.next()) |v| std.mem.trim(u8, v, " \t\r") else "";
const range = try parseRange(cp_str);
const property = derived_core_property_map.get(property_str) orelse blk: {
std.log.err("Unknown DerivedCoreProperties property: {s}", .{property_str});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .is_alphabetic;
}
};
const indic_conjunct_break = indic_conjunct_break_map.get(value_str);
for (range.start..range.end) |cp| {
var row = rows.get(cp);
switch (property) {
.indic_conjunct_break => {
setBuiltField(&row, "indic_conjunct_break", indic_conjunct_break orelse blk: {
std.log.err("Unknown InCB value: {s}", .{value_str});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .linker;
}
});
},
inline else => |p| {
setBuiltField(&row, @tagName(p), true);
},
}
rows.set(cp, row);
}
}
}
};
const derived_core_property_map = std.StaticStringMap(enum {
is_math,
is_alphabetic,
is_lowercase,
is_uppercase,
is_cased,
is_case_ignorable,
changes_when_lowercased,
changes_when_uppercased,
changes_when_titlecased,
changes_when_casefolded,
changes_when_casemapped,
is_id_start,
is_id_continue,
is_xid_start,
is_xid_continue,
is_default_ignorable,
is_grapheme_extend,
is_grapheme_base,
is_grapheme_link,
indic_conjunct_break,
}).initComptime(.{
.{ "Math", .is_math },
.{ "Alphabetic", .is_alphabetic },
.{ "Lowercase", .is_lowercase },
.{ "Uppercase", .is_uppercase },
.{ "Cased", .is_cased },
.{ "Case_Ignorable", .is_case_ignorable },
.{ "Changes_When_Lowercased", .changes_when_lowercased },
.{ "Changes_When_Uppercased", .changes_when_uppercased },
.{ "Changes_When_Titlecased", .changes_when_titlecased },
.{ "Changes_When_Casefolded", .changes_when_casefolded },
.{ "Changes_When_Casemapped", .changes_when_casemapped },
.{ "ID_Start", .is_id_start },
.{ "ID_Continue", .is_id_continue },
.{ "XID_Start", .is_xid_start },
.{ "XID_Continue", .is_xid_continue },
.{ "Default_Ignorable_Code_Point", .is_default_ignorable },
.{ "Grapheme_Extend", .is_grapheme_extend },
.{ "Grapheme_Base", .is_grapheme_base },
.{ "Grapheme_Link", .is_grapheme_link },
.{ "InCB", .indic_conjunct_break },
});
const indic_conjunct_break_map = std.StaticStringMap(types.IndicConjunctBreak).initComptime(.{
.{ "Linker", .linker },
.{ "Consonant", .consonant },
.{ "Extend", .extend },
});
const DerivedBidiClass = struct {
pub fn build(
comptime InputRow: type,
comptime Row: type,
allocator: std.mem.Allocator,
io: std.Io,
inputs: config.MultiSlice(InputRow),
rows: *config.MultiSlice(Row),
backing: anytype,
tracking: anytype,
) !void {
_ = inputs;
_ = backing;
_ = tracking;
rows.len = config.num_code_points;
const items = rows.items(.bidi_class);
@memset(items, .left_to_right);
const file_path = "ucd/extracted/DerivedBidiClass.txt";
const content = try readFile(allocator, io, file_path);
defer allocator.free(content);
var lines = std.mem.splitScalar(u8, content, '\n');
while (lines.next()) |line| {
const trimmed = std.mem.trim(u8, line, " \t\r");
if (trimmed.len == 0) continue;
if (std.mem.startsWith(u8, trimmed, "# @missing:")) {
const missing_line = trimmed["# @missing:".len..];
var parts = std.mem.splitScalar(u8, missing_line, ';');
const cp_str = std.mem.trim(u8, parts.next().?, " \t\r");
const class_str = std.mem.trim(u8, parts.next().?, " \t\r");
const range = try parseRange(cp_str);
if (std.mem.eql(u8, class_str, "Left_To_Right")) {
continue;
}
const bidi_class = bidi_longform_map.get(class_str) orelse blk: {
std.log.err("Unknown @missing BidiClass value: {s}", .{class_str});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .left_to_right;
}
};
for (range.start..range.end) |cp| {
items[cp] = bidi_class;
}
continue;
}
const data_line = trim(trimmed);
if (data_line.len == 0) continue;
var parts = std.mem.splitScalar(u8, data_line, ';');
const cp_str = std.mem.trim(u8, parts.next().?, " \t\r");
const class_str = std.mem.trim(u8, parts.next().?, " \t\r");
const range = try parseRange(cp_str);
const bidi_class = bidi_class_map.get(class_str) orelse blk: {
std.log.err("Unknown BidiClass value: {s}", .{class_str});
if (!config.is_updating_ucd) {
unreachable;
} else {
break :blk .left_to_right;
}
};
for (range.start..range.end) |cp| {
items[cp] = bidi_class;
}
}
}
};
const bidi_longform_map = std.StaticStringMap(types.BidiClass).initComptime(.{
.{ "Left_To_Right", .left_to_right },
.{ "Right_To_Left", .right_to_left },
.{ "Arabic_Letter", .right_to_left_arabic },
.{ "European_Terminator", .european_number_terminator },
});
const EastAsianWidth = struct {
pub fn build(
comptime InputRow: type,
comptime Row: type,
allocator: std.mem.Allocator,
io: std.Io,