diff options
| author | 2024-04-11 08:31:57 -0500 | |
|---|---|---|
| committer | 2024-04-11 08:35:54 -0500 | |
| commit | e9e5bbcc854ca83ea75f9c7ef34b566aa43e8569 (patch) | |
| tree | 837a163a54d0689db4d992b651d6cbf52036bd35 | |
| parent | Updated benchmarks (diff) | |
| download | zg-e9e5bbcc854ca83ea75f9c7ef34b566aa43e8569.tar.gz zg-e9e5bbcc854ca83ea75f9c7ef34b566aa43e8569.tar.xz zg-e9e5bbcc854ca83ea75f9c7ef34b566aa43e8569.zip | |
DisplayWidth: explicitly set width to 2 when VS16 is found
Explicitly set the width of an emoji to two when the next codepoint is a
VS16 selector. Add unit test for this case.
This is essentially the same PR as
https://codeberg.org/dude_the_builder/ziglyph/pulls/11
| -rw-r--r-- | src/DisplayWidth.zig | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/src/DisplayWidth.zig b/src/DisplayWidth.zig index e547adf..621b8c1 100644 --- a/src/DisplayWidth.zig +++ b/src/DisplayWidth.zig | |||
| @@ -39,6 +39,7 @@ pub fn strWidth(self: Self, str: []const u8) usize { | |||
| 39 | if (cp_iter.next()) |ncp| { | 39 | if (cp_iter.next()) |ncp| { |
| 40 | // emoji text sequence. | 40 | // emoji text sequence. |
| 41 | if (ncp.code == 0xFE0E) w = 1; | 41 | if (ncp.code == 0xFE0E) w = 1; |
| 42 | if (ncp.code == 0xFE0F) w = 2; | ||
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | // Only adding width of first non-zero-width code point. | 45 | // Only adding width of first non-zero-width code point. |
| @@ -70,6 +71,9 @@ test "strWidth" { | |||
| 70 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{26A1}")); // Lone emoji | 71 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{26A1}")); // Lone emoji |
| 71 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{26A1}\u{FE0E}")); // Text sequence | 72 | try testing.expectEqual(@as(usize, 1), self.strWidth("\u{26A1}\u{FE0E}")); // Text sequence |
| 72 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{26A1}\u{FE0F}")); // Presentation sequence | 73 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{26A1}\u{FE0F}")); // Presentation sequence |
| 74 | 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 | ||
| 76 | try testing.expectEqual(@as(usize, 2), self.strWidth("\u{2764}\u{FE0F}")); // Default text presentation with VS16 selector | ||
| 73 | try testing.expectEqual(@as(usize, 0), self.strWidth("A\x08")); // Backspace | 77 | try testing.expectEqual(@as(usize, 0), self.strWidth("A\x08")); // Backspace |
| 74 | try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA")); // DEL | 78 | try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA")); // DEL |
| 75 | try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA\x08\x08")); // never less than o | 79 | try testing.expectEqual(@as(usize, 0), self.strWidth("\x7FA\x08\x08")); // never less than o |