summaryrefslogtreecommitdiff
path: root/src/NumericData.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-03-28 16:45:31 -0400
committerGravatar Jose Colon Rodriguez2024-03-28 16:45:31 -0400
commit801b0ae894401bd98679c98a70f409b98a1bfd00 (patch)
tree33119c74a93a85eaa4248a0f84a2c349dda0e5e5 /src/NumericData.zig
parentPropsData and errdefers for init fns (diff)
downloadzg-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.zig69
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 @@
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 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
38pub 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.
44pub 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.
49pub 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.
54pub inline fn isDecimal(self: Self, cp: u21) bool {
55 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 4 == 4;
56}
57
58test "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}