diff options
| author | 2024-03-28 16:45:31 -0400 | |
|---|---|---|
| committer | 2024-03-28 16:45:31 -0400 | |
| commit | 801b0ae894401bd98679c98a70f409b98a1bfd00 (patch) | |
| tree | 33119c74a93a85eaa4248a0f84a2c349dda0e5e5 | |
| parent | PropsData and errdefers for init fns (diff) | |
| download | zg-801b0ae894401bd98679c98a70f409b98a1bfd00.tar.gz zg-801b0ae894401bd98679c98a70f409b98a1bfd00.tar.xz zg-801b0ae894401bd98679c98a70f409b98a1bfd00.zip | |
Merged NumericData into PropsData
| -rw-r--r-- | build.zig | 10 | ||||
| -rw-r--r-- | src/NumericData.zig | 69 | ||||
| -rw-r--r-- | src/PropsData.zig | 44 |
3 files changed, 46 insertions, 77 deletions
| @@ -295,14 +295,6 @@ pub fn build(b: *std.Build) void { | |||
| 295 | case_fold.addImport("FoldData", fold_data); | 295 | case_fold.addImport("FoldData", fold_data); |
| 296 | case_fold.addImport("Normalize", norm); | 296 | case_fold.addImport("Normalize", norm); |
| 297 | 297 | ||
| 298 | // Numeric type | ||
| 299 | const num_data = b.createModule(.{ | ||
| 300 | .root_source_file = .{ .path = "src/NumericData.zig" }, | ||
| 301 | .target = target, | ||
| 302 | .optimize = optimize, | ||
| 303 | }); | ||
| 304 | num_data.addAnonymousImport("numeric", .{ .root_source_file = num_gen_out }); | ||
| 305 | |||
| 306 | // Letter case | 298 | // Letter case |
| 307 | const case_data = b.addModule("CaseData", .{ | 299 | const case_data = b.addModule("CaseData", .{ |
| 308 | .root_source_file = .{ .path = "src/CaseData.zig" }, | 300 | .root_source_file = .{ .path = "src/CaseData.zig" }, |
| @@ -330,6 +322,7 @@ pub fn build(b: *std.Build) void { | |||
| 330 | }); | 322 | }); |
| 331 | props_data.addAnonymousImport("core_props", .{ .root_source_file = core_gen_out }); | 323 | props_data.addAnonymousImport("core_props", .{ .root_source_file = core_gen_out }); |
| 332 | props_data.addAnonymousImport("props", .{ .root_source_file = props_gen_out }); | 324 | props_data.addAnonymousImport("props", .{ .root_source_file = props_gen_out }); |
| 325 | props_data.addAnonymousImport("numeric", .{ .root_source_file = num_gen_out }); | ||
| 333 | 326 | ||
| 334 | // Tests | 327 | // Tests |
| 335 | const exe_unit_tests = b.addTest(.{ | 328 | const exe_unit_tests = b.addTest(.{ |
| @@ -354,6 +347,7 @@ pub fn build(b: *std.Build) void { | |||
| 354 | // exe_unit_tests.root_module.addAnonymousImport("scripts", .{ .root_source_file = scripts_gen_out }); | 347 | // exe_unit_tests.root_module.addAnonymousImport("scripts", .{ .root_source_file = scripts_gen_out }); |
| 355 | exe_unit_tests.root_module.addAnonymousImport("core_props", .{ .root_source_file = core_gen_out }); | 348 | exe_unit_tests.root_module.addAnonymousImport("core_props", .{ .root_source_file = core_gen_out }); |
| 356 | exe_unit_tests.root_module.addAnonymousImport("props", .{ .root_source_file = props_gen_out }); | 349 | exe_unit_tests.root_module.addAnonymousImport("props", .{ .root_source_file = props_gen_out }); |
| 350 | exe_unit_tests.root_module.addAnonymousImport("numeric", .{ .root_source_file = num_gen_out }); | ||
| 357 | // exe_unit_tests.filter = "nfd !ASCII"; | 351 | // exe_unit_tests.filter = "nfd !ASCII"; |
| 358 | 352 | ||
| 359 | const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); | 353 | const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); |
diff --git a/src/NumericData.zig b/src/NumericData.zig deleted file mode 100644 index 28e8206..0000000 --- a/src/NumericData.zig +++ /dev/null | |||
| @@ -1,69 +0,0 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | const testing = std.testing; | ||
| 6 | |||
| 7 | allocator: mem.Allocator, | ||
| 8 | s1: []u16 = undefined, | ||
| 9 | s2: []u8 = undefined, | ||
| 10 | |||
| 11 | const Self = @This(); | ||
| 12 | |||
| 13 | pub 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 | errdefer allocator.free(self.s1); | ||
| 28 | for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); | ||
| 29 | |||
| 30 | const stage_2_len: u16 = try reader.readInt(u16, endian); | ||
| 31 | self.s2 = try allocator.alloc(u8, stage_2_len); | ||
| 32 | errdefer allocator.free(self.s2); | ||
| 33 | _ = try reader.readAll(self.s2); | ||
| 34 | |||
| 35 | return self; | ||
| 36 | } | ||
| 37 | |||
| 38 | pub fn deinit(self: *const Self) void { | ||
| 39 | self.allocator.free(self.s1); | ||
| 40 | self.allocator.free(self.s2); | ||
| 41 | } | ||
| 42 | |||
| 43 | /// True if `cp` is numeric. | ||
| 44 | pub inline fn isNumeric(self: Self, cp: u21) bool { | ||
| 45 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 1 == 1; | ||
| 46 | } | ||
| 47 | |||
| 48 | /// True if `cp` is a digit. | ||
| 49 | pub inline fn isDigit(self: Self, cp: u21) bool { | ||
| 50 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 2 == 2; | ||
| 51 | } | ||
| 52 | |||
| 53 | /// True if `cp` is decimal. | ||
| 54 | pub inline fn isDecimal(self: Self, cp: u21) bool { | ||
| 55 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 4 == 4; | ||
| 56 | } | ||
| 57 | |||
| 58 | test "isDecimal" { | ||
| 59 | const self = try init(testing.allocator); | ||
| 60 | defer self.deinit(); | ||
| 61 | |||
| 62 | try testing.expect(self.isNumeric('\u{277f}')); | ||
| 63 | try testing.expect(self.isDigit('\u{2070}')); | ||
| 64 | try testing.expect(self.isDecimal('3')); | ||
| 65 | |||
| 66 | try testing.expect(!self.isNumeric('1')); | ||
| 67 | try testing.expect(!self.isDigit('2')); | ||
| 68 | try testing.expect(!self.isDecimal('g')); | ||
| 69 | } | ||
diff --git a/src/PropsData.zig b/src/PropsData.zig index 252462e..9d24e68 100644 --- a/src/PropsData.zig +++ b/src/PropsData.zig | |||
| @@ -9,6 +9,8 @@ core_s1: []u16 = undefined, | |||
| 9 | core_s2: []u8 = undefined, | 9 | core_s2: []u8 = undefined, |
| 10 | props_s1: []u16 = undefined, | 10 | props_s1: []u16 = undefined, |
| 11 | props_s2: []u8 = undefined, | 11 | props_s2: []u8 = undefined, |
| 12 | num_s1: []u16 = undefined, | ||
| 13 | num_s2: []u8 = undefined, | ||
| 12 | 14 | ||
| 13 | const Self = @This(); | 15 | const Self = @This(); |
| 14 | 16 | ||
| @@ -52,6 +54,23 @@ pub fn init(allocator: mem.Allocator) !Self { | |||
| 52 | errdefer allocator.free(self.props_s2); | 54 | errdefer allocator.free(self.props_s2); |
| 53 | _ = try props_reader.readAll(self.props_s2); | 55 | _ = try props_reader.readAll(self.props_s2); |
| 54 | 56 | ||
| 57 | // Process DerivedNumericType.txt | ||
| 58 | const num_bytes = @embedFile("numeric"); | ||
| 59 | var num_fbs = std.io.fixedBufferStream(num_bytes); | ||
| 60 | var num_decomp = try decompressor(allocator, num_fbs.reader(), null); | ||
| 61 | defer num_decomp.deinit(); | ||
| 62 | var num_reader = num_decomp.reader(); | ||
| 63 | |||
| 64 | const num_stage_1_len: u16 = try num_reader.readInt(u16, endian); | ||
| 65 | self.num_s1 = try allocator.alloc(u16, num_stage_1_len); | ||
| 66 | errdefer allocator.free(self.num_s1); | ||
| 67 | for (0..num_stage_1_len) |i| self.num_s1[i] = try num_reader.readInt(u16, endian); | ||
| 68 | |||
| 69 | const num_stage_2_len: u16 = try num_reader.readInt(u16, endian); | ||
| 70 | self.num_s2 = try allocator.alloc(u8, num_stage_2_len); | ||
| 71 | errdefer allocator.free(self.num_s2); | ||
| 72 | _ = try num_reader.readAll(self.num_s2); | ||
| 73 | |||
| 55 | return self; | 74 | return self; |
| 56 | } | 75 | } |
| 57 | 76 | ||
| @@ -60,6 +79,8 @@ pub fn deinit(self: *const Self) void { | |||
| 60 | self.allocator.free(self.core_s2); | 79 | self.allocator.free(self.core_s2); |
| 61 | self.allocator.free(self.props_s1); | 80 | self.allocator.free(self.props_s1); |
| 62 | self.allocator.free(self.props_s2); | 81 | self.allocator.free(self.props_s2); |
| 82 | self.allocator.free(self.num_s1); | ||
| 83 | self.allocator.free(self.num_s2); | ||
| 63 | } | 84 | } |
| 64 | 85 | ||
| 65 | /// True if `cp` is a mathematical symbol. | 86 | /// True if `cp` is a mathematical symbol. |
| @@ -107,6 +128,21 @@ pub inline fn isDiacritic(self: Self, cp: u21) bool { | |||
| 107 | return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 4 == 4; | 128 | return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 4 == 4; |
| 108 | } | 129 | } |
| 109 | 130 | ||
| 131 | /// True if `cp` is numeric. | ||
| 132 | pub inline fn isNumeric(self: Self, cp: u21) bool { | ||
| 133 | return self.num_s2[self.num_s1[cp >> 8] + (cp & 0xff)] & 1 == 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | /// True if `cp` is a digit. | ||
| 137 | pub inline fn isDigit(self: Self, cp: u21) bool { | ||
| 138 | return self.num_s2[self.num_s1[cp >> 8] + (cp & 0xff)] & 2 == 2; | ||
| 139 | } | ||
| 140 | |||
| 141 | /// True if `cp` is decimal. | ||
| 142 | pub inline fn isDecimal(self: Self, cp: u21) bool { | ||
| 143 | return self.num_s2[self.num_s1[cp >> 8] + (cp & 0xff)] & 4 == 4; | ||
| 144 | } | ||
| 145 | |||
| 110 | test "Props" { | 146 | test "Props" { |
| 111 | const self = try init(testing.allocator); | 147 | const self = try init(testing.allocator); |
| 112 | defer self.deinit(); | 148 | defer self.deinit(); |
| @@ -120,4 +156,12 @@ test "Props" { | |||
| 120 | try testing.expect(self.isAlphabetic('A')); | 156 | try testing.expect(self.isAlphabetic('A')); |
| 121 | try testing.expect(!self.isAlphabetic('3')); | 157 | try testing.expect(!self.isAlphabetic('3')); |
| 122 | try testing.expect(self.isMath('+')); | 158 | try testing.expect(self.isMath('+')); |
| 159 | |||
| 160 | try testing.expect(self.isNumeric('\u{277f}')); | ||
| 161 | try testing.expect(self.isDigit('\u{2070}')); | ||
| 162 | try testing.expect(self.isDecimal('3')); | ||
| 163 | |||
| 164 | try testing.expect(!self.isNumeric('1')); | ||
| 165 | try testing.expect(!self.isDigit('2')); | ||
| 166 | try testing.expect(!self.isDecimal('g')); | ||
| 123 | } | 167 | } |