summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-03-24 20:03:45 -0400
committerGravatar Jose Colon Rodriguez2024-03-24 20:03:45 -0400
commit789625d2ce4b6e74f2372f87c20304639fa343ef (patch)
tree2846363d18d5739f2bc2c8a490d16a82248125b5
parentRename CaseFold and Normalize (diff)
downloadzg-789625d2ce4b6e74f2372f87c20304639fa343ef.tar.gz
zg-789625d2ce4b6e74f2372f87c20304639fa343ef.tar.xz
zg-789625d2ce4b6e74f2372f87c20304639fa343ef.zip
NumericData
-rw-r--r--build.zig34
-rw-r--r--codegen/numeric.zig141
-rw-r--r--src/NumericData.zig75
-rw-r--r--src/main.zig32
4 files changed, 263 insertions, 19 deletions
diff --git a/build.zig b/build.zig
index c3d9fe0..316292e 100644
--- a/build.zig
+++ b/build.zig
@@ -97,6 +97,16 @@ pub fn build(b: *std.Build) void {
97 const run_fold_gen_exe = b.addRunArtifact(fold_gen_exe); 97 const run_fold_gen_exe = b.addRunArtifact(fold_gen_exe);
98 const fold_gen_out = run_fold_gen_exe.addOutputFileArg("fold.bin.z"); 98 const fold_gen_out = run_fold_gen_exe.addOutputFileArg("fold.bin.z");
99 99
100 // Numeric types
101 const num_gen_exe = b.addExecutable(.{
102 .name = "numeric",
103 .root_source_file = .{ .path = "codegen/numeric.zig" },
104 .target = b.host,
105 .optimize = .Debug,
106 });
107 const run_num_gen_exe = b.addRunArtifact(num_gen_exe);
108 const num_gen_out = run_num_gen_exe.addOutputFileArg("numeric.bin.z");
109
100 // Modules we provide 110 // Modules we provide
101 // Code points 111 // Code points
102 const code_point = b.addModule("code_point", .{ 112 const code_point = b.addModule("code_point", .{
@@ -228,6 +238,14 @@ pub fn build(b: *std.Build) void {
228 case_fold.addImport("FoldData", fold_data); 238 case_fold.addImport("FoldData", fold_data);
229 case_fold.addImport("Normalize", norm); 239 case_fold.addImport("Normalize", norm);
230 240
241 // Numeric type
242 const num_data = b.createModule(.{
243 .root_source_file = .{ .path = "src/NumericData.zig" },
244 .target = target,
245 .optimize = optimize,
246 });
247 num_data.addAnonymousImport("numeric", .{ .root_source_file = num_gen_out });
248
231 // Benchmark rig 249 // Benchmark rig
232 const exe = b.addExecutable(.{ 250 const exe = b.addExecutable(.{
233 .name = "zg", 251 .name = "zg",
@@ -238,12 +256,13 @@ pub fn build(b: *std.Build) void {
238 }); 256 });
239 // exe.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); 257 // exe.root_module.addImport("ziglyph", ziglyph.module("ziglyph"));
240 // exe.root_module.addImport("ascii", ascii); 258 // exe.root_module.addImport("ascii", ascii);
241 // exe.root_module.addImport("code_point", code_point); 259 exe.root_module.addImport("code_point", code_point);
242 // exe.root_module.addImport("grapheme", grapheme); 260 // exe.root_module.addImport("grapheme", grapheme);
243 // exe.root_module.addImport("DisplayWidth", display_width); 261 // exe.root_module.addImport("DisplayWidth", display_width);
244 exe.root_module.addImport("Normalize", norm); 262 // exe.root_module.addImport("Normalize", norm);
245 // exe.root_module.addImport("CaseFold", case_fold); 263 // exe.root_module.addImport("CaseFold", case_fold);
246 // exe.root_module.addImport("GenCatData", gencat_data); 264 // exe.root_module.addImport("GenCatData", gencat_data);
265 exe.root_module.addImport("NumericData", num_data);
247 b.installArtifact(exe); 266 b.installArtifact(exe);
248 267
249 const run_cmd = b.addRunArtifact(exe); 268 const run_cmd = b.addRunArtifact(exe);
@@ -255,20 +274,21 @@ pub fn build(b: *std.Build) void {
255 274
256 // Tests 275 // Tests
257 const exe_unit_tests = b.addTest(.{ 276 const exe_unit_tests = b.addTest(.{
258 .root_source_file = .{ .path = "src/CaseFold.zig" }, 277 .root_source_file = .{ .path = "src/NumericData.zig" },
259 .target = target, 278 .target = target,
260 .optimize = optimize, 279 .optimize = optimize,
261 }); 280 });
262 exe_unit_tests.root_module.addImport("ascii", ascii); 281 // exe_unit_tests.root_module.addImport("ascii", ascii);
263 // exe_unit_tests.root_module.addImport("code_point", code_point); 282 // exe_unit_tests.root_module.addImport("code_point", code_point);
264 // exe_unit_tests.root_module.addImport("GraphemeData", grapheme_data); 283 // exe_unit_tests.root_module.addImport("GraphemeData", grapheme_data);
265 // exe_unit_tests.root_module.addImport("grapheme", grapheme); 284 // exe_unit_tests.root_module.addImport("grapheme", grapheme);
266 // exe_unit_tests.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); 285 // exe_unit_tests.root_module.addImport("ziglyph", ziglyph.module("ziglyph"));
267 // exe_unit_tests.root_module.addAnonymousImport("normp", .{ .root_source_file = normp_gen_out }); 286 // exe_unit_tests.root_module.addAnonymousImport("normp", .{ .root_source_file = normp_gen_out });
268 // exe_unit_tests.root_module.addImport("DisplayWidthData", dw_data); 287 // exe_unit_tests.root_module.addImport("DisplayWidthData", dw_data);
269 exe_unit_tests.root_module.addImport("NormData", norm_data); 288 // exe_unit_tests.root_module.addImport("NormData", norm_data);
270 exe_unit_tests.root_module.addImport("Normalize", norm); 289 // exe_unit_tests.root_module.addImport("Normalize", norm);
271 exe_unit_tests.root_module.addImport("FoldData", fold_data); 290 // exe_unit_tests.root_module.addImport("FoldData", fold_data);
291 exe_unit_tests.root_module.addAnonymousImport("numeric", .{ .root_source_file = num_gen_out });
272 // exe_unit_tests.filter = "nfd !ASCII"; 292 // exe_unit_tests.filter = "nfd !ASCII";
273 293
274 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); 294 const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
diff --git a/codegen/numeric.zig b/codegen/numeric.zig
new file mode 100644
index 0000000..80fc7c2
--- /dev/null
+++ b/codegen/numeric.zig
@@ -0,0 +1,141 @@
1const std = @import("std");
2const builtin = @import("builtin");
3
4const block_size = 256;
5const Block = [block_size]u8;
6
7const BlockMap = std.HashMap(
8 Block,
9 u16,
10 struct {
11 pub fn hash(_: @This(), k: Block) u64 {
12 var hasher = std.hash.Wyhash.init(0);
13 std.hash.autoHashStrat(&hasher, k, .DeepRecursive);
14 return hasher.final();
15 }
16
17 pub fn eql(_: @This(), a: Block, b: Block) bool {
18 return std.mem.eql(u8, &a, &b);
19 }
20 },
21 std.hash_map.default_max_load_percentage,
22);
23
24pub fn main() !void {
25 var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
26 defer arena.deinit();
27 const allocator = arena.allocator();
28
29 var flat_map = std.AutoHashMap(u21, u8).init(allocator);
30 defer flat_map.deinit();
31
32 var line_buf: [4096]u8 = undefined;
33
34 // Process DerivedCombiningClass.txt
35 var num_file = try std.fs.cwd().openFile("data/unicode/extracted/DerivedNumericType.txt", .{});
36 defer num_file.close();
37 var num_buf = std.io.bufferedReader(num_file.reader());
38 const num_reader = num_buf.reader();
39
40 while (try num_reader.readUntilDelimiterOrEof(&line_buf, '\n')) |line| {
41 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
44 var field_iter = std.mem.tokenizeAny(u8, no_comment, "; ");
45 var current_code: [2]u21 = undefined;
46
47 var i: usize = 0;
48 while (field_iter.next()) |field| : (i += 1) {
49 switch (i) {
50 0 => {
51 // Code point(s)
52 if (std.mem.indexOf(u8, field, "..")) |dots| {
53 current_code = .{
54 try std.fmt.parseInt(u21, field[0..dots], 16),
55 try std.fmt.parseInt(u21, field[dots + 2 ..], 16),
56 };
57 } else {
58 const code = try std.fmt.parseInt(u21, field, 16);
59 current_code = .{ code, code };
60 }
61 },
62 1 => {
63 // Numeric type
64 if (std.mem.eql(u8, field, "Numeric")) {
65 for (current_code[0]..current_code[1] + 1) |cp| {
66 const gop = try flat_map.getOrPut(@intCast(cp));
67 if (!gop.found_existing) gop.value_ptr.* = 0;
68 gop.value_ptr.* |= 1;
69 }
70 } else if (std.mem.eql(u8, field, "Digit")) {
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| {
78 const gop = try flat_map.getOrPut(@intCast(cp));
79 if (!gop.found_existing) gop.value_ptr.* = 0;
80 gop.value_ptr.* |= 4;
81 }
82 } else continue;
83 },
84 else => {},
85 }
86 }
87 }
88
89 var blocks_map = BlockMap.init(allocator);
90 defer blocks_map.deinit();
91
92 var stage1 = std.ArrayList(u16).init(allocator);
93 defer stage1.deinit();
94
95 var stage2 = std.ArrayList(u8).init(allocator);
96 defer stage2.deinit();
97
98 var block: Block = [_]u8{0} ** block_size;
99 var block_len: u16 = 0;
100
101 for (0..0x110000) |i| {
102 const cp: u21 = @intCast(i);
103 const nt = flat_map.get(cp) orelse 0;
104
105 // Process block
106 block[block_len] = nt;
107 block_len += 1;
108
109 if (block_len < block_size and cp != 0x10ffff) continue;
110
111 const gop = try blocks_map.getOrPut(block);
112 if (!gop.found_existing) {
113 gop.value_ptr.* = @intCast(stage2.items.len);
114 try stage2.appendSlice(&block);
115 }
116
117 try stage1.append(gop.value_ptr.*);
118 block_len = 0;
119 }
120
121 var args_iter = try std.process.argsWithAllocator(allocator);
122 defer args_iter.deinit();
123 _ = args_iter.skip();
124 const output_path = args_iter.next() orelse @panic("No output file arg!");
125
126 const compressor = std.compress.deflate.compressor;
127 var out_file = try std.fs.cwd().createFile(output_path, .{});
128 defer out_file.close();
129 var out_comp = try compressor(allocator, out_file.writer(), .{ .level = .best_compression });
130 defer out_comp.deinit();
131 const writer = out_comp.writer();
132
133 const endian = builtin.cpu.arch.endian();
134 try writer.writeInt(u16, @intCast(stage1.items.len), endian);
135 for (stage1.items) |i| try writer.writeInt(u16, i, endian);
136
137 try writer.writeInt(u16, @intCast(stage2.items.len), endian);
138 try writer.writeAll(stage2.items);
139
140 try out_comp.flush();
141}
diff --git a/src/NumericData.zig b/src/NumericData.zig
new file mode 100644
index 0000000..baf8f11
--- /dev/null
+++ b/src/NumericData.zig
@@ -0,0 +1,75 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const compress = std.compress;
4const mem = std.mem;
5const testing = std.testing;
6
7allocator: mem.Allocator,
8s1: []u16 = undefined,
9s2: []u8 = undefined,
10
11const Self = @This();
12
13pub fn init(allocator: mem.Allocator) !Self {
14 const decompressor = compress.deflate.decompressor;
15 const in_bytes = @embedFile("numeric");
16 var in_fbs = std.io.fixedBufferStream(in_bytes);
17 var in_decomp = try decompressor(allocator, in_fbs.reader(), null);
18 defer in_decomp.deinit();
19 var reader = in_decomp.reader();
20
21 const endian = builtin.cpu.arch.endian();
22
23 var self = Self{ .allocator = allocator };
24
25 const stage_1_len: u16 = try reader.readInt(u16, endian);
26 self.s1 = try allocator.alloc(u16, stage_1_len);
27 for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian);
28
29 const stage_2_len: u16 = try reader.readInt(u16, endian);
30 self.s2 = try allocator.alloc(u8, stage_2_len);
31 _ = try reader.readAll(self.s2);
32
33 return self;
34}
35
36pub fn deinit(self: *Self) void {
37 self.allocator.free(self.s1);
38 self.allocator.free(self.s2);
39}
40
41/// True if `cp` is any numeric type.
42pub fn isNumber(self: Self, cp: u21) bool {
43 return self.isNumeric(cp) or self.isDigit(cp) or self.isDecimal(cp);
44}
45
46/// True if `cp` is numeric.
47pub inline fn isNumeric(self: Self, cp: u21) bool {
48 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 1 == 1;
49}
50
51/// True if `cp` is a digit.
52pub inline fn isDigit(self: Self, cp: u21) bool {
53 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 2 == 2;
54}
55
56/// True if `cp` is decimal.
57pub inline fn isDecimal(self: Self, cp: u21) bool {
58 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 4 == 4;
59}
60
61test "isDecimal" {
62 var self = try init(testing.allocator);
63 defer self.deinit();
64
65 try testing.expect(self.isNumber('\u{277f}'));
66 try testing.expect(self.isNumber('3'));
67 try testing.expect(self.isNumeric('\u{277f}'));
68 try testing.expect(self.isDigit('\u{2070}'));
69 try testing.expect(self.isDecimal('3'));
70
71 try testing.expect(!self.isNumber('z'));
72 try testing.expect(!self.isNumeric('1'));
73 try testing.expect(!self.isDigit('2'));
74 try testing.expect(!self.isDecimal('g'));
75}
diff --git a/src/main.zig b/src/main.zig
index 0b0d550..52d823c 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -11,18 +11,20 @@ const std = @import("std");
11// const strWidth = @import("display_width").strWidth; 11// const strWidth = @import("display_width").strWidth;
12 12
13// const CodePointIterator = @import("ziglyph").CodePointIterator; 13// const CodePointIterator = @import("ziglyph").CodePointIterator;
14// const CodePointIterator = @import("code_point").Iterator; 14const CodePointIterator = @import("code_point").Iterator;
15 15
16// const ascii = @import("ascii"); 16// const ascii = @import("ascii");
17// const ascii = std.ascii; 17// const ascii = std.ascii;
18 18
19// const Normalize = @import("ziglyph").Normalizer; 19// const Normalize = @import("ziglyph").Normalizer;
20const Normalize = @import("Normalize"); 20// const Normalize = @import("Normalize");
21 21
22// const CaseFold = @import("CaseFold"); 22// const CaseFold = @import("CaseFold");
23 23
24// const GenCatData = @import("GenCatData"); 24// const GenCatData = @import("GenCatData");
25 25
26const NumericData = @import("NumericData");
27
26pub fn main() !void { 28pub fn main() !void {
27 var args_iter = std.process.args(); 29 var args_iter = std.process.args();
28 _ = args_iter.skip(); 30 _ = args_iter.skip();
@@ -39,9 +41,9 @@ pub fn main() !void {
39 ); 41 );
40 defer allocator.free(input); 42 defer allocator.free(input);
41 43
42 var norm_data = try Normalize.NormData.init(allocator); 44 // var norm_data = try Normalize.NormData.init(allocator);
43 defer norm_data.deinit(); 45 // defer norm_data.deinit();
44 var norm = Normalize{ .norm_data = &norm_data }; 46 // var norm = Normalize{ .norm_data = &norm_data };
45 // var norm = try Normalize.init(allocator); 47 // var norm = try Normalize.init(allocator);
46 // defer norm.deinit(); 48 // defer norm.deinit();
47 49
@@ -52,10 +54,13 @@ pub fn main() !void {
52 // defer fold_data.deinit(); 54 // defer fold_data.deinit();
53 // var caser = CaseFold{ .fold_data = &fold_data }; 55 // var caser = CaseFold{ .fold_data = &fold_data };
54 56
57 var num_data = try NumericData.init(allocator);
58 defer num_data.deinit();
59
55 // var iter = GraphemeIterator.init(input, &data); 60 // var iter = GraphemeIterator.init(input, &data);
56 // defer iter.deinit(); 61 // defer iter.deinit();
57 // var iter = CodePointIterator{ .bytes = input }; 62 var iter = CodePointIterator{ .bytes = input };
58 var iter = std.mem.splitScalar(u8, input, '\n'); 63 // var iter = std.mem.splitScalar(u8, input, '\n');
59 64
60 var result: usize = 0; 65 var result: usize = 0;
61 // var prev_line: []const u8 = ""; 66 // var prev_line: []const u8 = "";
@@ -65,11 +70,11 @@ pub fn main() !void {
65 // while (iter.next()) |cp| result += codePointWidth(@intCast(cp.code)); 70 // while (iter.next()) |cp| result += codePointWidth(@intCast(cp.code));
66 // while (iter.next()) |_| result += 1; 71 // while (iter.next()) |_| result += 1;
67 // while (iter.next()) |line| result += strWidth(line, &data); 72 // while (iter.next()) |line| result += strWidth(line, &data);
68 while (iter.next()) |line| { 73 // while (iter.next()) |line| {
69 const nfc = try norm.nfc(allocator, line); 74 // const nfc = try norm.nfc(allocator, line);
70 result += nfc.slice.len; 75 // result += nfc.slice.len;
71 // nfc.deinit(); 76 // // nfc.deinit();
72 } 77 // }
73 // while (iter.next()) |cp| { 78 // while (iter.next()) |cp| {
74 // if (cp.code == 'É') std.debug.print("`{u}` Gc: {s}\n", .{ cp.code, @tagName(gencat_data.gc(cp.code)) }); 79 // if (cp.code == 'É') std.debug.print("`{u}` Gc: {s}\n", .{ cp.code, @tagName(gencat_data.gc(cp.code)) });
75 // result += 1; 80 // result += 1;
@@ -80,6 +85,9 @@ pub fn main() !void {
80 // } 85 // }
81 // prev_line = line; 86 // prev_line = line;
82 // } 87 // }
88 while (iter.next()) |cp| {
89 if (num_data.isNumberic(cp)) result += 1;
90 }
83 91
84 std.debug.print("result: {}, took: {}\n", .{ result, timer.lap() / std.time.ns_per_ms }); 92 std.debug.print("result: {}, took: {}\n", .{ result, timer.lap() / std.time.ns_per_ms });
85} 93}