summaryrefslogtreecommitdiff
path: root/src/CaseData.zig
diff options
context:
space:
mode:
authorGravatar Jose Colon Rodriguez2024-03-26 07:45:32 -0400
committerGravatar Jose Colon Rodriguez2024-03-26 07:45:32 -0400
commit4c9b673c7f47d8a2090499f8e5c222312b284725 (patch)
tree5246f97dd39d3cea7ca309b9d91728e3e644ed6f /src/CaseData.zig
parentCaseData (diff)
downloadzg-4c9b673c7f47d8a2090499f8e5c222312b284725.tar.gz
zg-4c9b673c7f47d8a2090499f8e5c222312b284725.tar.xz
zg-4c9b673c7f47d8a2090499f8e5c222312b284725.zip
Removed title case processing
Diffstat (limited to 'src/CaseData.zig')
-rw-r--r--src/CaseData.zig50
1 files changed, 15 insertions, 35 deletions
diff --git a/src/CaseData.zig b/src/CaseData.zig
index 38830e3..d790e8c 100644
--- a/src/CaseData.zig
+++ b/src/CaseData.zig
@@ -8,7 +8,7 @@ const unicode = std.unicode;
8const CodePointIterator = @import("code_point").Iterator; 8const CodePointIterator = @import("code_point").Iterator;
9 9
10allocator: mem.Allocator, 10allocator: mem.Allocator,
11case_map: [][3]u21, 11case_map: [][2]u21,
12prop_s1: []u16 = undefined, 12prop_s1: []u16 = undefined,
13prop_s2: []u8 = undefined, 13prop_s2: []u8 = undefined,
14 14
@@ -20,13 +20,13 @@ pub fn init(allocator: mem.Allocator) !Self {
20 20
21 var self = Self{ 21 var self = Self{
22 .allocator = allocator, 22 .allocator = allocator,
23 .case_map = try allocator.alloc([3]u21, 0x110000), 23 .case_map = try allocator.alloc([2]u21, 0x110000),
24 }; 24 };
25 errdefer allocator.free(self.case_map); 25 errdefer allocator.free(self.case_map);
26 26
27 for (0..0x110000) |i| { 27 for (0..0x110000) |i| {
28 const cp: u21 = @intCast(i); 28 const cp: u21 = @intCast(i);
29 self.case_map[cp] = .{ cp, cp, cp }; 29 self.case_map[cp] = .{ cp, cp };
30 } 30 }
31 31
32 // Uppercase 32 // Uppercase
@@ -55,19 +55,6 @@ pub fn init(allocator: mem.Allocator) !Self {
55 self.case_map[cp][1] = @intCast(try lower_reader.readInt(u24, endian)); 55 self.case_map[cp][1] = @intCast(try lower_reader.readInt(u24, endian));
56 } 56 }
57 57
58 // Titlercase
59 const title_bytes = @embedFile("title");
60 var title_fbs = std.io.fixedBufferStream(title_bytes);
61 var title_decomp = try decompressor(allocator, title_fbs.reader(), null);
62 defer title_decomp.deinit();
63 var title_reader = title_decomp.reader();
64
65 while (true) {
66 const cp = try title_reader.readInt(u24, endian);
67 if (cp == 0) break;
68 self.case_map[cp][2] = @intCast(try title_reader.readInt(u24, endian));
69 }
70
71 // Case properties 58 // Case properties
72 const cp_bytes = @embedFile("case_prop"); 59 const cp_bytes = @embedFile("case_prop");
73 var cp_fbs = std.io.fixedBufferStream(cp_bytes); 60 var cp_fbs = std.io.fixedBufferStream(cp_bytes);
@@ -101,7 +88,6 @@ pub inline fn isCased(self: Self, cp: u21) bool {
101 88
102// Returns true if `cp` is uppercase. 89// Returns true if `cp` is uppercase.
103pub fn isUpper(self: Self, cp: u21) bool { 90pub fn isUpper(self: Self, cp: u21) bool {
104 if (!self.isCased(cp)) return true;
105 return self.prop_s2[self.prop_s1[cp >> 8] + (cp & 0xff)] & 2 == 2; 91 return self.prop_s2[self.prop_s1[cp >> 8] + (cp & 0xff)] & 2 == 2;
106} 92}
107 93
@@ -110,7 +96,7 @@ pub fn isUpperStr(self: Self, str: []const u8) bool {
110 var iter = CodePointIterator{ .bytes = str }; 96 var iter = CodePointIterator{ .bytes = str };
111 97
112 return while (iter.next()) |cp| { 98 return while (iter.next()) |cp| {
113 if (!self.isUpper(cp.code)) break false; 99 if (self.isCased(cp.code) and !self.isUpper(cp.code)) break false;
114 } else true; 100 } else true;
115} 101}
116 102
@@ -123,6 +109,11 @@ test "isUpperStr" {
123 try testing.expect(!cd.isUpperStr("Hello, World 2112!")); 109 try testing.expect(!cd.isUpperStr("Hello, World 2112!"));
124} 110}
125 111
112/// Returns uppercase mapping for `cp`.
113pub inline fn toUpper(self: Self, cp: u21) u21 {
114 return self.case_map[cp][0];
115}
116
126/// Returns a new string with all letters in uppercase. 117/// Returns a new string with all letters in uppercase.
127/// Caller must free returned bytes with `allocator`. 118/// Caller must free returned bytes with `allocator`.
128pub fn toUpperStr( 119pub fn toUpperStr(
@@ -153,28 +144,17 @@ test "toUpperStr" {
153 try testing.expectEqualStrings("HELLO, WORLD 2112!", uppered); 144 try testing.expectEqualStrings("HELLO, WORLD 2112!", uppered);
154} 145}
155 146
156/// Returns uppercase mapping for `cp`.
157pub inline fn toUpper(self: Self, cp: u21) u21 {
158 return self.case_map[cp][0];
159}
160
161// Returns true if `cp` is lowercase. 147// Returns true if `cp` is lowercase.
162pub fn isLower(self: Self, cp: u21) bool { 148pub fn isLower(self: Self, cp: u21) bool {
163 if (!self.isCased(cp)) return true;
164 return self.prop_s2[self.prop_s1[cp >> 8] + (cp & 0xff)] & 1 == 1; 149 return self.prop_s2[self.prop_s1[cp >> 8] + (cp & 0xff)] & 1 == 1;
165} 150}
166 151
167/// Returns lowercase mapping for `cp`.
168pub inline fn toLower(self: Self, cp: u21) u21 {
169 return self.case_map[cp][1];
170}
171
172/// Returns true if `str` is all lowercase. 152/// Returns true if `str` is all lowercase.
173pub fn isLowerStr(self: Self, str: []const u8) bool { 153pub fn isLowerStr(self: Self, str: []const u8) bool {
174 var iter = CodePointIterator{ .bytes = str }; 154 var iter = CodePointIterator{ .bytes = str };
175 155
176 return while (iter.next()) |cp| { 156 return while (iter.next()) |cp| {
177 if (!self.isLower(cp.code)) break false; 157 if (self.isCased(cp.code) and !self.isLower(cp.code)) break false;
178 } else true; 158 } else true;
179} 159}
180 160
@@ -187,6 +167,11 @@ test "isLowerStr" {
187 try testing.expect(!cd.isLowerStr("Hello, World 2112!")); 167 try testing.expect(!cd.isLowerStr("Hello, World 2112!"));
188} 168}
189 169
170/// Returns lowercase mapping for `cp`.
171pub inline fn toLower(self: Self, cp: u21) u21 {
172 return self.case_map[cp][1];
173}
174
190/// Returns a new string with all letters in lowercase. 175/// Returns a new string with all letters in lowercase.
191/// Caller must free returned bytes with `allocator`. 176/// Caller must free returned bytes with `allocator`.
192pub fn toLowerStr( 177pub fn toLowerStr(
@@ -216,8 +201,3 @@ test "toLowerStr" {
216 defer testing.allocator.free(lowered); 201 defer testing.allocator.free(lowered);
217 try testing.expectEqualStrings("hello, world 2112!", lowered); 202 try testing.expectEqualStrings("hello, world 2112!", lowered);
218} 203}
219
220/// Returns titlecase mapping for `cp`.
221pub inline fn toTitle(self: Self, cp: u21) u21 {
222 return self.case_map[cp][2];
223}