summaryrefslogtreecommitdiff
path: root/src/NumericData.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-03-24 20:03:45 -0400
committerGravatar Jose Colon Rodriguez2024-03-24 20:03:45 -0400
commit789625d2ce4b6e74f2372f87c20304639fa343ef (patch)
tree2846363d18d5739f2bc2c8a490d16a82248125b5 /src/NumericData.zig
parentRename CaseFold and Normalize (diff)
downloadzg-789625d2ce4b6e74f2372f87c20304639fa343ef.tar.gz
zg-789625d2ce4b6e74f2372f87c20304639fa343ef.tar.xz
zg-789625d2ce4b6e74f2372f87c20304639fa343ef.zip
NumericData
Diffstat (limited to 'src/NumericData.zig')
-rw-r--r--src/NumericData.zig75
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 @@
1const std = @import("std");
2const builtin = @import("builtin");
3const compress = std.compress;
4const mem = std.mem;
5const testing = std.testing;
6
7allocator: mem.Allocator,
8s1: []u16 = undefined,
9s2: []u8 = undefined,
10
11const Self = @This();
12
13pub 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
36pub 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.
42pub 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.
47pub 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.
52pub 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.
57pub inline fn isDecimal(self: Self, cp: u21) bool {
58 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 4 == 4;
59}
60
61test "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}