summaryrefslogtreecommitdiff
path: root/codegen/gencat.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-02-27 18:04:32 -0400
committerGravatar Jose Colon Rodriguez2024-02-27 18:04:32 -0400
commitecd9c2277de17e24fa26aefee955caa10b5b990c (patch)
treeb76f3e2b9886456f61b0001f4dcae15d00a76856 /codegen/gencat.zig
parentNormalizer 2x faster than Ziglyph; Uses 2x memory (diff)
downloadzg-ecd9c2277de17e24fa26aefee955caa10b5b990c.tar.gz
zg-ecd9c2277de17e24fa26aefee955caa10b5b990c.tar.xz
zg-ecd9c2277de17e24fa26aefee955caa10b5b990c.zip
General Category with GenCatData
Diffstat (limited to 'codegen/gencat.zig')
-rw-r--r--codegen/gencat.zig172
1 files changed, 172 insertions, 0 deletions
diff --git a/codegen/gencat.zig b/codegen/gencat.zig
new file mode 100644
index 0000000..5407040
--- /dev/null
+++ b/codegen/gencat.zig
@@ -0,0 +1,172 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4const Gc = enum {
5 Cc,
6 Cf,
7 Cn,
8 Co,
9 Cs,
10 Ll,
11 Lm,
12 Lo,
13 Lt,
14 Lu,
15 Mc,
16 Me,
17 Mn,
18 Nd,
19 Nl,
20 No,
21 Pc,
22 Pd,
23 Pe,
24 Pf,
25 Pi,
26 Po,
27 Ps,
28 Sc,
29 Sk,
30 Sm,
31 So,
32 Zl,
33 Zp,
34 Zs,
35};
36
37const block_size = 256;
38const Block = [block_size]u5;
39
40const BlockMap = std.HashMap(
41 Block,
42 u16,
43 struct {
44 pub fn hash(_: @This(), k: Block) u64 {
45 var hasher = std.hash.Wyhash.init(0);
46 std.hash.autoHashStrat(&hasher, k, .DeepRecursive);
47 return hasher.final();
48 }
49
50 pub fn eql(_: @This(), a: Block, b: Block) bool {
51 return std.mem.eql(u5, &a, &b);
52 }
53 },
54 std.hash_map.default_max_load_percentage,
55);
56
57pub fn main() !void {
58 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
59 defer arena.deinit();
60 const allocator = arena.allocator();
61
62 var flat_map = std.AutoHashMap(u21, u5).init(allocator);
63 defer flat_map.deinit();
64
65 var line_buf: [4096]u8 = undefined;
66
67 // Process DerivedEastAsianWidth.txt
68 var in_file = try std.fs.cwd().openFile("data/unicode/extracted/DerivedGeneralCategory.txt", .{});
69 defer in_file.close();
70 var in_buf = std.io.bufferedReader(in_file.reader());
71 const in_reader = in_buf.reader();
72
73 while (try in_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| {
74 if (line.len == 0 or line[0] == '#') continue;
75
76 const no_comment = if (std.mem.indexOfScalar(u8, line, '#')) |octo| line[0..octo] else line;
77
78 var field_iter = std.mem.tokenizeAny(u8, no_comment, "; ");
79 var current_code: [2]u21 = undefined;
80
81 var i: usize = 0;
82 while (field_iter.next()) |field| : (i += 1) {
83 switch (i) {
84 0 => {
85 // Code point(s)
86 if (std.mem.indexOf(u8, field, "..")) |dots| {
87 current_code = .{
88 try std.fmt.parseInt(u21, field[0..dots], 16),
89 try std.fmt.parseInt(u21, field[dots + 2 ..], 16),
90 };
91 } else {
92 const code = try std.fmt.parseInt(u21, field, 16);
93 current_code = .{ code, code };
94 }
95 },
96 1 => {
97 // General category
98 const gc = std.meta.stringToEnum(Gc, field) orelse return error.UnknownGenCat;
99 for (current_code[0]..current_code[1] + 1) |cp| try flat_map.put(@intCast(cp), @intFromEnum(gc));
100 },
101 else => {},
102 }
103 }
104 }
105
106 var blocks_map = BlockMap.init(allocator);
107 defer blocks_map.deinit();
108
109 var stage1 = std.ArrayList(u16).init(allocator);
110 defer stage1.deinit();
111
112 var stage2 = std.ArrayList(u5).init(allocator);
113 defer stage2.deinit();
114
115 var stage3 = std.ArrayList(u5).init(allocator);
116 defer stage3.deinit();
117
118 var block: Block = [_]u5{0} ** block_size;
119 var block_len: u16 = 0;
120
121 for (0..0x110000) |i| {
122 const cp: u21 = @intCast(i);
123 const gc = flat_map.get(cp).?;
124
125 const stage3_idx = blk: {
126 for (stage3.items, 0..) |gci, j| {
127 if (gc == gci) break :blk j;
128 }
129 try stage3.append(gc);
130 break :blk stage3.items.len - 1;
131 };
132
133 // Process block
134 block[block_len] = @intCast(stage3_idx);
135 block_len += 1;
136
137 if (block_len < block_size and cp != 0x10ffff) continue;
138
139 const gop = try blocks_map.getOrPut(block);
140 if (!gop.found_existing) {
141 gop.value_ptr.* = @intCast(stage2.items.len);
142 try stage2.appendSlice(&block);
143 }
144
145 try stage1.append(gop.value_ptr.*);
146 block_len = 0;
147 }
148
149 var args_iter = try std.process.argsWithAllocator(allocator);
150 defer args_iter.deinit();
151 _ = args_iter.skip();
152 const output_path = args_iter.next() orelse @panic("No output file arg!");
153
154 const compressor = std.compress.deflate.compressor;
155 var out_file = try std.fs.cwd().createFile(output_path, .{});
156 defer out_file.close();
157 var out_comp = try compressor(allocator, out_file.writer(), .{ .level = .best_compression });
158 defer out_comp.deinit();
159 const writer = out_comp.writer();
160
161 const endian = builtin.cpu.arch.endian();
162 try writer.writeInt(u16, @intCast(stage1.items.len), endian);
163 for (stage1.items) |i| try writer.writeInt(u16, i, endian);
164
165 try writer.writeInt(u16, @intCast(stage2.items.len), endian);
166 for (stage2.items) |i| try writer.writeInt(u8, i, endian);
167
168 try writer.writeInt(u8, @intCast(stage3.items.len), endian);
169 for (stage3.items) |i| try writer.writeInt(u8, i, endian);
170
171 try out_comp.flush();
172}