diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 19db8845380..798046c461d 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -1056,6 +1056,15 @@ mod tests { assert_eq!("\u2620".char_len(), 1u); assert_eq!("\U0001d11e".char_len(), 1u); assert_eq!("ประเทศไทย中华Việt Nam".char_len(), 19u); + + assert_eq!("hello".width(false), 10u); + assert_eq!("hello".width(true), 10u); + assert_eq!("\0\0\0\0\0".width(false), 0u); + assert_eq!("\0\0\0\0\0".width(true), 0u); + assert_eq!("".width(false), 0u); + assert_eq!("".width(true), 0u); + assert_eq!("\u2081\u2082\u2083\u2084".width(false), 4u); + assert_eq!("\u2081\u2082\u2083\u2084".width(true), 8u); } #[test] diff --git a/src/libunicode/u_str.rs b/src/libunicode/u_str.rs index 84a2eab4b25..b0c40cdbcf9 100644 --- a/src/libunicode/u_str.rs +++ b/src/libunicode/u_str.rs @@ -16,10 +16,11 @@ */ use core::collections::Collection; -use core::iter::{Filter}; +use core::iter::{Filter, AdditiveIterator}; use core::str::{CharSplits, StrSlice}; use core::iter::Iterator; use u_char; +use u_char::UnicodeChar; /// An iterator over the words of a string, separated by a sequence of whitespace pub type Words<'a> = @@ -78,7 +79,7 @@ pub trait UnicodeStrSlice<'a> { /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/) /// recommends that these characters be treated as 1 column (i.e., /// `is_cjk` = `false`) if the locale is unknown. - //fn width(&self, is_cjk: bool) -> uint; + fn width(&self, is_cjk: bool) -> uint; /// Returns a string with leading and trailing whitespace removed. fn trim(&self) -> &'a str; @@ -102,6 +103,11 @@ impl<'a> UnicodeStrSlice<'a> for &'a str { #[inline] fn is_alphanumeric(&self) -> bool { self.chars().all(u_char::is_alphanumeric) } + #[inline] + fn width(&self, is_cjk: bool) -> uint { + self.chars().map(|c| c.width(is_cjk).unwrap_or(0)).sum() + } + #[inline] fn trim(&self) -> &'a str { self.trim_left().trim_right()