From 005f2a30036ca5328ae0fffcd61749f2de2d0a7c Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Tue, 13 Feb 2024 14:23:34 -0400 Subject: Using emoji table --- build.zig | 26 +++++++++++----- src/Grapheme.zig | 3 +- src/emoji_gen.zig | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 src/emoji_gen.zig diff --git a/build.zig b/build.zig index 3a856b3..abe666d 100644 --- a/build.zig +++ b/build.zig @@ -6,15 +6,25 @@ pub fn build(b: *std.Build) void { const ziglyph = b.dependency("ziglyph", .{}); - const gen_exe = b.addExecutable(.{ - .name = "gen", + const gbp_gen_exe = b.addExecutable(.{ + .name = "gbp_gen", .root_source_file = .{ .path = "src/gbp_gen.zig" }, .target = target, .optimize = optimize, }); - gen_exe.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); - const run_gen_exe = b.addRunArtifact(gen_exe); - const gen_out = run_gen_exe.addOutputFileArg("gbp.zig"); + gbp_gen_exe.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); + const run_gbp_gen_exe = b.addRunArtifact(gbp_gen_exe); + const gbp_gen_out = run_gbp_gen_exe.addOutputFileArg("gbp.zig"); + + const emoji_gen_exe = b.addExecutable(.{ + .name = "emoji_gen", + .root_source_file = .{ .path = "src/emoji_gen.zig" }, + .target = target, + .optimize = optimize, + }); + emoji_gen_exe.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); + const run_emoji_gen_exe = b.addRunArtifact(emoji_gen_exe); + const emoji_gen_out = run_emoji_gen_exe.addOutputFileArg("emoji.zig"); const exe = b.addExecutable(.{ .name = "zgbench", @@ -23,7 +33,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); exe.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); - exe.root_module.addAnonymousImport("gbp", .{ .root_source_file = gen_out }); + exe.root_module.addAnonymousImport("gbp", .{ .root_source_file = gbp_gen_out }); + exe.root_module.addAnonymousImport("emoji", .{ .root_source_file = emoji_gen_out }); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); @@ -39,7 +50,8 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); exe_unit_tests.root_module.addImport("ziglyph", ziglyph.module("ziglyph")); - exe_unit_tests.root_module.addAnonymousImport("gbp", .{ .root_source_file = gen_out }); + exe_unit_tests.root_module.addAnonymousImport("gbp", .{ .root_source_file = gbp_gen_out }); + exe_unit_tests.root_module.addAnonymousImport("emoji", .{ .root_source_file = emoji_gen_out }); const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); diff --git a/src/Grapheme.zig b/src/Grapheme.zig index 1e9606f..1165a0e 100644 --- a/src/Grapheme.zig +++ b/src/Grapheme.zig @@ -7,9 +7,10 @@ const ziglyph = @import("ziglyph"); const CodePoint = ziglyph.CodePoint; const CodePointIterator = CodePoint.CodePointIterator; const readCodePoint = CodePoint.readCodePoint; -const emoji = ziglyph.emoji; +// const emoji = ziglyph.emoji; // const gbp = ziglyph.grapheme_break; const gbp = @import("gbp"); +const emoji = @import("emoji"); pub const Grapheme = @This(); diff --git a/src/emoji_gen.zig b/src/emoji_gen.zig new file mode 100644 index 0000000..acad0ca --- /dev/null +++ b/src/emoji_gen.zig @@ -0,0 +1,93 @@ +const std = @import("std"); + +const emoji = @import("ziglyph").emoji; + +const block_size = 256; +const Block = [block_size]bool; + +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 std.mem.eql(bool, &a, &b); + } + }, + std.hash_map.default_max_load_percentage, +); + +pub fn main() !void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + + var blocks_map = BlockMap.init(allocator); + defer blocks_map.deinit(); + + var stage1 = std.ArrayList(u16).init(allocator); + defer stage1.deinit(); + + var stage2 = std.ArrayList(bool).init(allocator); + defer stage2.deinit(); + + var block: Block = [_]bool{false} ** block_size; + var block_len: u16 = 0; + + for (0..0x10ffff + 1) |cp| { + const isEmoji = emoji.isExtendedPictographic(@intCast(cp)); + + block[block_len] = isEmoji; + 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 = std.process.args(); + _ = args_iter.skip(); + const output_path = args_iter.next() orelse @panic("No output file arg!"); + + var out_file = try std.fs.cwd().createFile(output_path, .{}); + defer out_file.close(); + var out_buf = std.io.bufferedWriter(out_file.writer()); + const writer = out_buf.writer(); + + try writer.print("const stage_1 = [{}]u16{{", .{stage1.items.len}); + for (stage1.items) |v| { + _ = try writer.print("{},", .{v}); + } + try writer.writeAll("};\n"); + + try writer.print("const stage_2 = [{}]bool{{", .{stage2.items.len}); + for (stage2.items) |v| { + _ = try writer.print("{},", .{v}); + } + try writer.writeAll("};\n"); + + const code = + \\pub inline fn isExtendedPictographic(cp: u21) bool { + \\ const stage_1_index = cp >> 8; + \\ const stage_2_index = stage_1[stage_1_index] + (cp & 0xff); + \\ return stage_2[stage_2_index]; + \\} + \\ + ; + + try writer.writeAll(code); + + try out_buf.flush(); +} -- cgit v1.2.3