Skip to content

Commit

Permalink
Grapheme clusters
Browse files Browse the repository at this point in the history
See #826
  • Loading branch information
gwenn authored Jan 15, 2025
1 parent c0c9ac3 commit 0239ef2
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,23 @@ pub(crate) fn swidth(s: &str) -> Unit {
Unit::try_from(s.width()).unwrap()
}

fn wcwidth(s: &str) -> Unit {

Check warning on line 16 in src/layout.rs

View workflow job for this annotation

GitHub Actions / Test min versions

function `wcwidth` is never used
let mut width = 0;
for c in s.chars() {
width += cwidh(c);
}
width
}

const ZWJ: char = '\u{200D}';

Check warning on line 24 in src/layout.rs

View workflow job for this annotation

GitHub Actions / Test min versions

constant `ZWJ` is never used
fn no_zwj(s: &str) -> Unit {

Check warning on line 25 in src/layout.rs

View workflow job for this annotation

GitHub Actions / Test min versions

function `no_zwj` is never used
let mut width = 0;
for x in s.split(ZWJ) {
width += swidth(x);
}
width
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
pub struct Position {
pub col: Unit, // The leftmost column is number 0.
Expand Down Expand Up @@ -44,3 +61,34 @@ pub struct Layout {
/// Number of rows used so far (from start of prompt to end of input)
pub end: Position,
}

#[cfg(test)]
mod test {
#[test]
fn unicode_width() {
assert_eq!(1, super::swidth("a"));
assert_eq!(2, super::swidth("πŸ‘©β€πŸš€"));
assert_eq!(2, super::swidth("πŸ‘‹πŸΏ"));
assert_eq!(2, super::swidth("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦"));
assert_eq!(2, super::swidth("πŸ‘©πŸΌβ€πŸ‘¨πŸΌβ€πŸ‘¦πŸΌβ€πŸ‘¦πŸΌ"));
assert_eq!(2, super::swidth("❀️"));
}
#[test]
fn test_wcwidth() {
assert_eq!(1, super::wcwidth("a"));
assert_eq!(4, super::wcwidth("πŸ‘©β€πŸš€"));
assert_eq!(4, super::wcwidth("πŸ‘‹πŸΏ"));
assert_eq!(8, super::wcwidth("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦"));
assert_eq!(16, super::wcwidth("πŸ‘©πŸΌβ€πŸ‘¨πŸΌβ€πŸ‘¦πŸΌβ€πŸ‘¦πŸΌ"));
assert_eq!(1, super::wcwidth("❀️"));
}
#[test]
fn test_no_zwj() {
assert_eq!(1, super::no_zwj("a"));
assert_eq!(4, super::no_zwj("πŸ‘©β€πŸš€"));
assert_eq!(2, super::no_zwj("πŸ‘‹πŸΏ"));
assert_eq!(8, super::no_zwj("πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦"));
assert_eq!(8, super::no_zwj("πŸ‘©πŸΌβ€πŸ‘¨πŸΌβ€πŸ‘¦πŸΌβ€πŸ‘¦πŸΌ"));
assert_eq!(2, super::no_zwj("❀️"));
}
}

0 comments on commit 0239ef2

Please sign in to comment.