Trait unicode::str::UnicodeStrPreludeExperimental
[-]
[+]
[src]
pub trait UnicodeStrPrelude {
fn graphemes<'a>(&'a self, is_extended: bool) -> Graphemes<'a>;
fn grapheme_indices<'a>(&'a self, is_extended: bool) -> GraphemeIndices<'a>;
fn words<'a>(&'a self) -> Words<'a>;
fn is_whitespace(&self) -> bool;
fn is_alphanumeric(&self) -> bool;
fn width(&self, is_cjk: bool) -> uint;
fn trim<'a>(&'a self) -> &'a str;
fn trim_left<'a>(&'a self) -> &'a str;
fn trim_right<'a>(&'a self) -> &'a str;
}Methods for Unicode string slices
Required Methods
fn graphemes<'a>(&'a self, is_extended: bool) -> Graphemes<'a>
Returns an iterator over the grapheme clusters of the string.
If is_extended is true, the iterator is over the extended grapheme clusters;
otherwise, the iterator is over the legacy grapheme clusters.
UAX#29
recommends extended grapheme cluster boundaries for general processing.
Example
fn main() { let gr1 = "a\u0310e\u0301o\u0308\u0332".graphemes(true).collect::<Vec<&str>>(); let b: &[_] = &["a\u0310", "e\u0301", "o\u0308\u0332"]; assert_eq!(gr1.as_slice(), b); let gr2 = "a\r\nb🇷🇺🇸🇹".graphemes(true).collect::<Vec<&str>>(); let b: &[_] = &["a", "\r\n", "b", "🇷🇺🇸🇹"]; assert_eq!(gr2.as_slice(), b); }let gr1 = "a\u0310e\u0301o\u0308\u0332".graphemes(true).collect::<Vec<&str>>(); let b: &[_] = &["a\u0310", "e\u0301", "o\u0308\u0332"]; assert_eq!(gr1.as_slice(), b); let gr2 = "a\r\nb🇷🇺🇸🇹".graphemes(true).collect::<Vec<&str>>(); let b: &[_] = &["a", "\r\n", "b", "🇷🇺🇸🇹"]; assert_eq!(gr2.as_slice(), b);
fn grapheme_indices<'a>(&'a self, is_extended: bool) -> GraphemeIndices<'a>
Returns an iterator over the grapheme clusters of self and their byte offsets.
See graphemes() method for more information.
Example
fn main() { let gr_inds = "a̐éö̲\r\n".grapheme_indices(true).collect::<Vec<(uint, &str)>>(); let b: &[_] = &[(0u, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")]; assert_eq!(gr_inds.as_slice(), b); }let gr_inds = "a̐éö̲\r\n".grapheme_indices(true).collect::<Vec<(uint, &str)>>(); let b: &[_] = &[(0u, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")]; assert_eq!(gr_inds.as_slice(), b);
fn words<'a>(&'a self) -> Words<'a>
An iterator over the words of a string (subsequences separated by any sequence of whitespace). Sequences of whitespace are collapsed, so empty "words" are not included.
Example
fn main() { let some_words = " Mary had\ta little \n\t lamb"; let v: Vec<&str> = some_words.words().collect(); assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); }let some_words = " Mary had\ta little \n\t lamb"; let v: Vec<&str> = some_words.words().collect(); assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]);
fn is_whitespace(&self) -> bool
Returns true if the string contains only whitespace.
Whitespace characters are determined by char::is_whitespace.
Example
fn main() { assert!(" \t\n".is_whitespace()); assert!("".is_whitespace()); assert!( !"abc".is_whitespace()); }assert!(" \t\n".is_whitespace()); assert!("".is_whitespace()); assert!( !"abc".is_whitespace());
fn is_alphanumeric(&self) -> bool
Returns true if the string contains only alphanumeric code points.
Alphanumeric characters are determined by char::is_alphanumeric.
Example
fn main() { assert!("Löwe老虎Léopard123".is_alphanumeric()); assert!("".is_alphanumeric()); assert!( !" &*~".is_alphanumeric()); }assert!("Löwe老虎Léopard123".is_alphanumeric()); assert!("".is_alphanumeric()); assert!( !" &*~".is_alphanumeric());
fn width(&self, is_cjk: bool) -> uint
Returns a string's displayed width in columns, treating control characters as zero-width.
is_cjk determines behavior for characters in the Ambiguous category:
if is_cjk is true, these are 2 columns wide; otherwise, they are 1.
In CJK locales, is_cjk should be true, else it should be false.
Unicode Standard Annex #11
recommends that these characters be treated as 1 column (i.e.,
is_cjk = false) if the locale is unknown.
fn trim<'a>(&'a self) -> &'a str
Returns a string with leading and trailing whitespace removed.
fn trim_left<'a>(&'a self) -> &'a str
Returns a string with leading whitespace removed.
fn trim_right<'a>(&'a self) -> &'a str
Returns a string with trailing whitespace removed.
Implementors
impl UnicodeStrPrelude for str