diff options
Diffstat (limited to 'src/PropsData.zig')
| -rw-r--r-- | src/PropsData.zig | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/src/PropsData.zig b/src/PropsData.zig new file mode 100644 index 0000000..252462e --- /dev/null +++ b/src/PropsData.zig | |||
| @@ -0,0 +1,123 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | const testing = std.testing; | ||
| 6 | |||
| 7 | allocator: mem.Allocator, | ||
| 8 | core_s1: []u16 = undefined, | ||
| 9 | core_s2: []u8 = undefined, | ||
| 10 | props_s1: []u16 = undefined, | ||
| 11 | props_s2: []u8 = undefined, | ||
| 12 | |||
| 13 | const Self = @This(); | ||
| 14 | |||
| 15 | pub fn init(allocator: mem.Allocator) !Self { | ||
| 16 | const decompressor = compress.deflate.decompressor; | ||
| 17 | const endian = builtin.cpu.arch.endian(); | ||
| 18 | |||
| 19 | // Process DerivedCoreProperties.txt | ||
| 20 | const core_bytes = @embedFile("core_props"); | ||
| 21 | var core_fbs = std.io.fixedBufferStream(core_bytes); | ||
| 22 | var core_decomp = try decompressor(allocator, core_fbs.reader(), null); | ||
| 23 | defer core_decomp.deinit(); | ||
| 24 | var core_reader = core_decomp.reader(); | ||
| 25 | |||
| 26 | var self = Self{ .allocator = allocator }; | ||
| 27 | |||
| 28 | const core_stage_1_len: u16 = try core_reader.readInt(u16, endian); | ||
| 29 | self.core_s1 = try allocator.alloc(u16, core_stage_1_len); | ||
| 30 | errdefer allocator.free(self.core_s1); | ||
| 31 | for (0..core_stage_1_len) |i| self.core_s1[i] = try core_reader.readInt(u16, endian); | ||
| 32 | |||
| 33 | const core_stage_2_len: u16 = try core_reader.readInt(u16, endian); | ||
| 34 | self.core_s2 = try allocator.alloc(u8, core_stage_2_len); | ||
| 35 | errdefer allocator.free(self.core_s2); | ||
| 36 | _ = try core_reader.readAll(self.core_s2); | ||
| 37 | |||
| 38 | // Process PropList.txt | ||
| 39 | const props_bytes = @embedFile("props"); | ||
| 40 | var props_fbs = std.io.fixedBufferStream(props_bytes); | ||
| 41 | var props_decomp = try decompressor(allocator, props_fbs.reader(), null); | ||
| 42 | defer props_decomp.deinit(); | ||
| 43 | var props_reader = props_decomp.reader(); | ||
| 44 | |||
| 45 | const stage_1_len: u16 = try props_reader.readInt(u16, endian); | ||
| 46 | self.props_s1 = try allocator.alloc(u16, stage_1_len); | ||
| 47 | errdefer allocator.free(self.props_s1); | ||
| 48 | for (0..stage_1_len) |i| self.props_s1[i] = try props_reader.readInt(u16, endian); | ||
| 49 | |||
| 50 | const stage_2_len: u16 = try props_reader.readInt(u16, endian); | ||
| 51 | self.props_s2 = try allocator.alloc(u8, stage_2_len); | ||
| 52 | errdefer allocator.free(self.props_s2); | ||
| 53 | _ = try props_reader.readAll(self.props_s2); | ||
| 54 | |||
| 55 | return self; | ||
| 56 | } | ||
| 57 | |||
| 58 | pub fn deinit(self: *const Self) void { | ||
| 59 | self.allocator.free(self.core_s1); | ||
| 60 | self.allocator.free(self.core_s2); | ||
| 61 | self.allocator.free(self.props_s1); | ||
| 62 | self.allocator.free(self.props_s2); | ||
| 63 | } | ||
| 64 | |||
| 65 | /// True if `cp` is a mathematical symbol. | ||
| 66 | pub inline fn isMath(self: Self, cp: u21) bool { | ||
| 67 | return self.core_s2[self.core_s1[cp >> 8] + (cp & 0xff)] & 1 == 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | /// True if `cp` is an alphabetic character. | ||
| 71 | pub inline fn isAlphabetic(self: Self, cp: u21) bool { | ||
| 72 | return self.core_s2[self.core_s1[cp >> 8] + (cp & 0xff)] & 2 == 2; | ||
| 73 | } | ||
| 74 | |||
| 75 | /// True if `cp` is a valid identifier start character. | ||
| 76 | pub inline fn isIdStart(self: Self, cp: u21) bool { | ||
| 77 | return self.core_s2[self.core_s1[cp >> 8] + (cp & 0xff)] & 4 == 4; | ||
| 78 | } | ||
| 79 | |||
| 80 | /// True if `cp` is a valid identifier continuation character. | ||
| 81 | pub inline fn isIdContinue(self: Self, cp: u21) bool { | ||
| 82 | return self.core_s2[self.core_s1[cp >> 8] + (cp & 0xff)] & 8 == 8; | ||
| 83 | } | ||
| 84 | |||
| 85 | /// True if `cp` is a valid extended identifier start character. | ||
| 86 | pub inline fn isXidStart(self: Self, cp: u21) bool { | ||
| 87 | return self.core_s2[self.core_s1[cp >> 8] + (cp & 0xff)] & 16 == 16; | ||
| 88 | } | ||
| 89 | |||
| 90 | /// True if `cp` is a valid extended identifier continuation character. | ||
| 91 | pub inline fn isXidContinue(self: Self, cp: u21) bool { | ||
| 92 | return self.core_s2[self.core_s1[cp >> 8] + (cp & 0xff)] & 32 == 32; | ||
| 93 | } | ||
| 94 | |||
| 95 | /// True if `cp` is a whitespace character. | ||
| 96 | pub inline fn isWhitespace(self: Self, cp: u21) bool { | ||
| 97 | return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 1 == 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | /// True if `cp` is a hexadecimal digit. | ||
| 101 | pub inline fn isHexDigit(self: Self, cp: u21) bool { | ||
| 102 | return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 2 == 2; | ||
| 103 | } | ||
| 104 | |||
| 105 | /// True if `cp` is a diacritic mark. | ||
| 106 | pub inline fn isDiacritic(self: Self, cp: u21) bool { | ||
| 107 | return self.props_s2[self.props_s1[cp >> 8] + (cp & 0xff)] & 4 == 4; | ||
| 108 | } | ||
| 109 | |||
| 110 | test "Props" { | ||
| 111 | const self = try init(testing.allocator); | ||
| 112 | defer self.deinit(); | ||
| 113 | |||
| 114 | try testing.expect(self.isHexDigit('F')); | ||
| 115 | try testing.expect(self.isHexDigit('a')); | ||
| 116 | try testing.expect(self.isHexDigit('8')); | ||
| 117 | try testing.expect(!self.isHexDigit('z')); | ||
| 118 | |||
| 119 | try testing.expect(self.isDiacritic('\u{301}')); | ||
| 120 | try testing.expect(self.isAlphabetic('A')); | ||
| 121 | try testing.expect(!self.isAlphabetic('3')); | ||
| 122 | try testing.expect(self.isMath('+')); | ||
| 123 | } | ||