From 4c9b673c7f47d8a2090499f8e5c222312b284725 Mon Sep 17 00:00:00 2001 From: Jose Colon Rodriguez Date: Tue, 26 Mar 2024 07:45:32 -0400 Subject: Removed title case processing --- src/CaseData.zig | 50 +++++++++++++++----------------------------------- 1 file changed, 15 insertions(+), 35 deletions(-) (limited to 'src/CaseData.zig') 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; const CodePointIterator = @import("code_point").Iterator; allocator: mem.Allocator, -case_map: [][3]u21, +case_map: [][2]u21, prop_s1: []u16 = undefined, prop_s2: []u8 = undefined, @@ -20,13 +20,13 @@ pub fn init(allocator: mem.Allocator) !Self { var self = Self{ .allocator = allocator, - .case_map = try allocator.alloc([3]u21, 0x110000), + .case_map = try allocator.alloc([2]u21, 0x110000), }; errdefer allocator.free(self.case_map); for (0..0x110000) |i| { const cp: u21 = @intCast(i); - self.case_map[cp] = .{ cp, cp, cp }; + self.case_map[cp] = .{ cp, cp }; } // Uppercase @@ -55,19 +55,6 @@ pub fn init(allocator: mem.Allocator) !Self { self.case_map[cp][1] = @intCast(try lower_reader.readInt(u24, endian)); } - // Titlercase - const title_bytes = @embedFile("title"); - var title_fbs = std.io.fixedBufferStream(title_bytes); - var title_decomp = try decompressor(allocator, title_fbs.reader(), null); - defer title_decomp.deinit(); - var title_reader = title_decomp.reader(); - - while (true) { - const cp = try title_reader.readInt(u24, endian); - if (cp == 0) break; - self.case_map[cp][2] = @intCast(try title_reader.readInt(u24, endian)); - } - // Case properties const cp_bytes = @embedFile("case_prop"); var cp_fbs = std.io.fixedBufferStream(cp_bytes); @@ -101,7 +88,6 @@ pub inline fn isCased(self: Self, cp: u21) bool { // Returns true if `cp` is uppercase. pub fn isUpper(self: Self, cp: u21) bool { - if (!self.isCased(cp)) return true; return self.prop_s2[self.prop_s1[cp >> 8] + (cp & 0xff)] & 2 == 2; } @@ -110,7 +96,7 @@ pub fn isUpperStr(self: Self, str: []const u8) bool { var iter = CodePointIterator{ .bytes = str }; return while (iter.next()) |cp| { - if (!self.isUpper(cp.code)) break false; + if (self.isCased(cp.code) and !self.isUpper(cp.code)) break false; } else true; } @@ -123,6 +109,11 @@ test "isUpperStr" { try testing.expect(!cd.isUpperStr("Hello, World 2112!")); } +/// Returns uppercase mapping for `cp`. +pub inline fn toUpper(self: Self, cp: u21) u21 { + return self.case_map[cp][0]; +} + /// Returns a new string with all letters in uppercase. /// Caller must free returned bytes with `allocator`. pub fn toUpperStr( @@ -153,28 +144,17 @@ test "toUpperStr" { try testing.expectEqualStrings("HELLO, WORLD 2112!", uppered); } -/// Returns uppercase mapping for `cp`. -pub inline fn toUpper(self: Self, cp: u21) u21 { - return self.case_map[cp][0]; -} - // Returns true if `cp` is lowercase. pub fn isLower(self: Self, cp: u21) bool { - if (!self.isCased(cp)) return true; return self.prop_s2[self.prop_s1[cp >> 8] + (cp & 0xff)] & 1 == 1; } -/// Returns lowercase mapping for `cp`. -pub inline fn toLower(self: Self, cp: u21) u21 { - return self.case_map[cp][1]; -} - /// Returns true if `str` is all lowercase. pub fn isLowerStr(self: Self, str: []const u8) bool { var iter = CodePointIterator{ .bytes = str }; return while (iter.next()) |cp| { - if (!self.isLower(cp.code)) break false; + if (self.isCased(cp.code) and !self.isLower(cp.code)) break false; } else true; } @@ -187,6 +167,11 @@ test "isLowerStr" { try testing.expect(!cd.isLowerStr("Hello, World 2112!")); } +/// Returns lowercase mapping for `cp`. +pub inline fn toLower(self: Self, cp: u21) u21 { + return self.case_map[cp][1]; +} + /// Returns a new string with all letters in lowercase. /// Caller must free returned bytes with `allocator`. pub fn toLowerStr( @@ -216,8 +201,3 @@ test "toLowerStr" { defer testing.allocator.free(lowered); try testing.expectEqualStrings("hello, world 2112!", lowered); } - -/// Returns titlecase mapping for `cp`. -pub inline fn toTitle(self: Self, cp: u21) u21 { - return self.case_map[cp][2]; -} -- cgit v1.2.3