From f37875c6c4e42055735f1cda9fdbcb7ab11b80bc Mon Sep 17 00:00:00 2001 From: Sam Atman Date: Fri, 13 Dec 2024 15:14:15 -0500 Subject: Add c0 and c1 control width options This allows a build of DisplayWidth to give characters in those classes a width, for cases where they'll be printed with a substitute in the final display. It also raises the size of possible characters from an i3 to an i4, to accommodate printing C1s as e.g. <80> or \u{80}. --- src/DisplayWidth.zig | 14 +++++++++----- src/WidthData.zig | 54 ++++++++++++++++++++++++++-------------------------- 2 files changed, 36 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/DisplayWidth.zig b/src/DisplayWidth.zig index 621b8c1..04e6b0c 100644 --- a/src/DisplayWidth.zig +++ b/src/DisplayWidth.zig @@ -1,5 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); +const options = @import("options"); const ArrayList = std.ArrayList; const mem = std.mem; const simd = std.simd; @@ -60,6 +61,7 @@ test "strWidth" { const data = try DisplayWidthData.init(testing.allocator); defer data.deinit(); const self = Self{ .data = &data }; + const c0 = options.c0_width orelse 0; try testing.expectEqual(@as(usize, 5), self.strWidth("Hello\r\n")); try testing.expectEqual(@as(usize, 1), self.strWidth("\u{0065}\u{0301}")); @@ -74,19 +76,21 @@ test "strWidth" { try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}")); // Default text presentation try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}\u{FE0E}")); // Default text presentation with VS15 selector try testing.expectEqual(@as(usize, 2), self.strWidth("\u{2764}\u{FE0F}")); // Default text presentation with VS16 selector - try testing.expectEqual(@as(usize, 0), self.strWidth("A\x08")); // Backspace - try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA")); // DEL - try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA\x08\x08")); // never less than o + const expect_bs: usize = if (c0 == 0) 0 else 1 + c0; + try testing.expectEqual(expect_bs, self.strWidth("A\x08")); // Backspace + try testing.expectEqual(expect_bs, self.strWidth("\x7FA")); // DEL + const expect_long_del: usize = if (c0 == 0) 0 else 1 + (c0 * 3); + try testing.expectEqual(expect_long_del, self.strWidth("\x7FA\x08\x08")); // never less than 0 // wcwidth Python lib tests. See: https://github.com/jquast/wcwidth/blob/master/tests/test_core.py const empty = ""; try testing.expectEqual(@as(usize, 0), self.strWidth(empty)); const with_null = "hello\x00world"; - try testing.expectEqual(@as(usize, 10), self.strWidth(with_null)); + try testing.expectEqual(@as(usize, 10 + c0), self.strWidth(with_null)); const hello_jp = "コンニチハ, セカイ!"; try testing.expectEqual(@as(usize, 19), self.strWidth(hello_jp)); const control = "\x1b[0m"; - try testing.expectEqual(@as(usize, 3), self.strWidth(control)); + try testing.expectEqual(@as(usize, 3 + c0), self.strWidth(control)); const balinese = "\u{1B13}\u{1B28}\u{1B2E}\u{1B44}"; try testing.expectEqual(@as(usize, 3), self.strWidth(balinese)); diff --git a/src/WidthData.zig b/src/WidthData.zig index 1b7fb2e..d77879e 100644 --- a/src/WidthData.zig +++ b/src/WidthData.zig @@ -9,7 +9,7 @@ const GraphemeData = @import("GraphemeData"); allocator: mem.Allocator, g_data: GraphemeData, s1: []u16 = undefined, -s2: []i3 = undefined, +s2: []i4 = undefined, const Self = @This(); @@ -34,7 +34,7 @@ pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { 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(i3, stage_2_len); + 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); @@ -52,33 +52,33 @@ pub fn deinit(self: *const Self) void { /// 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) i3 { +pub fn codePointWidth(self: Self, cp: u21) i4 { return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; } test "codePointWidth" { - try testing.expectEqual(@as(i3, 0), codePointWidth(0x0000)); // null - try testing.expectEqual(@as(i3, -1), codePointWidth(0x8)); // \b - try testing.expectEqual(@as(i3, -1), codePointWidth(0x7f)); // DEL - try testing.expectEqual(@as(i3, 0), codePointWidth(0x0005)); // Cf - try testing.expectEqual(@as(i3, 0), codePointWidth(0x0007)); // \a BEL - try testing.expectEqual(@as(i3, 0), codePointWidth(0x000A)); // \n LF - try testing.expectEqual(@as(i3, 0), codePointWidth(0x000B)); // \v VT - try testing.expectEqual(@as(i3, 0), codePointWidth(0x000C)); // \f FF - try testing.expectEqual(@as(i3, 0), codePointWidth(0x000D)); // \r CR - try testing.expectEqual(@as(i3, 0), codePointWidth(0x000E)); // SQ - try testing.expectEqual(@as(i3, 0), codePointWidth(0x000F)); // SI - - try testing.expectEqual(@as(i3, 0), codePointWidth(0x070F)); // Cf - try testing.expectEqual(@as(i3, 1), codePointWidth(0x0603)); // Cf Arabic - - try testing.expectEqual(@as(i3, 1), codePointWidth(0x00AD)); // soft-hyphen - try testing.expectEqual(@as(i3, 2), codePointWidth(0x2E3A)); // two-em dash - try testing.expectEqual(@as(i3, 3), codePointWidth(0x2E3B)); // three-em dash - - try testing.expectEqual(@as(i3, 1), codePointWidth(0x00BD)); // ambiguous halfwidth - - try testing.expectEqual(@as(i3, 1), codePointWidth('é')); - try testing.expectEqual(@as(i3, 2), codePointWidth('😊')); - try testing.expectEqual(@as(i3, 2), codePointWidth('统')); + try testing.expectEqual(@as(i4, 0), codePointWidth(0x0000)); // null + try testing.expectEqual(@as(i4, -1), codePointWidth(0x8)); // \b + try testing.expectEqual(@as(i4, -1), codePointWidth(0x7f)); // DEL + try testing.expectEqual(@as(i4, 0), codePointWidth(0x0005)); // Cf + try testing.expectEqual(@as(i4, 0), codePointWidth(0x0007)); // \a BEL + try testing.expectEqual(@as(i4, 0), codePointWidth(0x000A)); // \n LF + try testing.expectEqual(@as(i4, 0), codePointWidth(0x000B)); // \v VT + try testing.expectEqual(@as(i4, 0), codePointWidth(0x000C)); // \f FF + try testing.expectEqual(@as(i4, 0), codePointWidth(0x000D)); // \r CR + try testing.expectEqual(@as(i4, 0), codePointWidth(0x000E)); // SQ + try testing.expectEqual(@as(i4, 0), codePointWidth(0x000F)); // SI + + try testing.expectEqual(@as(i4, 0), codePointWidth(0x070F)); // Cf + try testing.expectEqual(@as(i4, 1), codePointWidth(0x0603)); // Cf Arabic + + try testing.expectEqual(@as(i4, 1), codePointWidth(0x00AD)); // soft-hyphen + try testing.expectEqual(@as(i4, 2), codePointWidth(0x2E3A)); // two-em dash + try testing.expectEqual(@as(i4, 3), codePointWidth(0x2E3B)); // three-em dash + + try testing.expectEqual(@as(i4, 1), codePointWidth(0x00BD)); // ambiguous halfwidth + + try testing.expectEqual(@as(i4, 1), codePointWidth('é')); + try testing.expectEqual(@as(i4, 2), codePointWidth('😊')); + try testing.expectEqual(@as(i4, 2), codePointWidth('统')); } -- cgit v1.2.3