summaryrefslogtreecommitdiff
path: root/src
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
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')
-rw-r--r--src/NumericData.zig69
-rw-r--r--src/PropsData.zig44
2 files changed, 44 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}
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}