add UnicodeStrSlice width() function

This commit is contained in:
kwantam 2014-07-10 20:28:40 -04:00
parent e62479133b
commit c066a1ee9f
2 changed files with 17 additions and 2 deletions

View File

@ -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!("".width(false), 10u);
assert_eq!("".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]

View File

@ -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()