diff options
| author | 2025-04-30 15:32:34 -0400 | |
|---|---|---|
| committer | 2025-04-30 15:32:34 -0400 | |
| commit | 958c13ba442e7077a50d7163fdeb9bba378f95c2 (patch) | |
| tree | 0727fd03ea2344ebbad842daa05b55ea0a143a6c /src/GeneralCategories.zig | |
| parent | Remove FoldData, make CaseFolding (diff) | |
| download | zg-958c13ba442e7077a50d7163fdeb9bba378f95c2.tar.gz zg-958c13ba442e7077a50d7163fdeb9bba378f95c2.tar.xz zg-958c13ba442e7077a50d7163fdeb9bba378f95c2.zip | |
Rest of the Renamings
These get different names, but don't otherwise change.
Diffstat (limited to 'src/GeneralCategories.zig')
| -rw-r--r-- | src/GeneralCategories.zig | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/src/GeneralCategories.zig b/src/GeneralCategories.zig new file mode 100644 index 0000000..a69f7a2 --- /dev/null +++ b/src/GeneralCategories.zig | |||
| @@ -0,0 +1,170 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const builtin = @import("builtin"); | ||
| 3 | const compress = std.compress; | ||
| 4 | const mem = std.mem; | ||
| 5 | |||
| 6 | /// General Category | ||
| 7 | pub const Gc = enum { | ||
| 8 | Cc, // Other, Control | ||
| 9 | Cf, // Other, Format | ||
| 10 | Cn, // Other, Unassigned | ||
| 11 | Co, // Other, Private Use | ||
| 12 | Cs, // Other, Surrogate | ||
| 13 | Ll, // Letter, Lowercase | ||
| 14 | Lm, // Letter, Modifier | ||
| 15 | Lo, // Letter, Other | ||
| 16 | Lu, // Letter, Uppercase | ||
| 17 | Lt, // Letter, Titlecase | ||
| 18 | Mc, // Mark, Spacing Combining | ||
| 19 | Me, // Mark, Enclosing | ||
| 20 | Mn, // Mark, Non-Spacing | ||
| 21 | Nd, // Number, Decimal Digit | ||
| 22 | Nl, // Number, Letter | ||
| 23 | No, // Number, Other | ||
| 24 | Pc, // Punctuation, Connector | ||
| 25 | Pd, // Punctuation, Dash | ||
| 26 | Pe, // Punctuation, Close | ||
| 27 | Pf, // Punctuation, Final quote (may behave like Ps or Pe depending on usage) | ||
| 28 | Pi, // Punctuation, Initial quote (may behave like Ps or Pe depending on usage) | ||
| 29 | Po, // Punctuation, Other | ||
| 30 | Ps, // Punctuation, Open | ||
| 31 | Sc, // Symbol, Currency | ||
| 32 | Sk, // Symbol, Modifier | ||
| 33 | Sm, // Symbol, Math | ||
| 34 | So, // Symbol, Other | ||
| 35 | Zl, // Separator, Line | ||
| 36 | Zp, // Separator, Paragraph | ||
| 37 | Zs, // Separator, Space | ||
| 38 | }; | ||
| 39 | |||
| 40 | s1: []u16 = undefined, | ||
| 41 | s2: []u5 = undefined, | ||
| 42 | s3: []u5 = undefined, | ||
| 43 | |||
| 44 | const Self = @This(); | ||
| 45 | |||
| 46 | pub fn init(allocator: mem.Allocator) !Self { | ||
| 47 | const decompressor = compress.flate.inflate.decompressor; | ||
| 48 | const in_bytes = @embedFile("gencat"); | ||
| 49 | var in_fbs = std.io.fixedBufferStream(in_bytes); | ||
| 50 | var in_decomp = decompressor(.raw, in_fbs.reader()); | ||
| 51 | var reader = in_decomp.reader(); | ||
| 52 | |||
| 53 | const endian = builtin.cpu.arch.endian(); | ||
| 54 | |||
| 55 | var self = Self{}; | ||
| 56 | |||
| 57 | const s1_len: u16 = try reader.readInt(u16, endian); | ||
| 58 | self.s1 = try allocator.alloc(u16, s1_len); | ||
| 59 | errdefer allocator.free(self.s1); | ||
| 60 | for (0..s1_len) |i| self.s1[i] = try reader.readInt(u16, endian); | ||
| 61 | |||
| 62 | const s2_len: u16 = try reader.readInt(u16, endian); | ||
| 63 | self.s2 = try allocator.alloc(u5, s2_len); | ||
| 64 | errdefer allocator.free(self.s2); | ||
| 65 | for (0..s2_len) |i| self.s2[i] = @intCast(try reader.readInt(u8, endian)); | ||
| 66 | |||
| 67 | const s3_len: u16 = try reader.readInt(u8, endian); | ||
| 68 | self.s3 = try allocator.alloc(u5, s3_len); | ||
| 69 | errdefer allocator.free(self.s3); | ||
| 70 | for (0..s3_len) |i| self.s3[i] = @intCast(try reader.readInt(u8, endian)); | ||
| 71 | |||
| 72 | return self; | ||
| 73 | } | ||
| 74 | |||
| 75 | pub fn deinit(self: *const Self, allocator: mem.Allocator) void { | ||
| 76 | allocator.free(self.s1); | ||
| 77 | allocator.free(self.s2); | ||
| 78 | allocator.free(self.s3); | ||
| 79 | } | ||
| 80 | |||
| 81 | /// Lookup the General Category for `cp`. | ||
| 82 | pub fn gc(self: Self, cp: u21) Gc { | ||
| 83 | return @enumFromInt(self.s3[self.s2[self.s1[cp >> 8] + (cp & 0xff)]]); | ||
| 84 | } | ||
| 85 | |||
| 86 | /// True if `cp` has an C general category. | ||
| 87 | pub fn isControl(self: Self, cp: u21) bool { | ||
| 88 | return switch (self.gc(cp)) { | ||
| 89 | .Cc, | ||
| 90 | .Cf, | ||
| 91 | .Cn, | ||
| 92 | .Co, | ||
| 93 | .Cs, | ||
| 94 | => true, | ||
| 95 | else => false, | ||
| 96 | }; | ||
| 97 | } | ||
| 98 | |||
| 99 | /// True if `cp` has an L general category. | ||
| 100 | pub fn isLetter(self: Self, cp: u21) bool { | ||
| 101 | return switch (self.gc(cp)) { | ||
| 102 | .Ll, | ||
| 103 | .Lm, | ||
| 104 | .Lo, | ||
| 105 | .Lu, | ||
| 106 | .Lt, | ||
| 107 | => true, | ||
| 108 | else => false, | ||
| 109 | }; | ||
| 110 | } | ||
| 111 | |||
| 112 | /// True if `cp` has an M general category. | ||
| 113 | pub fn isMark(self: Self, cp: u21) bool { | ||
| 114 | return switch (self.gc(cp)) { | ||
| 115 | .Mc, | ||
| 116 | .Me, | ||
| 117 | .Mn, | ||
| 118 | => true, | ||
| 119 | else => false, | ||
| 120 | }; | ||
| 121 | } | ||
| 122 | |||
| 123 | /// True if `cp` has an N general category. | ||
| 124 | pub fn isNumber(self: Self, cp: u21) bool { | ||
| 125 | return switch (self.gc(cp)) { | ||
| 126 | .Nd, | ||
| 127 | .Nl, | ||
| 128 | .No, | ||
| 129 | => true, | ||
| 130 | else => false, | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | |||
| 134 | /// True if `cp` has an P general category. | ||
| 135 | pub fn isPunctuation(self: Self, cp: u21) bool { | ||
| 136 | return switch (self.gc(cp)) { | ||
| 137 | .Pc, | ||
| 138 | .Pd, | ||
| 139 | .Pe, | ||
| 140 | .Pf, | ||
| 141 | .Pi, | ||
| 142 | .Po, | ||
| 143 | .Ps, | ||
| 144 | => true, | ||
| 145 | else => false, | ||
| 146 | }; | ||
| 147 | } | ||
| 148 | |||
| 149 | /// True if `cp` has an S general category. | ||
| 150 | pub fn isSymbol(self: Self, cp: u21) bool { | ||
| 151 | return switch (self.gc(cp)) { | ||
| 152 | .Sc, | ||
| 153 | .Sk, | ||
| 154 | .Sm, | ||
| 155 | .So, | ||
| 156 | => true, | ||
| 157 | else => false, | ||
| 158 | }; | ||
| 159 | } | ||
| 160 | |||
| 161 | /// True if `cp` has an Z general category. | ||
| 162 | pub fn isSeparator(self: Self, cp: u21) bool { | ||
| 163 | return switch (self.gc(cp)) { | ||
| 164 | .Zl, | ||
| 165 | .Zp, | ||
| 166 | .Zs, | ||
| 167 | => true, | ||
| 168 | else => false, | ||
| 169 | }; | ||
| 170 | } | ||