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
|
const std = @import("std");
const builtin = @import("builtin");
const mem = std.mem;
const block_size = 256;
const Block = [block_size]u8;
const BlockMap = std.HashMap(
Block,
u16,
struct {
pub fn hash(_: @This(), k: Block) u64 {
var hasher = std.hash.Wyhash.init(0);
std.hash.autoHashStrat(&hasher, k, .DeepRecursive);
return hasher.final();
}
pub fn eql(_: @This(), a: Block, b: Block) bool {
return mem.eql(u8, &a, &b);
}
},
std.hash_map.default_max_load_percentage,
);
pub fn main() anyerror!void {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
const allocator = arena.allocator();
var flat_map = std.AutoHashMap(u21, u8).init(allocator);
defer flat_map.deinit();
// Process PropList.txt
var in_reader = std.io.Reader.fixed(@embedFile("PropList.txt"));
while (in_reader.takeDelimiterInclusive('\n')) |took| {
const line = std.mem.trimRight(u8, took, "\n");
if (line.len == 0 or line[0] == '#') continue;
const no_comment = if (mem.indexOfScalar(u8, line, '#')) |octo| line[0..octo] else line;
var field_iter = mem.tokenizeAny(u8, no_comment, "; ");
var current_code: [2]u21 = undefined;
var i: usize = 0;
while (field_iter.next()) |field| : (i += 1) {
switch (i) {
0 => {
// Code point(s)
if (mem.indexOf(u8, field, "..")) |dots| {
current_code = .{
try std.fmt.parseInt(u21, field[0..dots], 16),
try std.fmt.parseInt(u21, field[dots + 2 ..], 16),
};
} else {
const code = try std.fmt.parseInt(u21, field, 16);
current_code = .{ code, code };
}
},
1 => {
// Core property
var bit: u8 = 0;
if (mem.eql(u8, field, "White_Space")) bit = 1;
if (mem.eql(u8, field, "Hex_Digit")) bit = 2;
if (mem.eql(u8, field, "Diacritic")) bit = 4;
if (bit != 0) {
for (current_code[0]..current_code[1] + 1) |cp| {
const gop = try flat_map.getOrPut(@intCast(cp));
if (!gop.found_existing) gop.value_ptr.* = 0;
gop.value_ptr.* |= bit;
}
}
},
else => {},
}
}
} else |err| switch (err) {
error.EndOfStream => {},
else => {
return err;
},
}
var blocks_map = BlockMap.init(allocator);
defer blocks_map.deinit();
var stage1 = std.array_list.Managed(u16).init(allocator);
defer stage1.deinit();
var stage2 = std.array_list.Managed(u8).init(allocator);
defer stage2.deinit();
var block: Block = [_]u8{0} ** block_size;
var block_len: u16 = 0;
for (0..0x110000) |i| {
const cp: u21 = @intCast(i);
const prop = flat_map.get(cp) orelse 0;
// Process block
block[block_len] = prop;
block_len += 1;
if (block_len < block_size and cp != 0x10ffff) continue;
const gop = try blocks_map.getOrPut(block);
if (!gop.found_existing) {
gop.value_ptr.* = @intCast(stage2.items.len);
try stage2.appendSlice(&block);
}
try stage1.append(gop.value_ptr.*);
block_len = 0;
}
var args_iter = try std.process.argsWithAllocator(allocator);
defer args_iter.deinit();
_ = args_iter.skip();
const output_path = args_iter.next() orelse @panic("No output file arg!");
var write_buf: [4096]u8 = undefined;
var out_file = try std.fs.cwd().createFile(output_path, .{});
defer out_file.close();
var writer = out_file.writer(&write_buf);
const endian = builtin.cpu.arch.endian();
try writer.interface.writeInt(u16, @intCast(stage1.items.len), endian);
for (stage1.items) |i| try writer.interface.writeInt(u16, i, endian);
try writer.interface.writeInt(u16, @intCast(stage2.items.len), endian);
try writer.interface.writeAll(stage2.items);
try writer.interface.flush();
}
|