summaryrefslogtreecommitdiff
path: root/codegen
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-02-26 20:22:41 -0400
committerGravatar Jose Colon Rodriguez2024-02-26 20:22:41 -0400
commitad306724ae574b1a22abbcb6de37e65a69db82e4 (patch)
tree4b78d89006a2524b63c0ca7530e4c56a111d6eca /codegen
parentUsing NormData nfc and nfd (diff)
downloadzg-ad306724ae574b1a22abbcb6de37e65a69db82e4.tar.gz
zg-ad306724ae574b1a22abbcb6de37e65a69db82e4.tar.xz
zg-ad306724ae574b1a22abbcb6de37e65a69db82e4.zip
Using NormData nfkd
Diffstat (limited to 'codegen')
-rw-r--r--codegen/compat.zig65
1 files changed, 65 insertions, 0 deletions
diff --git a/codegen/compat.zig b/codegen/compat.zig
new file mode 100644
index 0000000..916d4d0
--- /dev/null
+++ b/codegen/compat.zig
@@ -0,0 +1,65 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4pub fn main() !void {
5 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
6 defer arena.deinit();
7 const allocator = arena.allocator();
8
9 // Process DerivedEastAsianWidth.txt
10 var in_file = try std.fs.cwd().openFile("data/unicode/UnicodeData.txt", .{});
11 defer in_file.close();
12 var in_buf = std.io.bufferedReader(in_file.reader());
13 const in_reader = in_buf.reader();
14
15 var args_iter = try std.process.argsWithAllocator(allocator);
16 defer args_iter.deinit();
17 _ = args_iter.skip();
18 const output_path = args_iter.next() orelse @panic("No output file arg!");
19
20 const compressor = std.compress.deflate.compressor;
21 var out_file = try std.fs.cwd().createFile(output_path, .{});
22 defer out_file.close();
23 var out_comp = try compressor(allocator, out_file.writer(), .{ .level = .best_compression });
24 defer out_comp.deinit();
25 const writer = out_comp.writer();
26
27 const endian = builtin.cpu.arch.endian();
28 var line_buf: [4096]u8 = undefined;
29
30 lines: while (try in_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| {
31 if (line.len == 0) continue;
32
33 var field_iter = std.mem.splitScalar(u8, line, ';');
34 var cps: [19]u24 = undefined;
35 var len: u8 = 1;
36
37 var i: usize = 0;
38 while (field_iter.next()) |field| : (i += 1) {
39 switch (i) {
40 0 => cps[0] = try std.fmt.parseInt(u24, field, 16),
41
42 5 => {
43 // Not compatibility.
44 if (field.len == 0 or field[0] != '<') continue :lines;
45 var cp_iter = std.mem.tokenizeScalar(u8, field, ' ');
46 _ = cp_iter.next(); // <compat type>
47
48 while (cp_iter.next()) |cp_str| : (len += 1) {
49 cps[len] = try std.fmt.parseInt(u24, cp_str, 16);
50 }
51 },
52
53 2 => if (line[0] == '<') continue :lines,
54
55 else => {},
56 }
57 }
58
59 try writer.writeInt(u8, @intCast(len), endian);
60 for (cps[0..len]) |cp| try writer.writeInt(u24, cp, endian);
61 }
62
63 try writer.writeInt(u16, 0, endian);
64 try out_comp.flush();
65}