From 7a212f5ec5aabf016d17d3ed28649e7982b810ef Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Wed, 30 Apr 2025 12:02:17 -0400 Subject: grapheme now Graphemes, Data files gone --- src/WidthData.zig | 102 ------------------------------------------------------ 1 file changed, 102 deletions(-) delete mode 100644 src/WidthData.zig (limited to 'src/WidthData.zig') diff --git a/src/WidthData.zig b/src/WidthData.zig deleted file mode 100644 index ca7eaf0..0000000 --- a/src/WidthData.zig +++ /dev/null @@ -1,102 +0,0 @@ -const std = @import("std"); -const builtin = @import("builtin"); -const compress = std.compress; -const mem = std.mem; -const testing = std.testing; - -const Graphemes = @import("Graphemes"); - -g_data: Graphemes, -s1: []u16 = undefined, -s2: []i4 = undefined, -owns_gdata: bool, - -const Self = @This(); - -pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { - var self: Self = try Self.setup(allocator); - errdefer { - allocator.free(self.s1); - allocator.free(self.s2); - } - self.owns_gdata = true; - self.g_data = try Graphemes.init(allocator); - errdefer self.g_data.deinit(allocator); - return self; -} - -pub fn initWithGraphemeData(allocator: mem.Allocator, g_data: Graphemes) mem.Allocator.Error!Self { - var self = try Self.setup(allocator); - self.g_data = g_data; - self.owns_gdata = false; - return self; -} - -// Sets up the DisplayWidthData, leaving the GraphemeData undefined. -fn setup(allocator: mem.Allocator) mem.Allocator.Error!Self { - const decompressor = compress.flate.inflate.decompressor; - const in_bytes = @embedFile("dwp"); - var in_fbs = std.io.fixedBufferStream(in_bytes); - var in_decomp = decompressor(.raw, in_fbs.reader()); - var reader = in_decomp.reader(); - - const endian = builtin.cpu.arch.endian(); - - var self: Self = undefined; - - const stage_1_len: u16 = reader.readInt(u16, endian) catch unreachable; - self.s1 = try allocator.alloc(u16, stage_1_len); - errdefer allocator.free(self.s1); - for (0..stage_1_len) |i| self.s1[i] = reader.readInt(u16, endian) catch unreachable; - - const stage_2_len: u16 = reader.readInt(u16, endian) catch unreachable; - self.s2 = try allocator.alloc(i4, stage_2_len); - errdefer allocator.free(self.s2); - for (0..stage_2_len) |i| self.s2[i] = @intCast(reader.readInt(i8, endian) catch unreachable); - - return self; -} - -pub fn deinit(self: *const Self, allocator: mem.Allocator) void { - allocator.free(self.s1); - allocator.free(self.s2); - if (self.owns_gdata) self.g_data.deinit(allocator); -} - -/// codePointWidth returns the number of cells `cp` requires when rendered -/// in a fixed-pitch font (i.e. a terminal screen). This can range from -1 to -/// 3, where BACKSPACE and DELETE return -1 and 3-em-dash returns 3. C0/C1 -/// control codes return 0. If `cjk` is true, ambiguous code points return 2, -/// otherwise they return 1. -pub fn codePointWidth(self: Self, cp: u21) i4 { - return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; -} - -test "codePointWidth" { - const wd = try Self.init(std.testing.allocator); - defer wd.deinit(std.testing.allocator); - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x0000)); // null - try testing.expectEqual(@as(i4, -1), wd.codePointWidth(0x8)); // \b - try testing.expectEqual(@as(i4, -1), wd.codePointWidth(0x7f)); // DEL - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x0005)); // Cf - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x0007)); // \a BEL - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000A)); // \n LF - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000B)); // \v VT - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000C)); // \f FF - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000D)); // \r CR - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000E)); // SQ - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x000F)); // SI - - try testing.expectEqual(@as(i4, 0), wd.codePointWidth(0x070F)); // Cf - try testing.expectEqual(@as(i4, 1), wd.codePointWidth(0x0603)); // Cf Arabic - - try testing.expectEqual(@as(i4, 1), wd.codePointWidth(0x00AD)); // soft-hyphen - try testing.expectEqual(@as(i4, 2), wd.codePointWidth(0x2E3A)); // two-em dash - try testing.expectEqual(@as(i4, 3), wd.codePointWidth(0x2E3B)); // three-em dash - - try testing.expectEqual(@as(i4, 1), wd.codePointWidth(0x00BD)); // ambiguous halfwidth - - try testing.expectEqual(@as(i4, 1), wd.codePointWidth('é')); - try testing.expectEqual(@as(i4, 2), wd.codePointWidth('😊')); - try testing.expectEqual(@as(i4, 2), wd.codePointWidth('统')); -} -- cgit v1.2.3