diff options
| author | 2024-03-28 16:45:31 -0400 | |
|---|---|---|
| committer | 2024-03-28 16:45:31 -0400 | |
| commit | 801b0ae894401bd98679c98a70f409b98a1bfd00 (patch) | |
| tree | 33119c74a93a85eaa4248a0f84a2c349dda0e5e5 /src/NumericData.zig | |
| parent | PropsData and errdefers for init fns (diff) | |
| download | zg-801b0ae894401bd98679c98a70f409b98a1bfd00.tar.gz zg-801b0ae894401bd98679c98a70f409b98a1bfd00.tar.xz zg-801b0ae894401bd98679c98a70f409b98a1bfd00.zip | |
Merged NumericData into PropsData
Diffstat (limited to 'src/NumericData.zig')
| -rw-r--r-- | src/NumericData.zig | 69 |
1 files changed, 0 insertions, 69 deletions
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 | } | ||