From 789625d2ce4b6e74f2372f87c20304639fa343ef Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Sun, 24 Mar 2024 20:03:45 -0400 Subject: NumericData --- src/NumericData.zig | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/NumericData.zig (limited to 'src/NumericData.zig') 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 @@ +const std = @import("std"); +const builtin = @import("builtin"); +const compress = std.compress; +const mem = std.mem; +const testing = std.testing; + +allocator: mem.Allocator, +s1: []u16 = undefined, +s2: []u8 = undefined, + +const Self = @This(); + +pub fn init(allocator: mem.Allocator) !Self { + const decompressor = compress.deflate.decompressor; + const in_bytes = @embedFile("numeric"); + var in_fbs = std.io.fixedBufferStream(in_bytes); + var in_decomp = try decompressor(allocator, in_fbs.reader(), null); + defer in_decomp.deinit(); + var reader = in_decomp.reader(); + + const endian = builtin.cpu.arch.endian(); + + var self = Self{ .allocator = allocator }; + + const stage_1_len: u16 = try reader.readInt(u16, endian); + self.s1 = try allocator.alloc(u16, stage_1_len); + for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); + + const stage_2_len: u16 = try reader.readInt(u16, endian); + self.s2 = try allocator.alloc(u8, stage_2_len); + _ = try reader.readAll(self.s2); + + return self; +} + +pub fn deinit(self: *Self) void { + self.allocator.free(self.s1); + self.allocator.free(self.s2); +} + +/// True if `cp` is any numeric type. +pub fn isNumber(self: Self, cp: u21) bool { + return self.isNumeric(cp) or self.isDigit(cp) or self.isDecimal(cp); +} + +/// True if `cp` is numeric. +pub inline fn isNumeric(self: Self, cp: u21) bool { + return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 1 == 1; +} + +/// True if `cp` is a digit. +pub inline fn isDigit(self: Self, cp: u21) bool { + return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 2 == 2; +} + +/// True if `cp` is decimal. +pub inline fn isDecimal(self: Self, cp: u21) bool { + return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 4 == 4; +} + +test "isDecimal" { + var self = try init(testing.allocator); + defer self.deinit(); + + try testing.expect(self.isNumber('\u{277f}')); + try testing.expect(self.isNumber('3')); + try testing.expect(self.isNumeric('\u{277f}')); + try testing.expect(self.isDigit('\u{2070}')); + try testing.expect(self.isDecimal('3')); + + try testing.expect(!self.isNumber('z')); + try testing.expect(!self.isNumeric('1')); + try testing.expect(!self.isDigit('2')); + try testing.expect(!self.isDecimal('g')); +} -- cgit v1.2.3