diff options
| author | 2024-12-13 15:14:15 -0500 | |
|---|---|---|
| committer | 2025-03-20 20:17:07 -0400 | |
| commit | f37875c6c4e42055735f1cda9fdbcb7ab11b80bc (patch) | |
| tree | c889b4287d9d6d7db4fca9fda475c9fd90b33adc /src | |
| parent | Update LICENSE, add CONTRIBUTORS.md (diff) | |
| download | zg-f37875c6c4e42055735f1cda9fdbcb7ab11b80bc.tar.gz zg-f37875c6c4e42055735f1cda9fdbcb7ab11b80bc.tar.xz zg-f37875c6c4e42055735f1cda9fdbcb7ab11b80bc.zip | |
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}.
Diffstat (limited to 'src')
| -rw-r--r-- | src/DisplayWidth.zig | 14 | ||||
| -rw-r--r-- | src/WidthData.zig | 54 |
2 files changed, 36 insertions, 32 deletions
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 @@ | |||
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const builtin = @import("builtin"); | 2 | const builtin = @import("builtin"); |
| 3 | const options = @import("options"); | ||
| 3 | const ArrayList = std.ArrayList; | 4 | const ArrayList = std.ArrayList; |
| 4 | const mem = std.mem; | 5 | const mem = std.mem; |
| 5 | const simd = std.simd; | 6 | const simd = std.simd; |
| @@ -60,6 +61,7 @@ test "strWidth" { | |||
| 60 | const data = try DisplayWidthData.init(testing.allocator); | 61 | const data = try DisplayWidthData.init(testing.allocator); |
| 61 | defer data.deinit(); | 62 | defer data.deinit(); |
| 62 | const self = Self{ .data = &data }; | 63 | const self = Self{ .data = &data }; |
| 64 | const c0 = options.c0_width orelse 0; | ||
| 63 | 65 | ||
| 64 | try testing.expectEqual(@as(usize, 5), self.strWidth("Hello\r\n")); | 66 | try testing.expectEqual(@as(usize, 5), self.strWidth("Hello\r\n")); |
| 65 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{0065}\u{0301}")); | 67 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{0065}\u{0301}")); |
| @@ -74,19 +76,21 @@ test "strWidth" { | |||
| 74 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}")); // Default text presentation | 76 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}")); // Default text presentation |
| 75 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}\u{FE0E}")); // Default text presentation with VS15 selector | 77 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{2764}\u{FE0E}")); // Default text presentation with VS15 selector |
| 76 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{2764}\u{FE0F}")); // Default text presentation with VS16 selector | 78 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{2764}\u{FE0F}")); // Default text presentation with VS16 selector |
| 77 | try testing.expectEqual(@as(usize, 0), self.strWidth("A\x08")); // Backspace | 79 | const expect_bs: usize = if (c0 == 0) 0 else 1 + c0; |
| 78 | try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA")); // DEL | 80 | try testing.expectEqual(expect_bs, self.strWidth("A\x08")); // Backspace |
| 79 | try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA\x08\x08")); // never less than o | 81 | try testing.expectEqual(expect_bs, self.strWidth("\x7FA")); // DEL |
| 82 | const expect_long_del: usize = if (c0 == 0) 0 else 1 + (c0 * 3); | ||
| 83 | try testing.expectEqual(expect_long_del, self.strWidth("\x7FA\x08\x08")); // never less than 0 | ||
| 80 | 84 | ||
| 81 | // wcwidth Python lib tests. See: https://github.com/jquast/wcwidth/blob/master/tests/test_core.py | 85 | // wcwidth Python lib tests. See: https://github.com/jquast/wcwidth/blob/master/tests/test_core.py |
| 82 | const empty = ""; | 86 | const empty = ""; |
| 83 | try testing.expectEqual(@as(usize, 0), self.strWidth(empty)); | 87 | try testing.expectEqual(@as(usize, 0), self.strWidth(empty)); |
| 84 | const with_null = "hello\x00world"; | 88 | const with_null = "hello\x00world"; |
| 85 | try testing.expectEqual(@as(usize, 10), self.strWidth(with_null)); | 89 | try testing.expectEqual(@as(usize, 10 + c0), self.strWidth(with_null)); |
| 86 | const hello_jp = "コンニチハ, セカイ!"; | 90 | const hello_jp = "コンニチハ, セカイ!"; |
| 87 | try testing.expectEqual(@as(usize, 19), self.strWidth(hello_jp)); | 91 | try testing.expectEqual(@as(usize, 19), self.strWidth(hello_jp)); |
| 88 | const control = "\x1b[0m"; | 92 | const control = "\x1b[0m"; |
| 89 | try testing.expectEqual(@as(usize, 3), self.strWidth(control)); | 93 | try testing.expectEqual(@as(usize, 3 + c0), self.strWidth(control)); |
| 90 | const balinese = "\u{1B13}\u{1B28}\u{1B2E}\u{1B44}"; | 94 | const balinese = "\u{1B13}\u{1B28}\u{1B2E}\u{1B44}"; |
| 91 | try testing.expectEqual(@as(usize, 3), self.strWidth(balinese)); | 95 | try testing.expectEqual(@as(usize, 3), self.strWidth(balinese)); |
| 92 | 96 | ||
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"); | |||
| 9 | allocator: mem.Allocator, | 9 | allocator: mem.Allocator, |
| 10 | g_data: GraphemeData, | 10 | g_data: GraphemeData, |
| 11 | s1: []u16 = undefined, | 11 | s1: []u16 = undefined, |
| 12 | s2: []i3 = undefined, | 12 | s2: []i4 = undefined, |
| 13 | 13 | ||
| 14 | const Self = @This(); | 14 | const Self = @This(); |
| 15 | 15 | ||
| @@ -34,7 +34,7 @@ pub fn init(allocator: mem.Allocator) mem.Allocator.Error!Self { | |||
| 34 | for (0..stage_1_len) |i| self.s1[i] = reader.readInt(u16, endian) catch unreachable; | 34 | for (0..stage_1_len) |i| self.s1[i] = reader.readInt(u16, endian) catch unreachable; |
| 35 | 35 | ||
| 36 | const stage_2_len: u16 = reader.readInt(u16, endian) catch unreachable; | 36 | const stage_2_len: u16 = reader.readInt(u16, endian) catch unreachable; |
| 37 | self.s2 = try allocator.alloc(i3, stage_2_len); | 37 | self.s2 = try allocator.alloc(i4, stage_2_len); |
| 38 | errdefer allocator.free(self.s2); | 38 | errdefer allocator.free(self.s2); |
| 39 | for (0..stage_2_len) |i| self.s2[i] = @intCast(reader.readInt(i8, endian) catch unreachable); | 39 | for (0..stage_2_len) |i| self.s2[i] = @intCast(reader.readInt(i8, endian) catch unreachable); |
| 40 | 40 | ||
| @@ -52,33 +52,33 @@ pub fn deinit(self: *const Self) void { | |||
| 52 | /// 3, where BACKSPACE and DELETE return -1 and 3-em-dash returns 3. C0/C1 | 52 | /// 3, where BACKSPACE and DELETE return -1 and 3-em-dash returns 3. C0/C1 |
| 53 | /// control codes return 0. If `cjk` is true, ambiguous code points return 2, | 53 | /// control codes return 0. If `cjk` is true, ambiguous code points return 2, |
| 54 | /// otherwise they return 1. | 54 | /// otherwise they return 1. |
| 55 | pub fn codePointWidth(self: Self, cp: u21) i3 { | 55 | pub fn codePointWidth(self: Self, cp: u21) i4 { |
| 56 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; | 56 | return self.s2[self.s1[cp >> 8] + (cp & 0xff)]; |
| 57 | } | 57 | } |
| 58 | 58 | ||
| 59 | test "codePointWidth" { | 59 | test "codePointWidth" { |
| 60 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x0000)); // null | 60 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x0000)); // null |
| 61 | try testing.expectEqual(@as(i3, -1), codePointWidth(0x8)); // \b | 61 | try testing.expectEqual(@as(i4, -1), codePointWidth(0x8)); // \b |
| 62 | try testing.expectEqual(@as(i3, -1), codePointWidth(0x7f)); // DEL | 62 | try testing.expectEqual(@as(i4, -1), codePointWidth(0x7f)); // DEL |
| 63 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x0005)); // Cf | 63 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x0005)); // Cf |
| 64 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x0007)); // \a BEL | 64 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x0007)); // \a BEL |
| 65 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x000A)); // \n LF | 65 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x000A)); // \n LF |
| 66 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x000B)); // \v VT | 66 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x000B)); // \v VT |
| 67 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x000C)); // \f FF | 67 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x000C)); // \f FF |
| 68 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x000D)); // \r CR | 68 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x000D)); // \r CR |
| 69 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x000E)); // SQ | 69 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x000E)); // SQ |
| 70 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x000F)); // SI | 70 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x000F)); // SI |
| 71 | 71 | ||
| 72 | try testing.expectEqual(@as(i3, 0), codePointWidth(0x070F)); // Cf | 72 | try testing.expectEqual(@as(i4, 0), codePointWidth(0x070F)); // Cf |
| 73 | try testing.expectEqual(@as(i3, 1), codePointWidth(0x0603)); // Cf Arabic | 73 | try testing.expectEqual(@as(i4, 1), codePointWidth(0x0603)); // Cf Arabic |
| 74 | 74 | ||
| 75 | try testing.expectEqual(@as(i3, 1), codePointWidth(0x00AD)); // soft-hyphen | 75 | try testing.expectEqual(@as(i4, 1), codePointWidth(0x00AD)); // soft-hyphen |
| 76 | try testing.expectEqual(@as(i3, 2), codePointWidth(0x2E3A)); // two-em dash | 76 | try testing.expectEqual(@as(i4, 2), codePointWidth(0x2E3A)); // two-em dash |
| 77 | try testing.expectEqual(@as(i3, 3), codePointWidth(0x2E3B)); // three-em dash | 77 | try testing.expectEqual(@as(i4, 3), codePointWidth(0x2E3B)); // three-em dash |
| 78 | 78 | ||
| 79 | try testing.expectEqual(@as(i3, 1), codePointWidth(0x00BD)); // ambiguous halfwidth | 79 | try testing.expectEqual(@as(i4, 1), codePointWidth(0x00BD)); // ambiguous halfwidth |
| 80 | 80 | ||
| 81 | try testing.expectEqual(@as(i3, 1), codePointWidth('é')); | 81 | try testing.expectEqual(@as(i4, 1), codePointWidth('é')); |
| 82 | try testing.expectEqual(@as(i3, 2), codePointWidth('😊')); | 82 | try testing.expectEqual(@as(i4, 2), codePointWidth('😊')); |
| 83 | try testing.expectEqual(@as(i3, 2), codePointWidth('统')); | 83 | try testing.expectEqual(@as(i4, 2), codePointWidth('统')); |
| 84 | } | 84 | } |