summaryrefslogtreecommitdiff
path: root/src/PropsData.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/PropsData.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/PropsData.zig')
-rw-r--r--src/PropsData.zig44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/PropsData.zig b/src/PropsData.zig
index 252462e..9d24e68 100644
--- a/src/PropsData.zig
+++ b/src/PropsData.zig
@@ -9,6 +9,8 @@ core_s1: []u16 = undefined,
9core_s2: []u8 = undefined, 9core_s2: []u8 = undefined,
10props_s1: []u16 = undefined, 10props_s1: []u16 = undefined,
11props_s2: []u8 = undefined, 11props_s2: []u8 = undefined,
12num_s1: []u16 = undefined,
13num_s2: []u8 = undefined,
12 14
13const Self = @This(); 15const Self = @This();
14 16
@@ -52,6 +54,23 @@ pub fn init(allocator: mem.Allocator) !Self {
52 errdefer allocator.free(self.props_s2); 54 errdefer allocator.free(self.props_s2);
53 _ = try props_reader.readAll(self.props_s2); 55 _ = try props_reader.readAll(self.props_s2);
54 56
57 // Process DerivedNumericType.txt
58 const num_bytes = @embedFile("numeric");
59 var num_fbs = std.io.fixedBufferStream(num_bytes);
60 var num_decomp = try decompressor(allocator, num_fbs.reader(), null);
61 defer num_decomp.deinit();
62 var num_reader = num_decomp.reader();
63
64 const num_stage_1_len: u16 = try num_reader.readInt(u16, endian);
65 self.num_s1 = try allocator.alloc(u16, num_stage_1_len);
66 errdefer allocator.free(self.num_s1);
67 for (0..num_stage_1_len) |i| self.num_s1[i] = try num_reader.readInt(u16, endian);
68
69 const num_stage_2_len: u16 = try num_reader.readInt(u16, endian);
70 self.num_s2 = try allocator.alloc(u8, num_stage_2_len);
71 errdefer allocator.free(self.num_s2);
72 _ = try num_reader.readAll(self.num_s2);
73
55 return self; 74 return self;
56} 75}
57 76
@@ -60,6 +79,8 @@ pub fn deinit(self: *const Self) void {
60 self.allocator.free(self.core_s2); 79 self.allocator.free(self.core_s2);
61 self.allocator.free(self.props_s1); 80 self.allocator.free(self.props_s1);
62 self.allocator.free(self.props_s2); 81 self.allocator.free(self.props_s2);
82 self.allocator.free(self.num_s1);
83 self.allocator.free(self.num_s2);
63} 84}
64 85
65/// True if `cp` is a mathematical symbol. 86/// True if `cp` is a mathematical symbol.
@@ -107,6 +128,21 @@ pub inline fn isDiacritic(self: Self, cp: u21) bool {
107 return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 4 == 4; 128 return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 4 == 4;
108} 129}
109 130
131/// True if `cp` is numeric.
132pub inline fn isNumeric(self: Self, cp: u21) bool {
133 return self.num_s2[self.num_s1[cp >> 8] + (cp & 0xff)] & 1 == 1;
134}
135
136/// True if `cp` is a digit.
137pub inline fn isDigit(self: Self, cp: u21) bool {
138 return self.num_s2[self.num_s1[cp >> 8] + (cp & 0xff)] & 2 == 2;
139}
140
141/// True if `cp` is decimal.
142pub inline fn isDecimal(self: Self, cp: u21) bool {
143 return self.num_s2[self.num_s1[cp >> 8] + (cp & 0xff)] & 4 == 4;
144}
145
110test "Props" { 146test "Props" {
111 const self = try init(testing.allocator); 147 const self = try init(testing.allocator);
112 defer self.deinit(); 148 defer self.deinit();
@@ -120,4 +156,12 @@ test "Props" {
120 try testing.expect(self.isAlphabetic('A')); 156 try testing.expect(self.isAlphabetic('A'));
121 try testing.expect(!self.isAlphabetic('3')); 157 try testing.expect(!self.isAlphabetic('3'));
122 try testing.expect(self.isMath('+')); 158 try testing.expect(self.isMath('+'));
159
160 try testing.expect(self.isNumeric('\u{277f}'));
161 try testing.expect(self.isDigit('\u{2070}'));
162 try testing.expect(self.isDecimal('3'));
163
164 try testing.expect(!self.isNumeric('1'));
165 try testing.expect(!self.isDigit('2'));
166 try testing.expect(!self.isDecimal('g'));
123} 167}