From 7a45f255017adfdd8ebc24740d97ee1830e0c6e4 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sun, 9 Mar 2025 06:23:40 -0700 Subject: [PATCH] termwiz: add is_white_space_(char|grapheme) helper functions These are not really expected to be used by most applications. They are being added to the cell module to support some higher level checks as part of trying to handle these wezterm issues a bit better: https://github.com/wezterm/wezterm/issues/1422 https://github.com/wezterm/wezterm/issues/6637 These functions use the White_Space properties as defined by the UCD version 16. I'm using the trie form of the data here because I'm already using the ucd-trie module on other properties. The dataset for White_Space is quite small, and is probably fine to use a binary search on the non-ucd form of the data, I just didn't have an example of that code hanging around and didn't want to have to get distracted debugging that; I somehow always end up with an off-by-one somewhere when hooking up a binary search for the first time! This commit has no functional impact on termwiz or any other crate in the wezterm repo. --- termwiz/src/cell.rs | 44 +++++++++++++++++++ termwiz/src/lib.rs | 1 + termwiz/src/white_space.rs | 88 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+) create mode 100644 termwiz/src/white_space.rs diff --git a/termwiz/src/cell.rs b/termwiz/src/cell.rs index f9bab4234..58e240285 100644 --- a/termwiz/src/cell.rs +++ b/termwiz/src/cell.rs @@ -927,6 +927,22 @@ pub const LATEST_UNICODE_VERSION: UnicodeVersion = UnicodeVersion { ambiguous_are_wide: false, }; +/// Returns true if the char `c` has the unicode White_Space property +pub fn is_white_space_char(c: char) -> bool { + crate::white_space::WHITE_SPACE.contains_u32(c as u32) +} + +/// Returns true if the grapheme string `g` consists entirely of characters +/// that have the unicode White_Space property. +pub fn is_white_space_grapheme(g: &str) -> bool { + for c in g.chars() { + if !is_white_space_char(c) { + return false; + } + } + true +} + /// Returns the number of cells visually occupied by a sequence /// of graphemes. /// Calls through to `grapheme_column_width` for each grapheme @@ -1228,4 +1244,32 @@ mod test { assert_eq!(unicode_column_width(sequence2, None), 2); assert_eq!(grapheme_column_width(sequence2, None), 2); } + + // See + // We're not directly "fixing" that issue here in termwiz at this time + // because it isn't clear that this cell module has enough context + // to eg: decide that the width of U+2028 should be returned as 1. + // That decision is made over in wezterm-term when processing + // a sequence of graphemes. This test case is just making assertions + // about the properties of a couple of problematic zero-width + // characters. + #[test] + fn issue_6637() { + // U+2028 is the unicode line separator. It is Non-printing White_Space. + let sequence = "\u{2028}"; + // It has zero width + assert_eq!(unicode_column_width(sequence, None), 0); + assert_eq!(grapheme_column_width(sequence, None), 0); + // it is white space + assert!(is_white_space_grapheme(sequence)); + + // Just a couple of sanity checks for the white space function + assert!(is_white_space_char(' ')); + assert!(!is_white_space_char('x')); + + // U+2068 is a BIDI control character and is relevant here + // due to . + // It is Non-Printing, non-White_Space + assert!(!is_white_space_char('\u{2068}')); + } } diff --git a/termwiz/src/lib.rs b/termwiz/src/lib.rs index 5184a09bf..812321a32 100644 --- a/termwiz/src/lib.rs +++ b/termwiz/src/lib.rs @@ -43,6 +43,7 @@ mod emoji; mod emoji_presentation; mod emoji_variation; +mod white_space; mod widechar_width; pub mod caps; diff --git a/termwiz/src/white_space.rs b/termwiz/src/white_space.rs new file mode 100644 index 000000000..ca9f5e0fa --- /dev/null +++ b/termwiz/src/white_space.rs @@ -0,0 +1,88 @@ +// DO NOT EDIT THIS FILE. IT WAS AUTOMATICALLY GENERATED BY: +// +// ucd-generate property-bool . --include White_Space --trie-set +// +// Unicode version: 16.0.0. +// +// ucd-generate 0.3.1 is available on crates.io. + +#[allow(unused)] +pub const BY_NAME: &'static [(&'static str, &'static ::ucd_trie::TrieSet)] = + &[("White_Space", WHITE_SPACE)]; + +pub const WHITE_SPACE: &'static ::ucd_trie::TrieSet = &::ucd_trie::TrieSet { + tree1_level1: &[ + 0x100003E00, + 0, + 0x100000020, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + 0, + ], + tree2_level1: &[ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,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