[][src]Trait core_extensions::prelude::StringExt

pub trait StringExt: Borrow<str> {
    fn previous_char_boundary(&self, index: usize) -> usize { ... }
fn next_char_boundary(&self, index: usize) -> usize { ... }
fn left_char_boundary(&self, index: usize) -> usize { ... }
fn right_char_boundary(&self, index: usize) -> usize { ... }
fn get_offset_inside_of(&self, parent: &str) -> Option<usize> { ... }
fn offset_inside_of(&self, parent: &str) -> usize { ... }
fn is_substring_of(&self, parent: &str) -> bool { ... }
fn split_while<'a, P, T: Eq + Clone>(
        &'a self,
        mapper: P
    ) -> SplitWhile<'a, P, T>
    where
        P: FnMut(char) -> T
, { ... }
fn rsplit_while<'a, P, T: Eq + Clone>(
        &'a self,
        mapper: P
    ) -> RSplitWhile<'a, P, T>
    where
        P: FnMut(char) -> T
, { ... }
fn get_nth_char_index(&self, n: usize) -> Option<usize> { ... }
fn nth_char_index(&self, n: usize) -> usize { ... }
fn nth_char(&self, n: usize) -> Option<char> { ... }
fn first_chars(&self, n: usize) -> &str { ... }
fn last_chars(&self, n: usize) -> &str { ... }
fn from_nth_char(&self, n: usize) -> &str { ... }
fn calc_len_utf16(&self) -> usize { ... }
fn get_char_at(&self, at_byte: usize) -> Option<char> { ... }
fn char_indices_to(&self, to: usize) -> CharIndices { ... }
fn char_indices_from(&self, from: usize) -> CharIndicesFrom { ... }
fn left_pad(&self, how_much: usize) -> String { ... }
fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a> { ... }
fn line_indentation(&self) -> usize { ... }
fn min_indentation(&self) -> usize { ... }
fn max_indentation(&self) -> usize { ... } }

Extension trait for strings (any type that borrows as str).

Provided methods

fn previous_char_boundary(&self, index: usize) -> usize

Returns the previous character boundary,stopping at 0.

if index>self.len() ,returns self.len()

Example

use core_extensions::strings::StringExt;

let word="niño";
assert_eq!(word.previous_char_boundary(0),0);
assert_eq!(word.previous_char_boundary(1),0);
assert_eq!(word.previous_char_boundary(2),1);
// This index is inside of 'ñ'
assert_eq!(word.previous_char_boundary(3),2);
assert_eq!(word.previous_char_boundary(4),2);
assert_eq!(word.previous_char_boundary(5),4);
assert_eq!(word.previous_char_boundary(6),5);
assert_eq!(word.previous_char_boundary(7),5);
assert_eq!(word.previous_char_boundary(8),5);

fn next_char_boundary(&self, index: usize) -> usize

Returns the next character boundary. if index>self.len() ,returns self.len()

Example

use core_extensions::strings::StringExt;

let word="niño";
assert_eq!(word.next_char_boundary(0),1);
assert_eq!(word.next_char_boundary(1),2);
assert_eq!(word.next_char_boundary(2),4);
// This index is inside of 'ñ'
assert_eq!(word.next_char_boundary(3),4);
assert_eq!(word.next_char_boundary(4),5);
assert_eq!(word.next_char_boundary(5),5);
assert_eq!(word.next_char_boundary(6),5);

fn left_char_boundary(&self, index: usize) -> usize

If index is at a character boundary it returns index, otherwise it returns the previous character boundary.

if index>self.len() ,returns self.len()

Example

use core_extensions::strings::StringExt;

let word="niño";
assert_eq!(word.left_char_boundary(0),0);
assert_eq!(word.left_char_boundary(1),1);
assert_eq!(word.left_char_boundary(2),2);
// This index is inside of 'ñ'
assert_eq!(word.left_char_boundary(3),2);
assert_eq!(word.left_char_boundary(4),4);
assert_eq!(word.left_char_boundary(5),5);
assert_eq!(word.left_char_boundary(6),5);
assert_eq!(word.left_char_boundary(7),5);

fn right_char_boundary(&self, index: usize) -> usize

If index is at a character boundary it returns index, otherwise it returns the next character boundary.

if index>self.len() ,returns self.len()

Example

use core_extensions::strings::StringExt;

let word="niño";
assert_eq!(word.right_char_boundary(0),0);
assert_eq!(word.right_char_boundary(1),1);
assert_eq!(word.right_char_boundary(2),2);
// This index is inside of 'ñ'
assert_eq!(word.right_char_boundary(3),4);
assert_eq!(word.right_char_boundary(4),4);
assert_eq!(word.right_char_boundary(5),5);
assert_eq!(word.right_char_boundary(6),5);
assert_eq!(word.right_char_boundary(7),5);

fn get_offset_inside_of(&self, parent: &str) -> Option<usize>

Returns the offset of a substring inside of the other str.

Returns None if self is not a substring of other.

This operation is constant time.Purely done using pointer comparisons.

Example

use core_extensions::strings::StringExt;
let text="What is this?";
assert_eq!(
    vec![(0,"What"),(5,"is"),(8,"this?")],
    text.split_whitespace()
        .filter_map(|w|{
            w.get_offset_inside_of(text)
                .map(|x| (x,w) )
        })
        .collect::<Vec<_>>()
);

fn offset_inside_of(&self, parent: &str) -> usize

Returns the offset of a substring inside of the other str.

Returns None if self is not a substring of other.

This operation is constant time.Purely done using pointer comparisons.

Example

use core_extensions::strings::StringExt;
let text="What is this?";
assert_eq!(
    vec![(0,"What"),(5,"is"),(8,"this?")],
    text.split_whitespace()
        .map(|w| (w.offset_inside_of(text),w) )
        .collect::<Vec<_>>()
);

fn is_substring_of(&self, parent: &str) -> bool

Returns whether self is a substring (in memory) of parent.

Example

use core_extensions::strings::StringExt;
use core_extensions::SliceExt;
let text="What is this?";
for i in 0..text.len() {
    let sub=text.slice_lossy(i..text.len(),());
    assert!(sub.is_substring_of(text));
}

assert!( !"What".to_string().is_substring_of(text));
assert!( !"is"  .to_string().is_substring_of(text));
assert!( !"this".to_string().is_substring_of(text));
assert!( !"".is_substring_of(text));

Important traits for SplitWhile<'a, P, T>
fn split_while<'a, P, T: Eq + Clone>(
    &'a self,
    mapper: P
) -> SplitWhile<'a, P, T> where
    P: FnMut(char) -> T, 

Returns an iterator over substrings whose chars were mapped to the same key by mapper.

Returns an impl Iterator<Item=KeyStr<T>>

Example

use core_extensions::strings::StringExt;

fn func<U:Eq+Clone,F>(s:&str,f:F)->Vec<(U,&str)>
where F:FnMut(char)->U
{
    s.split_while(f).map(|v| (v.key,v.str) ).collect()
}

assert_eq!(
    "Hello, world!"
        .split_while(|c|c.is_alphabetic())
        .filter(|v| v.key==true )
        .map(|v| v.str )
        .collect::<Vec<_>>(),
    vec!["Hello","world"]
);
assert_eq!(
    vec![(true,"Hello"),(false,", "),(true,"world"),(false,"!")] ,
    func("Hello, world!",|c| c.is_alphanumeric())
);
assert_eq!(
    vec![('a',"aaa"),('b',"bbb"),('c',"ccc")] ,
    func("aaabbbccc",|c|c)
);

Important traits for RSplitWhile<'a, P, T>
fn rsplit_while<'a, P, T: Eq + Clone>(
    &'a self,
    mapper: P
) -> RSplitWhile<'a, P, T> where
    P: FnMut(char) -> T, 

A variation of split_while that iterates from the right(the order of substrings is reversed).

Returns an impl Iterator<Item=KeyStr<T>>

Example

use core_extensions::strings::StringExt;

fn func<U:Eq+Clone,F>(s:&str,f:F)->Vec<(U,&str)>
where F:FnMut(char)->U
{
    s.rsplit_while(f).map(|v| (v.key,v.str) ).collect()
}

assert_eq!(
    "Hello, world!"
        .rsplit_while(|c|c.is_alphabetic())
        .filter(|v| v.key==true )
        .map(|v| v.str )
        .collect::<Vec<_>>(),
    vec!["world","Hello"]
);
assert_eq!(
    vec![(false,"!"),(true,"world"),(false,", "),(true,"Hello")] ,
    func("Hello, world!",|c| c.is_alphanumeric() )
);
assert_eq!(vec![('c',"ccc"),('b',"bbb"),('a',"aaa")],func("aaabbbccc",|c|c));

fn get_nth_char_index(&self, n: usize) -> Option<usize>

returns the position of the nth character

if there is no nth character it returns None

This operation takes O(n) time,where n is self.len().

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.get_nth_char_index(0),Some(0));
assert_eq!(word.get_nth_char_index(1),Some(1));
assert_eq!(word.get_nth_char_index(2),Some(2));
assert_eq!(word.get_nth_char_index(3),Some(4));
assert_eq!(word.get_nth_char_index(4),None);
assert_eq!(word.get_nth_char_index(5),None);
assert_eq!(word.get_nth_char_index(6),None);

fn nth_char_index(&self, n: usize) -> usize

returns the position of the nth character

if there is no nth character it returns self.len()

This operation takes O(n) time,where n is self.len().

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.nth_char_index(0),0);
assert_eq!(word.nth_char_index(1),1);
assert_eq!(word.nth_char_index(2),2);
assert_eq!(word.nth_char_index(3),4);
assert_eq!(word.nth_char_index(4),5);
assert_eq!(word.nth_char_index(5),5);
assert_eq!(word.nth_char_index(6),5);

fn nth_char(&self, n: usize) -> Option<char>

Returns the nth character in the str.

This operation takes O(n) time,where n is self.len().

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.nth_char(0),Some('n'));
assert_eq!(word.nth_char(1),Some('i'));
assert_eq!(word.nth_char(2),Some('ñ'));
assert_eq!(word.nth_char(3),Some('o'));
assert_eq!(word.nth_char(4),None);
assert_eq!(word.nth_char(5),None);
assert_eq!(word.nth_char(6),None);

fn first_chars(&self, n: usize) -> &str

returns a string containing the first n chars. if n > self.chars().count() it returns the entire string

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.first_chars(0),"");
assert_eq!(word.first_chars(1),"n");
assert_eq!(word.first_chars(2),"ni");
assert_eq!(word.first_chars(3),"niñ");
assert_eq!(word.first_chars(4),"niño");
assert_eq!(word.first_chars(5),"niño");
assert_eq!(word.first_chars(6),"niño");

fn last_chars(&self, n: usize) -> &str

returns a string containing the last n chars if n > self.chars().count() it returns the entire string

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.last_chars(0),"");
assert_eq!(word.last_chars(1),"o");
assert_eq!(word.last_chars(2),"ño");
assert_eq!(word.last_chars(3),"iño");
assert_eq!(word.last_chars(4),"niño");
assert_eq!(word.last_chars(5),"niño");
assert_eq!(word.last_chars(6),"niño");

fn from_nth_char(&self, n: usize) -> &str

returns the string from the nth character if n > self.chars().count() it returns an empty string

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.from_nth_char(0),"niño");
assert_eq!(word.from_nth_char(1),"iño");
assert_eq!(word.from_nth_char(2),"ño");
assert_eq!(word.from_nth_char(3),"o");
assert_eq!(word.from_nth_char(4),"");
assert_eq!(word.from_nth_char(5),"");
assert_eq!(word.from_nth_char(6),"");

fn calc_len_utf16(&self) -> usize

returns the length of the text in utf16

warning

This is calculated every time the function is called.

Example

use core_extensions::strings::StringExt;

assert_eq!("niño".calc_len_utf16(),4);
assert_eq!("ññññ".calc_len_utf16(),4);

fn get_char_at(&self, at_byte: usize) -> Option<char>

Returns the character at at_byte if it's an index inside of the str.

Example

use core_extensions::strings::StringExt;
let word="niño";

assert_eq!(word.get_char_at(0),Some('n'));
assert_eq!(word.get_char_at(1),Some('i'));
assert_eq!(word.get_char_at(2),Some('ñ'));
assert_eq!(word.get_char_at(3),Some('ñ'));
assert_eq!(word.get_char_at(4),Some('o'));
assert_eq!(word.get_char_at(5),None);
assert_eq!(word.get_char_at(6),None);

fn char_indices_to(&self, to: usize) -> CharIndices

Returns an iterator over (index,char) pairs up to (but not including) to.

Example

use core_extensions::strings::StringExt;
let word="niño";

fn func(s:&str,i:usize)->(Vec<usize>,String){
    (
        s.char_indices_to(i).map(|(i,_)|i).collect(),
        s.char_indices_to(i).map(|(_,c)|c).collect(),
    )
}
assert_eq!(func(word,0),(vec![]       ,""    .to_string()));
assert_eq!(func(word,1),(vec![0]      ,"n"   .to_string()));
assert_eq!(func(word,2),(vec![0,1]    ,"ni"  .to_string()));
assert_eq!(func(word,3),(vec![0,1,2]  ,"niñ" .to_string()));
assert_eq!(func(word,4),(vec![0,1,2]  ,"niñ" .to_string()));
assert_eq!(func(word,5),(vec![0,1,2,4],"niño".to_string()));
assert_eq!(func(word,6),(vec![0,1,2,4],"niño".to_string()));
assert_eq!(func(word,7),(vec![0,1,2,4],"niño".to_string()));

Important traits for CharIndicesFrom<'a>
fn char_indices_from(&self, from: usize) -> CharIndicesFrom

Returns an iterator over (index,char) pairs from from.

Example

use core_extensions::strings::StringExt;
let word="niño";

fn func(s:&str,i:usize)->(Vec<usize>,String){
    (
        s.char_indices_from(i).map(|(i,_)|i).collect(),
        s.char_indices_from(i).map(|(_,c)|c).collect(),
    )
}
assert_eq!(func(word,0),(vec![0,1,2,4],"niño".to_string()));
assert_eq!(func(word,1),(vec![1,2,4]  ,"iño" .to_string()));
assert_eq!(func(word,2),(vec![2,4]    ,"ño"  .to_string()));
assert_eq!(func(word,3),(vec![2,4]    ,"ño"  .to_string()));
assert_eq!(func(word,4),(vec![4]      ,"o"   .to_string()));
assert_eq!(func(word,5),(vec![]       ,""    .to_string()));
assert_eq!(func(word,6),(vec![]       ,""    .to_string()));
assert_eq!(func(word,7),(vec![]       ,""    .to_string()));

fn left_pad(&self, how_much: usize) -> String

Pads the string on the left with how_much additional spaces.

Example

use core_extensions::strings::StringExt;
assert_eq!(
    "what\n  the\n    hell".left_pad(4),
    "    what\n      the\n        hell");

fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a>

Pads the string on the left with how_much additional spaces in the Display impl of LeftPadder.

Use this to avoid allocating an extra string.

fn line_indentation(&self) -> usize

The indentation of the first line.

Example

use core_extensions::strings::StringExt;
assert_eq!("".line_indentation(),0);
assert_eq!("    ".line_indentation(),4);
assert_eq!("    \n      word".line_indentation(),4);
assert_eq!("    \nword".line_indentation(),4);

fn min_indentation(&self) -> usize

The minimum indentation of the string.

Example

use core_extensions::strings::StringExt;
assert_eq!("".min_indentation(),0);
assert_eq!("    ".min_indentation(),4);
assert_eq!("    \n      word".min_indentation(),4);
assert_eq!("    \nword".min_indentation(),0);

fn max_indentation(&self) -> usize

The maximum indentation of the string.

Example

use core_extensions::strings::StringExt;
assert_eq!("".max_indentation(),0);
assert_eq!("    ".max_indentation(),4);
assert_eq!("    \n      word".max_indentation(),6);
assert_eq!("    \nword".max_indentation(),4);
Loading content...

Implementors

impl<T: ?Sized> StringExt for T where
    T: Borrow<str>, 
[src]

fn previous_char_boundary(&self, index: usize) -> usize[src]

fn next_char_boundary(&self, index: usize) -> usize[src]

fn left_char_boundary(&self, index: usize) -> usize[src]

fn right_char_boundary(&self, index: usize) -> usize[src]

fn get_offset_inside_of(&self, parent: &str) -> Option<usize>[src]

fn offset_inside_of(&self, parent: &str) -> usize[src]

fn is_substring_of(&self, parent: &str) -> bool[src]

Important traits for SplitWhile<'a, P, T>
fn split_while<'a, P, T: Eq + Clone>(
    &'a self,
    mapper: P
) -> SplitWhile<'a, P, T> where
    P: FnMut(char) -> T, 
[src]

Important traits for RSplitWhile<'a, P, T>
fn rsplit_while<'a, P, T: Eq + Clone>(
    &'a self,
    mapper: P
) -> RSplitWhile<'a, P, T> where
    P: FnMut(char) -> T, 
[src]

fn get_nth_char_index(&self, n: usize) -> Option<usize>[src]

fn nth_char_index(&self, n: usize) -> usize[src]

fn nth_char(&self, n: usize) -> Option<char>[src]

fn first_chars(&self, n: usize) -> &str[src]

fn last_chars(&self, n: usize) -> &str[src]

fn from_nth_char(&self, n: usize) -> &str[src]

fn calc_len_utf16(&self) -> usize[src]

fn get_char_at(&self, at_byte: usize) -> Option<char>[src]

fn char_indices_to(&self, to: usize) -> CharIndices[src]

Important traits for CharIndicesFrom<'a>
fn char_indices_from(&self, from: usize) -> CharIndicesFrom[src]

fn left_pad(&self, how_much: usize) -> String[src]

fn left_padder<'a>(&'a self, how_much: usize) -> LeftPadder<'a>[src]

fn line_indentation(&self) -> usize[src]

fn min_indentation(&self) -> usize[src]

fn max_indentation(&self) -> usize[src]

Loading content...