diff options
| author | 2024-03-24 20:03:45 -0400 | |
|---|---|---|
| committer | 2024-03-24 20:03:45 -0400 | |
| commit | 789625d2ce4b6e74f2372f87c20304639fa343ef (patch) | |
| tree | 2846363d18d5739f2bc2c8a490d16a82248125b5 /src/NumericData.zig | |
| parent | Rename CaseFold and Normalize (diff) | |
| download | zg-789625d2ce4b6e74f2372f87c20304639fa343ef.tar.gz zg-789625d2ce4b6e74f2372f87c20304639fa343ef.tar.xz zg-789625d2ce4b6e74f2372f87c20304639fa343ef.zip | |
NumericData
Diffstat (limited to 'src/NumericData.zig')
| -rw-r--r-- | src/NumericData.zig | 75 |
1 files changed, 75 insertions, 0 deletions
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 @@ | |||
| 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 | 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 | |||
| 36 | pub 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. | ||
| 42 | pub 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. | ||
| 47 | pub 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. | ||
| 52 | pub 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. | ||
| 57 | pub inline fn isDecimal(self: Self, cp: u21) bool { | ||
| 58 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 4 == 4; | ||
| 59 | } | ||
| 60 | |||
| 61 | test "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 | } | ||