diff options
Diffstat (limited to '')
| -rw-r--r-- | codegen/case_prop.zig | 136 | ||||
| -rw-r--r-- | codegen/lower.zig | 58 | ||||
| -rw-r--r-- | codegen/numeric.zig | 45 | ||||
| -rw-r--r-- | codegen/title.zig | 58 | ||||
| -rw-r--r-- | codegen/upper.zig | 58 |
5 files changed, 330 insertions, 25 deletions
diff --git a/codegen/case_prop.zig b/codegen/case_prop.zig new file mode 100644 index 0000000..ce7ee0d --- /dev/null +++ b/codegen/case_prop.zig | |||
| @@ -0,0 +1,136 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const mem = std.mem; | ||
| 4 | |||
| 5 | const block_size = 256; | ||
| 6 | const Block = [block_size]u8; | ||
| 7 | |||
| 8 | const BlockMap = std.HashMap( | ||
| 9 | Block, | ||
| 10 | u16, | ||
| 11 | struct { | ||
| 12 | pub fn hash(_: @This(), k: Block) u64 { | ||
| 13 | var hasher = std.hash.Wyhash.init(0); | ||
| 14 | std.hash.autoHashStrat(&hasher, k, .DeepRecursive); | ||
| 15 | return hasher.final(); | ||
| 16 | } | ||
| 17 | |||
| 18 | pub fn eql(_: @This(), a: Block, b: Block) bool { | ||
| 19 | return mem.eql(u8, &a, &b); | ||
| 20 | } | ||
| 21 | }, | ||
| 22 | std.hash_map.default_max_load_percentage, | ||
| 23 | ); | ||
| 24 | |||
| 25 | pub fn main() !void { | ||
| 26 | var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); | ||
| 27 | defer arena.deinit(); | ||
| 28 | const allocator = arena.allocator(); | ||
| 29 | |||
| 30 | var flat_map = std.AutoHashMap(u21, u8).init(allocator); | ||
| 31 | defer flat_map.deinit(); | ||
| 32 | |||
| 33 | var line_buf: [4096]u8 = undefined; | ||
| 34 | |||
| 35 | // Process DerivedCoreProperties.txt | ||
| 36 | var in_file = try std.fs.cwd().openFile("data/unicode/DerivedCoreProperties.txt", .{}); | ||
| 37 | defer in_file.close(); | ||
| 38 | var in_buf = std.io.bufferedReader(in_file.reader()); | ||
| 39 | const in_reader = in_buf.reader(); | ||
| 40 | |||
| 41 | while (try in_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| { | ||
| 42 | if (line.len == 0 or line[0] == '#') continue; | ||
| 43 | const no_comment = if (mem.indexOfScalar(u8, line, '#')) |octo| line[0..octo] else line; | ||
| 44 | |||
| 45 | var field_iter = mem.tokenizeAny(u8, no_comment, "; "); | ||
| 46 | var current_code: [2]u21 = undefined; | ||
| 47 | |||
| 48 | var i: usize = 0; | ||
| 49 | while (field_iter.next()) |field| : (i += 1) { | ||
| 50 | switch (i) { | ||
| 51 | 0 => { | ||
| 52 | // Code point(s) | ||
| 53 | if (mem.indexOf(u8, field, "..")) |dots| { | ||
| 54 | current_code = .{ | ||
| 55 | try std.fmt.parseInt(u21, field[0..dots], 16), | ||
| 56 | try std.fmt.parseInt(u21, field[dots + 2 ..], 16), | ||
| 57 | }; | ||
| 58 | } else { | ||
| 59 | const code = try std.fmt.parseInt(u21, field, 16); | ||
| 60 | current_code = .{ code, code }; | ||
| 61 | } | ||
| 62 | }, | ||
| 63 | 1 => { | ||
| 64 | // Props | ||
| 65 | var bit: u8 = 0; | ||
| 66 | |||
| 67 | if (mem.eql(u8, field, "Lowercase")) bit = 1; | ||
| 68 | if (mem.eql(u8, field, "Uppercase")) bit = 2; | ||
| 69 | if (mem.eql(u8, field, "Cased")) bit = 4; | ||
| 70 | |||
| 71 | if (bit != 0) { | ||
| 72 | for (current_code[0]..current_code[1] + 1) |cp| { | ||
| 73 | const gop = try flat_map.getOrPut(@intCast(cp)); | ||
| 74 | if (!gop.found_existing) gop.value_ptr.* = 0; | ||
| 75 | gop.value_ptr.* |= bit; | ||
| 76 | } | ||
| 77 | } | ||
| 78 | }, | ||
| 79 | else => {}, | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | var blocks_map = BlockMap.init(allocator); | ||
| 85 | defer blocks_map.deinit(); | ||
| 86 | |||
| 87 | var stage1 = std.ArrayList(u16).init(allocator); | ||
| 88 | defer stage1.deinit(); | ||
| 89 | |||
| 90 | var stage2 = std.ArrayList(u8).init(allocator); | ||
| 91 | defer stage2.deinit(); | ||
| 92 | |||
| 93 | var block: Block = [_]u8{0} ** block_size; | ||
| 94 | var block_len: u16 = 0; | ||
| 95 | |||
| 96 | for (0..0x110000) |i| { | ||
| 97 | const cp: u21 = @intCast(i); | ||
| 98 | const prop = flat_map.get(cp) orelse 0; | ||
| 99 | |||
| 100 | // Process block | ||
| 101 | block[block_len] = prop; | ||
| 102 | block_len += 1; | ||
| 103 | |||
| 104 | if (block_len < block_size and cp != 0x10ffff) continue; | ||
| 105 | |||
| 106 | const gop = try blocks_map.getOrPut(block); | ||
| 107 | if (!gop.found_existing) { | ||
| 108 | gop.value_ptr.* = @intCast(stage2.items.len); | ||
| 109 | try stage2.appendSlice(&block); | ||
| 110 | } | ||
| 111 | |||
| 112 | try stage1.append(gop.value_ptr.*); | ||
| 113 | block_len = 0; | ||
| 114 | } | ||
| 115 | |||
| 116 | var args_iter = try std.process.argsWithAllocator(allocator); | ||
| 117 | defer args_iter.deinit(); | ||
| 118 | _ = args_iter.skip(); | ||
| 119 | const output_path = args_iter.next() orelse @panic("No output file arg!"); | ||
| 120 | |||
| 121 | const compressor = std.compress.deflate.compressor; | ||
| 122 | var out_file = try std.fs.cwd().createFile(output_path, .{}); | ||
| 123 | defer out_file.close(); | ||
| 124 | var out_comp = try compressor(allocator, out_file.writer(), .{ .level = .best_compression }); | ||
| 125 | defer out_comp.deinit(); | ||
| 126 | const writer = out_comp.writer(); | ||
| 127 | |||
| 128 | const endian = builtin.cpu.arch.endian(); | ||
| 129 | try writer.writeInt(u16, @intCast(stage1.items.len), endian); | ||
| 130 | for (stage1.items) |i| try writer.writeInt(u16, i, endian); | ||
| 131 | |||
| 132 | try writer.writeInt(u16, @intCast(stage2.items.len), endian); | ||
| 133 | try writer.writeAll(stage2.items); | ||
| 134 | |||
| 135 | try out_comp.flush(); | ||
| 136 | } | ||
diff --git a/codegen/lower.zig b/codegen/lower.zig new file mode 100644 index 0000000..5a1f1b3 --- /dev/null +++ b/codegen/lower.zig | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | |||
| 4 | pub 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 UnicodeData.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: [2]u24 = undefined; | ||
| 35 | |||
| 36 | var i: usize = 0; | ||
| 37 | while (field_iter.next()) |field| : (i += 1) { | ||
| 38 | switch (i) { | ||
| 39 | 0 => cps[0] = try std.fmt.parseInt(u24, field, 16), | ||
| 40 | |||
| 41 | 13 => { | ||
| 42 | // Simple lowercase mapping | ||
| 43 | if (field.len == 0) continue :lines; | ||
| 44 | cps[1] = try std.fmt.parseInt(u24, field, 16); | ||
| 45 | }, | ||
| 46 | |||
| 47 | 2 => if (line[0] == '<') continue :lines, | ||
| 48 | |||
| 49 | else => {}, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | for (&cps) |cp| try writer.writeInt(u24, cp, endian); | ||
| 54 | } | ||
| 55 | |||
| 56 | try writer.writeInt(u24, 0, endian); | ||
| 57 | try out_comp.flush(); | ||
| 58 | } | ||
diff --git a/codegen/numeric.zig b/codegen/numeric.zig index 80fc7c2..ad8490c 100644 --- a/codegen/numeric.zig +++ b/codegen/numeric.zig | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const mem = std.mem; | ||
| 3 | 4 | ||
| 4 | const block_size = 256; | 5 | const block_size = 256; |
| 5 | const Block = [block_size]u8; | 6 | const Block = [block_size]u8; |
| @@ -15,7 +16,7 @@ const BlockMap = std.HashMap( | |||
| 15 | } | 16 | } |
| 16 | 17 | ||
| 17 | pub fn eql(_: @This(), a: Block, b: Block) bool { | 18 | pub fn eql(_: @This(), a: Block, b: Block) bool { |
| 18 | return std.mem.eql(u8, &a, &b); | 19 | return mem.eql(u8, &a, &b); |
| 19 | } | 20 | } |
| 20 | }, | 21 | }, |
| 21 | std.hash_map.default_max_load_percentage, | 22 | std.hash_map.default_max_load_percentage, |
| @@ -31,17 +32,17 @@ pub fn main() !void { | |||
| 31 | 32 | ||
| 32 | var line_buf: [4096]u8 = undefined; | 33 | var line_buf: [4096]u8 = undefined; |
| 33 | 34 | ||
| 34 | // Process DerivedCombiningClass.txt | 35 | // Process DerivedNumericType.txt |
| 35 | var num_file = try std.fs.cwd().openFile("data/unicode/extracted/DerivedNumericType.txt", .{}); | 36 | var in_file = try std.fs.cwd().openFile("data/unicode/extracted/DerivedNumericType.txt", .{}); |
| 36 | defer num_file.close(); | 37 | defer in_file.close(); |
| 37 | var num_buf = std.io.bufferedReader(num_file.reader()); | 38 | var in_buf = std.io.bufferedReader(in_file.reader()); |
| 38 | const num_reader = num_buf.reader(); | 39 | const in_reader = in_buf.reader(); |
| 39 | 40 | ||
| 40 | while (try num_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| { | 41 | while (try in_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| { |
| 41 | if (line.len == 0 or line[0] == '#') continue; | 42 | if (line.len == 0 or line[0] == '#') continue; |
| 42 | const no_comment = if (std.mem.indexOfScalar(u8, line, '#')) |octo| line[0..octo] else line; | 43 | const no_comment = if (mem.indexOfScalar(u8, line, '#')) |octo| line[0..octo] else line; |
| 43 | 44 | ||
| 44 | var field_iter = std.mem.tokenizeAny(u8, no_comment, "; "); | 45 | var field_iter = mem.tokenizeAny(u8, no_comment, "; "); |
| 45 | var current_code: [2]u21 = undefined; | 46 | var current_code: [2]u21 = undefined; |
| 46 | 47 | ||
| 47 | var i: usize = 0; | 48 | var i: usize = 0; |
| @@ -49,7 +50,7 @@ pub fn main() !void { | |||
| 49 | switch (i) { | 50 | switch (i) { |
| 50 | 0 => { | 51 | 0 => { |
| 51 | // Code point(s) | 52 | // Code point(s) |
| 52 | if (std.mem.indexOf(u8, field, "..")) |dots| { | 53 | if (mem.indexOf(u8, field, "..")) |dots| { |
| 53 | current_code = .{ | 54 | current_code = .{ |
| 54 | try std.fmt.parseInt(u21, field[0..dots], 16), | 55 | try std.fmt.parseInt(u21, field[0..dots], 16), |
| 55 | try std.fmt.parseInt(u21, field[dots + 2 ..], 16), | 56 | try std.fmt.parseInt(u21, field[dots + 2 ..], 16), |
| @@ -61,25 +62,19 @@ pub fn main() !void { | |||
| 61 | }, | 62 | }, |
| 62 | 1 => { | 63 | 1 => { |
| 63 | // Numeric type | 64 | // Numeric type |
| 64 | if (std.mem.eql(u8, field, "Numeric")) { | 65 | var bit: u8 = 0; |
| 65 | for (current_code[0]..current_code[1] + 1) |cp| { | 66 | |
| 66 | const gop = try flat_map.getOrPut(@intCast(cp)); | 67 | if (mem.eql(u8, field, "Numeric")) bit = 1; |
| 67 | if (!gop.found_existing) gop.value_ptr.* = 0; | 68 | if (mem.eql(u8, field, "Digit")) bit = 2; |
| 68 | gop.value_ptr.* |= 1; | 69 | if (mem.eql(u8, field, "Decimal")) bit = 4; |
| 69 | } | 70 | |
| 70 | } else if (std.mem.eql(u8, field, "Digit")) { | 71 | if (bit != 0) { |
| 71 | for (current_code[0]..current_code[1] + 1) |cp| { | ||
| 72 | const gop = try flat_map.getOrPut(@intCast(cp)); | ||
| 73 | if (!gop.found_existing) gop.value_ptr.* = 0; | ||
| 74 | gop.value_ptr.* |= 2; | ||
| 75 | } | ||
| 76 | } else if (std.mem.eql(u8, field, "Decimal")) { | ||
| 77 | for (current_code[0]..current_code[1] + 1) |cp| { | 72 | for (current_code[0]..current_code[1] + 1) |cp| { |
| 78 | const gop = try flat_map.getOrPut(@intCast(cp)); | 73 | const gop = try flat_map.getOrPut(@intCast(cp)); |
| 79 | if (!gop.found_existing) gop.value_ptr.* = 0; | 74 | if (!gop.found_existing) gop.value_ptr.* = 0; |
| 80 | gop.value_ptr.* |= 4; | 75 | gop.value_ptr.* |= bit; |
| 81 | } | 76 | } |
| 82 | } else continue; | 77 | } |
| 83 | }, | 78 | }, |
| 84 | else => {}, | 79 | else => {}, |
| 85 | } | 80 | } |
diff --git a/codegen/title.zig b/codegen/title.zig new file mode 100644 index 0000000..653b812 --- /dev/null +++ b/codegen/title.zig | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | |||
| 4 | pub 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 UnicodeData.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: [2]u24 = undefined; | ||
| 35 | |||
| 36 | var i: usize = 0; | ||
| 37 | while (field_iter.next()) |field| : (i += 1) { | ||
| 38 | switch (i) { | ||
| 39 | 0 => cps[0] = try std.fmt.parseInt(u24, field, 16), | ||
| 40 | |||
| 41 | 14 => { | ||
| 42 | // Simple titlecase mapping | ||
| 43 | if (field.len == 0) continue :lines; | ||
| 44 | cps[1] = try std.fmt.parseInt(u24, field, 16); | ||
| 45 | }, | ||
| 46 | |||
| 47 | 2 => if (line[0] == '<') continue :lines, | ||
| 48 | |||
| 49 | else => {}, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | for (&cps) |cp| try writer.writeInt(u24, cp, endian); | ||
| 54 | } | ||
| 55 | |||
| 56 | try writer.writeInt(u24, 0, endian); | ||
| 57 | try out_comp.flush(); | ||
| 58 | } | ||
diff --git a/codegen/upper.zig b/codegen/upper.zig new file mode 100644 index 0000000..6fea608 --- /dev/null +++ b/codegen/upper.zig | |||
| @@ -0,0 +1,58 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | |||
| 4 | pub 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 UnicodeData.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: [2]u24 = undefined; | ||
| 35 | |||
| 36 | var i: usize = 0; | ||
| 37 | while (field_iter.next()) |field| : (i += 1) { | ||
| 38 | switch (i) { | ||
| 39 | 0 => cps[0] = try std.fmt.parseInt(u24, field, 16), | ||
| 40 | |||
| 41 | 12 => { | ||
| 42 | // Simple uppercase mapping | ||
| 43 | if (field.len == 0) continue :lines; | ||
| 44 | cps[1] = try std.fmt.parseInt(u24, field, 16); | ||
| 45 | }, | ||
| 46 | |||
| 47 | 2 => if (line[0] == '<') continue :lines, | ||
| 48 | |||
| 49 | else => {}, | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | for (&cps) |cp| try writer.writeInt(u24, cp, endian); | ||
| 54 | } | ||
| 55 | |||
| 56 | try writer.writeInt(u24, 0, endian); | ||
| 57 | try out_comp.flush(); | ||
| 58 | } | ||