diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-11-02 14:32:18 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-11-02 14:32:18 -0400 |
commit | 09fccc8d8a30ef7eea6a43aaff85fa7ec1ad72ca (patch) | |
tree | dfb083843775b63893235d1a64fe38df20200f49 /src | |
parent | 185c9bb0ea3a1464c608b20ba171e527a62de9cf (diff) | |
download | vt100-rust-09fccc8d8a30ef7eea6a43aaff85fa7ec1ad72ca.tar.gz vt100-rust-09fccc8d8a30ef7eea6a43aaff85fa7ec1ad72ca.zip |
fix some warnings, and make str_width a bit more efficient
Diffstat (limited to 'src')
-rw-r--r-- | src/unicode.rs | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/src/unicode.rs b/src/unicode.rs index 5cf597e..7897792 100644 --- a/src/unicode.rs +++ b/src/unicode.rs @@ -1,14 +1,18 @@ +use std::convert::TryInto as _; use unicode_width::UnicodeWidthChar as _; // soft hyphen is defined as width 1, but in a terminal setting it should // always be width 0 -pub fn char_width(c: char) -> usize { +pub fn char_width(c: char) -> u16 { match c { '\u{00ad}' => 0, - _ => c.width().unwrap_or(0), + _ => c.width().unwrap_or(0).try_into().unwrap(), } } -pub fn str_width(s: &str) -> usize { - s.chars().map(char_width).sum() +// strings in this context should always be an arbitrary character followed by +// zero or more zero-width characters, so we should only have to look at the +// first character +pub fn str_width(s: &str) -> u16 { + s.chars().next().map_or(0, char_width) } |