summaryrefslogtreecommitdiff
path: root/src/NumericData.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-03-28 10:06:00 -0400
committerGravatar Jose Colon Rodriguez2024-03-28 10:06:00 -0400
commit74be85ac145cc6de5d03348e07be8d982c2211cb (patch)
treeb3b5f2080235e5cf73d2be080fb70583567dfb2b /src/NumericData.zig
parentScriptsData and made all Datas const (diff)
downloadzg-74be85ac145cc6de5d03348e07be8d982c2211cb.tar.gz
zg-74be85ac145cc6de5d03348e07be8d982c2211cb.tar.xz
zg-74be85ac145cc6de5d03348e07be8d982c2211cb.zip
PropsData and errdefers for init fns
Diffstat (limited to 'src/NumericData.zig')
-rw-r--r--src/NumericData.zig10
1 files changed, 2 insertions, 8 deletions
diff --git a/src/NumericData.zig b/src/NumericData.zig
index 210d623..28e8206 100644
--- a/src/NumericData.zig
+++ b/src/NumericData.zig
@@ -24,10 +24,12 @@ pub fn init(allocator: mem.Allocator) !Self {
24 24
25 const stage_1_len: u16 = try reader.readInt(u16, endian); 25 const stage_1_len: u16 = try reader.readInt(u16, endian);
26 self.s1 = try allocator.alloc(u16, stage_1_len); 26 self.s1 = try allocator.alloc(u16, stage_1_len);
27 errdefer allocator.free(self.s1);
27 for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian); 28 for (0..stage_1_len) |i| self.s1[i] = try reader.readInt(u16, endian);
28 29
29 const stage_2_len: u16 = try reader.readInt(u16, endian); 30 const stage_2_len: u16 = try reader.readInt(u16, endian);
30 self.s2 = try allocator.alloc(u8, stage_2_len); 31 self.s2 = try allocator.alloc(u8, stage_2_len);
32 errdefer allocator.free(self.s2);
31 _ = try reader.readAll(self.s2); 33 _ = try reader.readAll(self.s2);
32 34
33 return self; 35 return self;
@@ -38,11 +40,6 @@ pub fn deinit(self: *const Self) void {
38 self.allocator.free(self.s2); 40 self.allocator.free(self.s2);
39} 41}
40 42
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. 43/// True if `cp` is numeric.
47pub inline fn isNumeric(self: Self, cp: u21) bool { 44pub inline fn isNumeric(self: Self, cp: u21) bool {
48 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 1 == 1; 45 return self.s2[self.s1[cp >> 8] + (cp & 0xff)] & 1 == 1;
@@ -62,13 +59,10 @@ test "isDecimal" {
62 const self = try init(testing.allocator); 59 const self = try init(testing.allocator);
63 defer self.deinit(); 60 defer self.deinit();
64 61
65 try testing.expect(self.isNumber('\u{277f}'));
66 try testing.expect(self.isNumber('3'));
67 try testing.expect(self.isNumeric('\u{277f}')); 62 try testing.expect(self.isNumeric('\u{277f}'));
68 try testing.expect(self.isDigit('\u{2070}')); 63 try testing.expect(self.isDigit('\u{2070}'));
69 try testing.expect(self.isDecimal('3')); 64 try testing.expect(self.isDecimal('3'));
70 65
71 try testing.expect(!self.isNumber('z'));
72 try testing.expect(!self.isNumeric('1')); 66 try testing.expect(!self.isNumeric('1'));
73 try testing.expect(!self.isDigit('2')); 67 try testing.expect(!self.isDigit('2'));
74 try testing.expect(!self.isDecimal('g')); 68 try testing.expect(!self.isDecimal('g'));