add UnicodeStrSlice width() function
This commit is contained in:
parent
e62479133b
commit
c066a1ee9f
2 changed files with 17 additions and 2 deletions
|
@ -1056,6 +1056,15 @@ mod tests {
|
||||||
assert_eq!("\u2620".char_len(), 1u);
|
assert_eq!("\u2620".char_len(), 1u);
|
||||||
assert_eq!("\U0001d11e".char_len(), 1u);
|
assert_eq!("\U0001d11e".char_len(), 1u);
|
||||||
assert_eq!("ประเทศไทย中华Việt Nam".char_len(), 19u);
|
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]
|
#[test]
|
||||||
|
|
|
@ -16,10 +16,11 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use core::collections::Collection;
|
use core::collections::Collection;
|
||||||
use core::iter::{Filter};
|
use core::iter::{Filter, AdditiveIterator};
|
||||||
use core::str::{CharSplits, StrSlice};
|
use core::str::{CharSplits, StrSlice};
|
||||||
use core::iter::Iterator;
|
use core::iter::Iterator;
|
||||||
use u_char;
|
use u_char;
|
||||||
|
use u_char::UnicodeChar;
|
||||||
|
|
||||||
/// An iterator over the words of a string, separated by a sequence of whitespace
|
/// An iterator over the words of a string, separated by a sequence of whitespace
|
||||||
pub type Words<'a> =
|
pub type Words<'a> =
|
||||||
|
@ -78,7 +79,7 @@ pub trait UnicodeStrSlice<'a> {
|
||||||
/// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
|
/// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/)
|
||||||
/// recommends that these characters be treated as 1 column (i.e.,
|
/// recommends that these characters be treated as 1 column (i.e.,
|
||||||
/// `is_cjk` = `false`) if the locale is unknown.
|
/// `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.
|
/// Returns a string with leading and trailing whitespace removed.
|
||||||
fn trim(&self) -> &'a str;
|
fn trim(&self) -> &'a str;
|
||||||
|
@ -102,6 +103,11 @@ impl<'a> UnicodeStrSlice<'a> for &'a str {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn is_alphanumeric(&self) -> bool { self.chars().all(u_char::is_alphanumeric) }
|
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]
|
#[inline]
|
||||||
fn trim(&self) -> &'a str {
|
fn trim(&self) -> &'a str {
|
||||||
self.trim_left().trim_right()
|
self.trim_left().trim_right()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue